美文网首页阿里云推广
nginx下http如何转https访问

nginx下http如何转https访问

作者: StimmerLove | 来源:发表于2018-09-21 10:41 被阅读6次

    1. 申请ssl证书

    可以到腾讯云中申请一个免费的ssl证书,下载证书找到nginx目录下的 .crt,.key结尾的文件;申请过程可以参考我的另一篇博客

    2. 证书安装

    将下载下来的证书,上传到你的服务器的某一位置上,例如:/usr/local/nginx/文件下

    3. nginx配置

    https通常监听443端口,所以我们也是监听443端口;然后在nginx.conf或者你的vhost的配置文件添加一个server{};

    server {
            listen 443 ssl;
            server_name zaozu.mai1.com;
            charset      utf-8;
    
            index  index.php index.html;
            root /home/www/zhaozu/amai-www/public; //项目根目录root不能使用变量的形式
     ssl on;
            ssl_certificate /usr/local/nginx/zaozu/1_zaozu.mai1.com_bundle.crt;
            ssl_certificate_key /usr/local/nginx/zaozu/2_zaozu.mai1.com.key;
            ssl_session_timeout 5m;
            ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
            ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
            ssl_prefer_server_ciphers on;
    
            location ~ \.php$ { //必须有,不然无法解析php文件;访问首页下载文件是因为无法解析php文件
                    try_files $uri =404;
                     fastcgi_buffer_size 128k;
                    fastcgi_buffers 32 32k;
                    fastcgi_pass   unix:/var/run/php5-fpm.sock;
                    #fastcgi_pass 127.0.0.1:9000;
                    fastcgi_index  index.php;
                    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                    include        fastcgi_params;
             }
            location / { // 让二级域名下的方法也能被https访问,不只是xxx.mai1.com能访问
                    try_files $uri $uri/ /index.php$is_args$args;
            }
    }
    

    4. 配置完成,你的网站就可以用http,https访问了

    相关文章

      网友评论

        本文标题:nginx下http如何转https访问

        本文链接:https://www.haomeiwen.com/subject/pakanftx.html