【问题标题】:Nginx - Upstream Configuration IssueNginx - 上游配置问题
【发布时间】:2021-07-14 00:21:30
【问题描述】:

我注意到一个 nginx 配置上的一些东西。配置了 2 个完全相同的上游块:

upstream test1.example.com {
    server flaskapp.example.com:5000
}

server {
    listen 443 ssl;
    proxy_pass test1.example.com;
    ssl_certificate /opt/certs/example1.com.crt;
    ssl_certificate_key /opt/example1.com.key;
    ssl_protocols TLSv1.2;
    ssl ciphers "ECDHE-ECDSA-AES128-GCM-SHA256"
}

upstream test2.example.com {
    server flaskapp.example.com:5000
}

server {
    listen 443 ssl;
    proxy_pass test2.test.com;
    ssl_certificate /opt/certs/test.com.crt;
    ssl_certificate_key /opt/test.com.key;
    ssl_protocols TLSv1.2;
    ssl ciphers "ECDHE-ECDSA-AES128-GCM-SHA256"
}

我有 2 个服务器块在端口 443 上侦听。所以我有同一个服务器在同一个块上侦听 2 个单独的连接...如果这有意义的话。

我的想法是这会失败,因为监听传入 https 连接到 test1 和 test2.example.com 的同一台服务器也不知道路由请求的“位置”。但事实并非如此。

如果我转到https://test1.example.com,我会被路由到正确的应用程序。 https 按预期工作。

如果我转到https://test2.example.com,我会被路由到正确的应用程序。但是 https 没有按预期工作。这很令人困惑,因为这两个证书都是通配符证书。我不确定为什么 1 个成功,一个失败。

如果我注释掉第一个上游块:

# upstream test1.example.com { server flaskapp.example.com:5000 }
# server {proxy_pass test1.example.com; }

发生了一些奇怪的事情。连接到https://test2.test.com 在我的网络浏览器中给我一个“连接服务器失败”的错误消息。日志将其显示为错误:

No "ssl_certificate" is defined in server listening on SSL port while SSL handshaking

这是针对 test1.example.com 的,我知道通配符证书有效。我在其他地方使用它。因此,当我以这种方式访问​​ test1.example.com 时,我不确定为什么会出现“连接服务器失败”错误。

需要注意的几点:

test1.example.com 和 test2.test.com 都指向同一个 nginx 服务器。

  1. 如果两个上游/服务器块都在工作,则 test1.example.com 显示该站点是 ssl 安全的。这是意料之中的。但是 test2.test.com 显示该网站不安全。这让我相信只有第一个服务器/上游块按预期工作。并且第二个服务器/上游块被忽略了。

  2. 实际上是有道理的,因为服务器不应该监听到同一个端口的传入连接,并路由到不同的服务器。代理不知道如何处理其中的 1 个连接(我的解释不好)。

  3. 但这并不能解释为什么第二个服务器/上游块会彻底失败。即使 test2.example.com 是唯一配置的服务器/上游块。

感谢您的任何建议,感谢您的时间和考虑。这是我一直在努力理解和做出正面/反面的事情。

大犀牛

【问题讨论】:

    标签: nginx ssl proxy


    【解决方案1】:

    我认为您需要使用server_name 指令。因为您的网络服务器侦听两个子域的相同 ip 和相同端口。

    我猜这个配置文件应该可以正常工作:

    upstream test1.example.com {
        server flaskapp.example.com:5000
    }
    
    server {
        listen 443 ssl;
        server_name test1.example.com;
        proxy_pass test1.example.com;
        ssl_certificate /opt/certs/example1.com.crt;
        ssl_certificate_key /opt/example1.com.key;
        ssl_protocols TLSv1.2;
        ssl ciphers "ECDHE-ECDSA-AES128-GCM-SHA256"
    }
    
    upstream test2.example.com {
        server flaskapp.example.com:5000
    }
    
    server {
        listen 443 ssl;
        server_name test2.example.com;
        proxy_pass test2.test.com;
        ssl_certificate /opt/certs/test.com.crt;
        ssl_certificate_key /opt/test.com.key;
        ssl_protocols TLSv1.2;
        ssl ciphers "ECDHE-ECDSA-AES128-GCM-SHA256"
    }
    

    【讨论】:

      猜你喜欢
      • 2014-06-03
      • 1970-01-01
      • 1970-01-01
      • 2012-03-17
      • 2012-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多