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}\n5、如果用户使用的是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}\n7、启动nginx服务:service nginx restart,重启完就可以通过https访问了。