美文网首页
nginx https TLSv1.0访问失败

nginx https TLSv1.0访问失败

作者: Always_July | 来源:发表于2022-11-27 14:48 被阅读0次

    1. 问题描述

    某一个客户对接系统,发现不能正常访问,使用Https调用接口。但其他客户能正常访问,Postman也能正常访问。抓包发现当前客户使用jdk1.6版本,默认的https连接的TLSv版本是1.0,其他客户使用TLSv 1.1, 1.2 。

    问题复现

    curl --tls-max 1.0 -k -X GET https://rule.baidu.com --verbose
    Note: Unnecessary use of -X or --request, GET is already inferred.
      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                     Dload  Upload   Total   Spent    Left  Speed
      0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0*   Trying 127.0.0.1:443...
    * Connected to rule.baidu.com (127.0.0.1) port 443 (#0)
    * ALPN: offers h2
    * ALPN: offers http/1.1
    } [5 bytes data]
    * TLSv1.0 (OUT), TLS handshake, Client hello (1):
    } [140 bytes data]
    * TLSv1.0 (IN), TLS alert, protocol version (582):
    { [2 bytes data]
    * error:1409442E:SSL routines:ssl3_read_bytes:tlsv1 alert protocol version
      0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
    * Closing connection 0
    curl: (35) error:1409442E:SSL routines:ssl3_read_bytes:tlsv1 alert protocol version
    

    使用curl 命令测试,我使用的shell的是windows上的bash shell,可以curl -all 查看如何指定ssl版本。

    # windows 上的bash shell
    curl --tls-max 1.0 -k -X GET https://devrule.baidu.com 
    -k 忽略 https验证 
    # centos bash
    curl --tlsv1.0 -k -X GET https://rule.baidu.com
    
    # 可以添加--verbose  查看详细的握手流程
    curl --tlsv1.0 -k -X GET https://rule.baidu.com --verbose
    

    2. 环境与配置

    nginx 版本

    nginx -v
    nginx version: openresty/1.13.6.2
    

    nginx 配置

    有devrule.baidu.com.conf和rule.baidu.com.conf两个配置

    devrule.baidu.com.conf

    server
    {
        listen 443;
        server_name devrule.baidu.com;
        access_log logs/access.log  main;
        ssl on;
        ssl_certificate /usr/local/openresty/nginx/conf/cert/server.crt;
        ssl_certificate_key /usr/local/openresty/nginx/conf/cert/server.key;
        ssl_session_timeout 5m;
        #ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_protocols TLSv1.1 TLSv1.2;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
        ssl_prefer_server_ciphers on;
        index index.html index.htm index.jsp index.do;
        root /data;
    
        location  ^~ /rulemanager/ {
            proxy_set_header X-Real-Ip $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://39.156.66.10;
        }
    }
    

    rule.baidu.com.conf

    server
    {
        listen 443;
        server_name rule.baidu.com;
        access_log logs/access.log  main;
        ssl on;
        ssl_certificate /usr/local/openresty/nginx/conf/cert/server.crt;
        ssl_certificate_key /usr/local/openresty/nginx/conf/cert/server.key;
        ssl_session_timeout 5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        #ssl_protocols TLSv1.1 TLSv1.2;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
        ssl_prefer_server_ciphers on;
        index index.html index.htm index.jsp index.do;
        root /data;
    
        location  ^~ /rulemanager/ {
            proxy_set_header X-Real-Ip $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://39.156.66.10;
        }
    }
    

    可以看到devrule.baidu.com不支持TLSv1 , rule.baidu.com支持TLSv1 ,但是使用TLSv 1.0访问rule.baidu.com 失败。

    $ curl --tls-max 1.0 -k -X GET https://rule.baidu.com --verbose
    Note: Unnecessary use of -X or --request, GET is already inferred.
      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                     Dload  Upload   Total   Spent    Left  Speed
      0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0*   Trying 127.0.0.1:443...
    * Connected to rule.baidu.com (127.0.0.1) port 443 (#0)
    * ALPN: offers h2
    * ALPN: offers http/1.1
    } [5 bytes data]
    * TLSv1.0 (OUT), TLS handshake, Client hello (1):
    } [140 bytes data]
    * TLSv1.0 (IN), TLS alert, protocol version (582):
    { [2 bytes data]
    * error:1409442E:SSL routines:ssl3_read_bytes:tlsv1 alert protocol version
      0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
    * Closing connection 0
    curl: (35) error:1409442E:SSL routines:ssl3_read_bytes:tlsv1 alert protocol version
    

    3. 问题原因

    猜想 devrule.baidu.com.conf 的ssl_protocols不支持TLSv1 影响了 rule.baidu.com.conf配置。

    现在将devrule.baidu.com.conf 的ssl_protocols修改为支持TLSv1,再将rule.baidu.com.conf改为不支持TLSv1。发现能访问 rule.baidu.com 了。

    Q: 为什么nginx 会忽略rule.baidu.com.conf的ssl_protocols而使用devrule.baidu.com.conf的ssl_protocols?
    A: 猜测nginx有多个配置文件时,它会使用按字母排序 第一个文件的ssl_protocols配置。

    新增 arule.baidu.com.conf

    server
    {
        listen 443;
        server_name arule.baidu.com;
        access_log logs/access.log  main;
        ssl on;
        ssl_certificate /usr/local/openresty/nginx/conf/cert/server.crt;
        ssl_certificate_key /usr/local/openresty/nginx/conf/cert/server.key;
        ssl_session_timeout 5m;
        #ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_protocols TLSv1.1 TLSv1.2;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
        ssl_prefer_server_ciphers on;
        index index.html index.htm index.jsp index.do;
        root /data;
    
        location  ^~ /rulemanager/ {
            proxy_set_header X-Real-Ip $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://39.156.66.10;
        }
    }
    

    在devrule.baidu.com.conf 的ssl_protocols支持TLSv1,rule.baidu.com.conf不支持TLSv1情况下。新增arule.baidu.com.conf , ssl_protocols 不支持 TLSv1 ,重新加载配置文件后访问rule.baidu.com失败,这个结果验证了我的猜想。

    4. 总结

    1. nginx ssl_protocols属于全局配置,只会使用第一个文件的配置,所以最好各个文件使用相同的配置。
    2. 网络问题一种调用方式可以,一种不可以,抓包对比。

    其他资料

    Nginx https 协议配置 ssl_protocols 的相关问题

    相关文章

      网友评论

          本文标题:nginx https TLSv1.0访问失败

          本文链接:https://www.haomeiwen.com/subject/yuvhfdtx.html