PHP值圈后端xuexiPHP实战

nginx配置https,nginx ssl配置

2018-01-17  本文已影响119人  whmblog

首先在阿里云或腾讯云申请免费的证书,选择文件验证方式生成证书。然后就是nginx虚拟主机配置文件的修改。以下是我的配置文件,适用于安全性较高,微信公众号和微信小程序的相关网站。
只需要关注带有ssl的配置选项,我增加了一个监听80和443的端口,同时增加了http跳转到https的配置

阿里云申请证书网址: https://common-buy.aliyun.com/?spm=5176.2020520163.cas.1.5aceb260AoQD9t&commodityCode=cas#/buy

腾讯云申请CA证书地址: https://buy.cloud.tencent.com/ssl?fromSource=ssl&from=qcloudHpHeaderSsl

相关配置文件示例如下:

server
    {
        listen 443;
        listen 80;

        
        server_name www.whmblog.cn;
        index index.html index.htm index.php default.html default.htm default.php;
        root  /data/wwwroot/www.whmblog.cn;

        #强制https
        if ($scheme = http) {
            return   301 https://$host$request_uri;
        }

        #Https配置区域
        ssl on;
        ssl_certificate  ../cert/214198214130804.pem;
        ssl_certificate_key ../cert/214198214130804.key;
        ssl_session_timeout 5m;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;        
        #Https配置区域结束


        include none.conf;
        #error_page   404   /404.html;
        location ~ [^/]\.php(/|$)
        {
            # comment try_files $uri =404; to enable pathinfo
            try_files $uri =404;
            fastcgi_pass  unix:/tmp/php-cgi.sock;
            fastcgi_index index.php;
            include fastcgi.conf;
            #include pathinfo.conf;
        }

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }

        location ~ .*\.(js|css)?$
        {
            expires      12h;
        }
        # 服务器日志,请根据自己服务器更改
        access_log off;
    }

注意:如果无法访问请检查:
1. 443端口公网是否已经开放
2. 证书文件位置是否正确
3.不要怀疑其他问题,基本是你配置不对。
成功后的效果:

1.png

转载请标明原文链接: https://www.jianshu.com/p/8247f80c8151
点击你的鼠标见证事实: https://www.whmblog.cn

上一篇 下一篇

猜你喜欢

热点阅读