一場Nginx https配置調試過程

原文連接:何曉東 博客html

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

開始配置

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

nginx.conf https配置git

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 不能訪問,可怕了,開始排錯。github

檢查配置和本地端口

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

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

使用 nginx -t 測試配置
segmentfault

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 自身的測試沒有報錯。centos

檢查本機端口監聽

使用 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;
    # 其餘配置不變
    
    ...
}

參考連接:官方文檔

學習 更多知識

© 原創文章

相關文章
相關標籤/搜索