Https配置教程 2020 (nginx)

邵嘉喜 程序猿

时间: 2020-10-19 阅读: 251 字数:3475

{}
腾讯云的案例教程

http升级https 教程 nginx 版

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

下面是腾讯云的申请教程

二、下载证书后的配置

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

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

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

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

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

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

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

修改前配置:

 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;
        }
    }

修改后配置:

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)

后续编写中

本文章网址:https://www.sjxi.cn/detil/f21e647e5f134e0b9af6320891b69cba

最新评论

当前未登陆哦
登陆后才可评论哦

湘ICP备2021009447号