腾讯云服务器nginx配置SSL证书实现https访问

ppylox
·3/14/2020·0 阅读
AI 摘要模型根据正文自动生成,仅供参考

本文介绍在腾讯云服务器上通过Nginx配置SSL证书实现HTTPS访问:申请并下载免费SSL证书,开放安全组443端口,将Nginx证书文件放入配置目录;在nginx.conf中配置监听443的server块,指定证书与私钥路径并启用ssl;另增监听80的server块,用rewrite permanent将HTTP请求重定向至HTTPS;最后重启Nginx生效。

1、由于我是在买的是腾讯云主机,在腾讯云上可以申请免费ssl证书,申请通过后就可以下载证书文件,另外先打开安全组里面的443端口。

2、腾讯云的SSL证书下载包中,有一个单独的Nginx文件夹,里面有两个文件都是我们需要的,我们需要把这两个文件放到我们的服务器中,如果是linux系统,我目前是放到/usr/local/nginx/conf,也就是nginx的配置目录下。

3、使用vim打开nginx配置文件

vim /usr/local/nginx/conf/nginx.conf

配置文件内容:

server {\n    listen 443;\n    server_name _;\n    ssl_certificate /usr/local/nginx/conf/1_www.dsiab.com_bundle.crt;\n    ssl_certificate_key /usr/local/nginx/conf/2_www.dsiab.com.key;\n    ssl on;\n    root /data/wwwroot/default/;\n    index index.html index.htm;\n    ...\n}\n

5、如果用户使用的是http协议进行访问,那么默认监听的端口是80端口,所以我们需要做一个重定向,我们在上一个代码块的基础上增加一个server节点提供重定向服务:

server {\n    listen 80;\n    server_name _;\n    rewrite ^/(.*)$ https://www.dsiab.com:443/ permanent;\n}\n

所以我的nginx完整的配置内容如下:

server {\n    listen 443;\n    server_name _;\n    ssl_certificate /usr/local/nginx/conf/1_www.dsiab.com_bundle.crt;\n    ssl_certificate_key /usr/local/nginx/conf/2_www.dsiab.com.key;\n    ssl on;**\n    root /data/wwwroot/default/;\n    index index.html index.htm;\n    location /nginx_status {\n      stub_status on;\n      access_log off;\n      allow 127.0.0.1;\n      deny all;\n    }\n    location ~ .*\\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {\n      expires 30d;\n      access_log off;\n    }\n    location ~ .*\\.(js|css)?$ {\n      expires 7d;\n      access_log off;\n    }\n    location /bootService {\n      proxy_pass http://127.0.0.1:8080;\n    }\n    location / {\n      try_files $uri $uri/ /index.html;\n      include proxy.conf;\n    }\n    location ~ ^/(\\.user.ini|\\.ht|\\.git|\\.svn|\\.project|LICENSE|README.md) {\n      deny all;\n    }\n  }\n  ## 80重定向443\n  server {\n    listen 80;\n    server_name _;\n    rewrite ^/(.*)$ https://www.dsiab.com:443/ permanent;\n  }\n}\n

7、启动nginx服务:service nginx restart,重启完就可以通过https访问了。

相关文章

评论 (0)

暂无评论,来写第一条吧