还有另一个与nginx有关的502错误

时间:2014-07-16 15:53:55

标签: nginx reverse-proxy

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

  • Debian wheezy与nginx用作反向代理:192.168.1.12
  • 使用nodeJS作为webapp服务器的Debian wheezy:192.168.1.43
    • 192.168.1.43:3000 =>在192.168.1.43:3001
    • 上进行重定向的http Web服务器
    • 192.168.1.43:3001 => https提供服务的Web服务器
  • 安装了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服务工作正常。有人有建议吗?非常感谢。

1 个答案:

答案 0 :(得分:1)

以下是用户" desasteralex"的回答。这是在serverfault.com上发布的相同问题。它起作用,所以我在这里分享他的答案(并且对他来说很重要: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;  } }

希望这有帮助。


相关问题