连接到通过 Nginx 运行 express 的 Nodejs 应用程序时出现 502 Bad Gateway

烘干

我在连接到在端口 8081 上运行的节点应用程序时遇到问题。
我的设置如下(一切都在 Raspberry Pi 上运行):

NGINX

events {
  worker_connections  1024;
}

http {
server {
  root /data/web;

  location / {
  }

  location /pub {
    proxy_pass http://localhost:8081;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
  }
}
}

我在第一个位置提供静态文件(这似乎工作正常),我希望第二个位置重新路由到我的节点应用程序。它在端口 8081 上运行。

我的节点应用程序如下所示:

app.get('/', function(req, res){
  res.send("Hello World!");
});
var server = app.listen(8081, '192.168.0.178');

我正在使用局域网中另一台电脑的简单 wget 测试我的连接:

wget http://192.168.0.178/pub

我得到的完整错误是这样的:

http://192.168.0.178/pub
Connecting to 192.168.0.178:80... connected.
HTTP request sent, awaiting response... 502 Bad Gateway
2018-01-14 15:42:27 ERROR 502: Bad Gateway.

解决方案
接受的答案确实是我遇到的问题。
我添加的另一件事是在我的 /pub 位置重写,因为 '/pub' 需要从转到 Node 应用程序的 url 中切断。所以最终的 nginx conf 看起来像这样:

http {
access_log /data/access_log.log;
error_log /data/error_log.log debug;

upstream backend {
  server localhost:8081;
}

server {
  root /data/web;

  location / {
  }

  location /pub {
    proxy_pass http://localhost:8081;
    rewrite /pub(.*) /$1; break;
  }
}
}
卢卡 T。

问题似乎与您暴露 nodejs 应用程序的网络接口有关。您已将应用程序设置为8081使用 ip侦听接口上的端口192.168.0.178,但根据说明,nginx 正在通过环回接口进行代理

proxy_pass http://localhost:8081;

您可以解决此问题,将 nodejs 应用程序暴露在环回接口上:

var server = app.listen(8081, 'localhost');

除了应用程序正在运行的机器之外,节点应用程序不应再从任何其他机器的端口 8081 上直接访问

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在Nginx之后运行Express应用程序的间歇性502 Bad Gateway错误

当 Jenkins 管道为 React 应用程序运行 docker 时,Nginx 502 Bad Gateway

Azure 应用程序网关连接到 Azure 容器实例的 502 Bad Gateway

Nginx / Flask / Python应用程序,Nginx抛出502 Bad Gateway错误

谁导致此“ 502 Bad Gateway nginx”出现?

Nginx和Daphne提供502 Bad Gateway

Nginx在apache上提供了502 Bad Gateway

我的服务器在nginx代理上与Node JS一起运行时出现502 Bad Gateway错误

将Spring Boot应用程序部署到Elastic Beanstalk时出现502 Bad Gateway

在AWS上通过HTTPS运行Express应用程序时出现问题

Gunicorn/Django/Nginx - 上传超过 100 MB 的文件时出现 502 Bad Gateway 错误

在Elastic beantalk中访问api服务器时出现Nginx 502(Bad Gateway)错误

还原服务器后出现502 Bad Gateway Nginx错误

如何修复 nginx 中的 502 Bad Gateway 错误?

Nginx上的负载均衡器提供502 Bad Gateway

Nginx:泊坞窗内的502 Bad Gateway

502 Bad Gateway-Rails App,Puma,Capistrano,Nginx

502 Bad Gateway / rails 4 / nginx / passenger 4.0.49 /

重新启动后Nginx 502 Bad Gateway

调用 api 时 Nginx 502 Bad Gateway 错误

nginx 后面有 pgadmin4 的 502 Bad Gateway?

通过PowerShell将ASP.NET Core应用程序发布到Azure并在Azure中获得502(Bad Gateway)

502:通过Nginx代理Ubuntu 18.04的Nodejs应用

在PUMA和Nginx上运行的Rails应用程序每隔几个小时就会因Bad Gateway死亡

nginx uwsgi websockets 502 Bad Gateway上游过早关闭连接,同时从上游读取响应头

通过IdentityServer4进行身份验证后,NGINX背后的.Net Core返回502 Bad Gateway

在 NGINX 和 Plumber API 之间添加负载平衡层 dockercloud/haproxy 后出现 502 Bad Gateway

UWSGI socket 更改组并拒绝连接,导致 502 bad gateway

无法在 Google App Engine 上部署 Flask 应用程序,收到错误“502 Bad Gateway”