http升级https 教程 nginx 版

一、去你所购买的服务器运营商申请一个SSL证书

下面是腾讯云的申请教程

二、下载证书后的配置

下载之后找到你服务器对应的文件夹(我的是nginx)

将文件上传到服务器的随意文件夹

为了方便我上传到了自己在根目录建的key文件夹

需要有一个操作将crt文件转为pem,才能配置nginx

1_shaojiaxi.com_bundle.crt 这个是你下载的crt文件
myshaocert.pem 这个是你转出的文件,随便写名字,后缀不要变就行

text
1
openssl x509 -in 1_shaojiaxi.com_bundle.crt -out myshaocert.pem -outform PE
修改nginx 的配置 nginx.conf

我给你来个修改前修改后,你做对比,加入到你的配置就可以了

修改前配置:

xml
1 2 3 4 5 6 7 8 9 10 11
 server {
            listen   80;   #监听端口
            server_name  localhost;   #监听地址       
            location  / {       #请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。
               try_files $uri $uri/ /index.html;
            }
        location /api/ {
            proxy_pass http://127.0.0.1:8888/;
            proxy_redirect default;
        }
    }

修改后配置:

xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
server {
            listen   80;   #监听端口
            server_name  localhost;   #监听地址       
            listen 443 ssl;
           ssl_certificate   /key/myshaocert.pem; ##你证书文件的位置
           ssl_certificate_key  /key/2_shaojiaxi.com.key; ##你证书文件的位置
           ssl_session_timeout 5m;
           ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
           ssl_prefer_server_ciphers on;
            location  / {       #请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。
               try_files $uri $uri/ /index.html;
            }
        location /api/ {
            proxy_pass http://127.0.0.1:8888/;
            proxy_redirect default;
        }
        if ( $scheme != "https" ) {
                  return 301 https://$host$request_uri;    ##永久跳转到https
            }   
    }

如果nginx 出现类似报错 (the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:13)

后续编写中