【Nginx】將http升級到https並且同時支持http和https兩種請求


一、如何將http升級到https

需要滿足下面三個:

1.域名
2.nginx
3.SSL證書
    一般第三方證書頒發機構下發的證書是收費的,一年好幾千。 
 
1) 從騰訊雲申請免費的SSL證書,有效期一年,可申請多個 
SSL 證書申請地址在這里: https://console.qcloud.com/ssl
申請過程幾分鍾就可以搞定,主要分兩步
1.申請免費的證書,設置手動DNS驗證
2.到域名對應的域名解析商處添加解析記錄 
下載申請好的域名,上傳到服務器指定位置
2)  nginx配置
2.1.使Nginx 支持 SSL
1)檢查 Nginx 是否支持 SSL
/usr/local/nginx/sbin/nginx  -V
configure arguments中是否有--with-http_ssl_module
如:
nginx version: nginx/1.13.4
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC)
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --with-http_ssl_module
2) 若不支持,為nginx添加SSL 模塊
進入nginx安裝目錄執行:
./configure --with-http_ssl_module 
然后,注意不要make install
make
3)備份原 Nginx 執行腳本
mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old
4)將新版本 Nginx 編譯腳本放到可執行文件目錄下
cd objs/ cp nginx /usr/local/nginx/sbin/
5)進行平滑升級
make upgrade
再次檢查是否安裝成功:
/usr/local/nginx/sbin/nginx -V 

2.2.編輯Nginx配置文件

cd /usr/local/nginx/conf
vim nginx.conf 
  server {
        listen       443 ssl;
        server_name  你的域名;

        ssl_certificate      你的證書.crt;
        ssl_certificate_key  你的證書.key;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;

        location / {
                proxy_pass  http://127.0.0.1:8080;
                proxy_redirect off;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

 

二、同時支持http和https兩種請求

nginx配置新增server的配置

# http -> https
 server {
        listen 80;
        server_name 你的域名;
        rewrite ^(.*)$ https://$host$1 permanent;
}
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM