1. 程式人生 > >nginx下http如何轉https訪問

nginx下http如何轉https訪問

  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訪問了