【问题标题】:Yet another 502 error with nginxnginx的另一个502错误
【发布时间】:2014-09-07 05:56:37
【问题描述】:

我正在尝试使用一些基本服务来设置 server@home。所有服务都运行在专用 VM 中。每个 VM 都托管在 vSphere 5.5 上。到目前为止我有:

  • Debian wheezy 使用 nginx 作为反向代理:192.168.1.12
  • Debian wheezy 使用 nodeJS 作为 webapp 服务器:192.168.1.43
    • 192.168.1.43:3000 => 在 192.168.1.43:3001 上进行重定向的 http 网络服务器
    • 192.168.1.43:3001 => 提供服务的 https 网络服务器
  • 安装了 madsonic 的 Debian wheezy:192.168.1.35
    • 如文档中所述,我将 --https-port=443 放在配置中以启用 https 访问

我使用 nginx 能够拥有这样的东西:

  • myapp.mydomaine.com => 转到 nodejs @ 192.168.1.43
  • music.mydomain.com => 转到 madsonic @ 192.168.1.35

我按照教程编辑了 /etc/nginx/sites-enabled 中的“默认”文件。这是它的样子:

server {
 listen 80;
 server_name myapp.domaine.com;
 location / {
   proxy_pass http://192.168.1.43:3000;
 }
}
server {
 listen 443;
 server_name myapp.domain.com;
 ssl on;
 ssl_certificate [...];
 ssl_certificate_key [...];
 location / {
   proxy_pass https://192.168.1.43:3001;
 }
}
server {
 listen 80;
 server_name music.domain.com;
 location / {
   proxy_pass http://192.168.1.35:4040;
 }
}
server {
 listen 443;
 server_name music.domain.com;
 ssl on;
 ssl_certificate [...];
 ssl_certificate_key [...];
 location / {
    proxy_pass https://192.168.1.35;
 }
}

myapp 上的第一个重定向有效。当我在 madsonic 服务器上只有 http 时,音乐重定向有效。当我在 madsonic 服务器上激活 https 时,我收到 502 Bad gateway 错误(但 Firefox 中的 URL 是 https://music.domain.com)。

我还尝试了其他一些方法,比如这里提到的: How to redirect on the same port from http to https with nginx reverse proxy

也没有用。

我还在 /var/logs/nginx/error.log 中看到 502 错误是由于 SSL_do_handshake 错误 (SSl23_GET_SERVER_HELLO:tlsv1)。不知道是否与 502 错误有关。

我有点困惑,因为其他 https 服务工作正常。有人有建议吗?非常感谢。

【问题讨论】:

    标签: nginx reverse-proxy


    【解决方案1】:

    以下是 serverfault.com 上针对同一问题发布的用户“desasteralex”的答案。它起作用了,所以我在这里分享他的答案(顺便说一句,谢谢他:D)。


    首先,Nginx 是你的 SSL 终结者。这意味着您不需要同时在 HTTP 和 HTTPS 模式下运行您的应用程序。 HTTP 就足够了。

    因此,对于您的应用,配置可能如下所示:

    server {  listen 192.168.1.12:80;  server_name myapp.domain.com;  location / {   rewrite ^ https://$server_name$request_uri? permanent;  } }
    

    上面的指令会将所有 HTTP 请求重定向到 HTTPS。

    server {  listen 192.168.1.12:443;  server_name myapp.domain.com;  ssl on;  ssl_certificate [...];  ssl_certificate_key [...];  location / {  proxy_pass https://192.168.1.43:3000;  } }
    

    我在这里的 proxy_pass 中选择了端口 3000 来指向您应用的 HTTP 版本。您需要关闭将应用重定向到端口 3001。

    关于您的 music.domain.com 重定向 - 对于 HTTP,您在 proxy_pass 参数中使用端口 4040,而在 HTTPS 中您不使用。我假设 madsonic 服务器只侦听端口 4040,所以配置可能如下所示:

    server {  listen 192.168.1.12:80;  server_name music.domain.com;  location / {   rewrite ^ https://$server_name$request_uri? permanent;  } } 
    
    server {  listen 192.168.1.12:443;  server_name music.domain.com;  ssl on;  ssl_certificate [...];  ssl_certificate_key [...];  location / {  proxy_pass https://192.168.1.35:4040;  } }
    

    希望这会有所帮助。


    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-14
      • 2011-05-14
      • 2019-06-05
      • 2015-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多