一場Nginx https配置調試過程

原文鏈接:何曉東 博客

測試環境爲:阿里雲 centos7.4 ,nginx1.14.3,其他版本的系統或者nginx如有不同,以官網爲準。

開始配置

最開始參考阿里雲棲社區的這篇 文章,在阿里雲控制面板進行配置,然後對應修改 nginx.conf 文件,執行 nginx -s reload 重載使之生效。

nginx.conf https配置

server {
    listen 443;
    server_name www.domain.com;
    ssl on;
    root html;
    index index.html index.htm;
    ssl_certificate   cert/domain.pem;
    ssl_certificate_key  cert/domain.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;
    location / {
        root html;
        index index.html index.htm;
    }
}

一番操作之後域名 https://www.domain.com 不能訪問,可怕了,開始排錯。

檢查配置和本地端口

檢查域名,證書位置,其他參數有沒有寫錯的

主要是看一下,沒有發現錯誤。

使用 nginx -t 測試配置

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

nginx 自身的測試沒有報錯。

檢查本機端口監聽

使用 netstat -anp |grep 443 命令,檢查443端口監聽,結果:
tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 15940/nginx: master 也正常,所以問題出在外部網絡端口上。

檢查防火牆端口及安全組規則

前提條件,使用 telnet www.domain.com 443 請求 443 端口,報錯 443 端口無法鏈接,所以開放 443 端口可以訪問。

檢查安全組設置

直接參考 官方文檔, 如果安全組沒有 443 端口,加上並且允許訪問就行,現在多少是默認開啓的。

firewall添加443端口

使用以下命令:

firewall-cmd --list-ports
#output 80/tcp
firewall-cmd --zone=public --add-port=443/tcp --permanent
firewall-cmd --reload

重載生效,理想情況下不會有什麼問題了,然後瀏覽器訪問還是無法連接。

在命令行下,執行 curl https://www.domain.com 檢查情況,返回報錯:

curl https://www.domain.com
curl: (35) SSL received a record that exceeded the maximum permissible length.

直接谷歌報錯信息,MD,nginx 高版本需要端口和 ssl 需要在一行,改爲 listen 443 ssl 然後去掉 ssl on 這行,再次 nginx -s reload 生效,一切正常了。搜到文章說 ssl on 這行在 nginx 1.15 版本中會報錯,沒去驗證了,高版本 nginx 將這兩個寫在一行就行。

最終配置爲:

server {
    listen 443 ssl;
    server_name www.domain.com;
    # 其他配置不變
    
    ...
}

參考鏈接:官方文檔

學習 更多知識

© 原創文章

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章