CentOS 7 配置nginx並默認強制使用https對http進行跳轉


1.安裝nginx

yum install nginx

2.啟動nginx服務

service nginx start

3.開啟防火牆80端口,雲服務器和本地虛擬服務器各有不同,不再贅述。

4.訪問你的域名,出現nginx頁面,成功。

關於https,首先需要下載ssl證書,本人使用的騰訊雲服務器,域名也是騰訊上買的域名服務,所以直接在騰訊雲申請了ssl dv證書。

還有一種方式通過openssl自己生成ssl證書,個人沒嘗試過,網上教程頗多。

5.ssl證書申請
這里建議使用騰訊雲申請免費ssl證書,一年免費,單域名模式下。當然如果有預算直接買泛域名的更好。

注:可以申請多個單域名模式證書,比如,www.yourdomain.com,    blog.yourdomain.com,  這樣就可以為二級域名設置https訪問。

大約半小時,騰訊就能審核通過。

參照騰訊的說明驗證通過后可以下載證書到本地,目錄如下:

E:\DOOFEETECH\00.公司\IT運維\www.yourdomain.COM
│  www.yourdomain.com.csr
│
├─Apache
│      1_root_bundle.crt
│      2_www.yourdomain.com.crt
│      3_www.yourdomain.com.key
│
├─IIS
│      www.yourdomain.com.pfx
│
├─Nginx
│      1_www.yourdomain.com_bundle.crt
│      2_www.yourdomain.com.key
│
└─Tomcat
        www.yourdomain.com.jks

各種主流web服務器的都提供了,這里我們用nginx的。

6.將ssl證書上傳至服務器,個人單獨建立了ssl文件目錄。

7.配置nginx.conf

我忘了nginx默認的配置文件在哪個位置,使用如下命令
nginx -t

發現默認的nginx.conf 在/etc/nginx/nginx.con

配置如下:

首先修改對80端口的監聽

server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;                           
        #經過試驗發現,在server_name里面可以不指定域名,兩種方式都OK
        #server_name www.yourdomain.com;          
        #rewrite     ^   https://$host$request_uri? permanent;
        rewrite ^(.*)$  https://$host$1 permanent; 
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

開啟防火牆443端口,並啟用對nginx中對443端口的監聽

server {
        listen       443 ssl http2 default_server;
        listen       [::]:443 ssl http2 default_server;
        #server_name  _;
        server_name  www.yourdomain.com;
        root         /usr/share/nginx/html;

        ssl_certificate "/home/ssl/keys/1_www.yourdomain.com_bundle.crt";
        ssl_certificate_key "/home/ssl/keys/2_www.yourdomain.com.key";
        ssl_session_cache shared:SSL:1m;
        ssl_session_timeout  10m;
        ssl_ciphers HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers on;
    
        

#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
        location / {
             
             tcp_nodelay on;
             proxy_set_header Host $host;
             proxy_set_header X-Real-IP $remote_addr;
             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
    }

8.重啟nginx或者重新加載配置

重啟 service nginx restart

重載 nginx -s reload

 

至此,使用http訪問你的域名,會自動跳轉到https。

 

參考:

https://www.jianshu.com/p/c0d2e5e77b0c

https://www.jianshu.com/p/9523d888cf77

https://www.jianshu.com/p/2a26539a9818

https://blog.csdn.net/h330531987/article/details/81481426

https://blog.csdn.net/zf5250/article/details/80429795


免責聲明!

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



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