一场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;
    # 其余配置不变
    
    ...
}

参考连接:官方文档

学习 更多知识

© 原创文章

相关文章
相关标签/搜索