简体   繁体   English

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

[英]502 Bad Gateway error for my server running with Node JS on nginx proxy

I am getting 502 bad gateway error: when I check the nginx error log I find this:我收到 502 bad gateway 错误:当我检查 nginx 错误日志时,我发现:

2017/05/06 02:36:04 [error] 48176#0: *135 connect() failed (111: Connection refused) while connecting to upstream, client: 10.163.XX.X, server: abc-def-ghi, request: "GET /favicon.ico HTTP/1.1", upstream: " https://127.0.0.1:5300/favicon.ico ", host: "hostnname", referrer: "hostname-1 2017/05/06 02:36:04 [错误] 48176#0:*135 连接()失败(111:连接被拒绝),同时连接到上游,客户端:10.163.XX.X,服务器:abc-def-ghi,请求:“GET /favicon.ico HTTP/1.1”,上游:“ https://127.0.0.1:5300/favicon.ico ”,主机:“hostnname”,引用者:“hostname-1”

I searched internet enough but could not find anything.我在互联网上搜索了足够多但找不到任何东西。 One thing to note here is that, this intermittent error is coming only on a particular page.这里要注意的一件事是,此间歇性错误仅出现在特定页面上。

Could this be a code issue?这可能是代码问题吗? or nginx configuration issue> Can anyone please help me here.或 nginx 配置问题> 任何人都可以在这里帮助我。

Some of my nginx conf:我的一些 nginx 配置:

  upstream node_api_server {
    server localhost:5300 fail_timeout=0;
  }

location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header Host $http_host;
    proxy_set_header X-NginX-Proxy true;
    proxy_read_timeout 5m;
    proxy_connect_timeout 5m;
    proxy_pass_header Set-Cookie;

    proxy_pass https://node_api_server;
    proxy_redirect off;
    proxy_buffer_size   128k;
    proxy_buffers   4 256k;
    proxy_busy_buffers_size   256k;
    break;
}

502 errors are generally caused by NGINX being unable to pass a request to "upstream", in this case your Node.js server (which is also what the error message suggests: "Connection refused" "). 502 错误通常是由 NGINX 无法将请求传递到“上游”引起的,在这种情况下是您的 Node.js 服务器(这也是错误消息所暗示的: “连接被拒绝” “)。

It may be crashing and restarting, so check its logfiles to see what's causing the crashes.它可能会崩溃并重新启动,因此请检查其日志文件以查看导致崩溃的原因。

I'm a little bit of a noob to this stuff, but I spent hours trying to debug my issue.我对这些东西有点菜鸟,但我花了几个小时试图调试我的问题。 It ended up being that my global variable for the port in my API wasn't being assigned correctly so node wasn't listening to the right port.结果是我的 API 中端口的全局变量没有被正确分配,所以节点没有监听正确的端口。 Due to my infrequent use of a lot of this stuff, I wrote down how I should trouble-shoot it in the future and figured I would share.由于我很少使用这些东西,我写下了我将来应该如何解决它,并想我会分享。 Again, this is for my API, so you may be slightly different (I was using port 3006).同样,这是针对我的 API,所以您可能略有不同(我使用的是端口 3006)。

  • In any console, double check you are getting an error by running wget https://example.com/api/v1/ping or if you are ssh'd into the production console you can do wget 127.0.0.1:3006/api/v1/ping在任何控制台中,通过运行wget https://example.com/api/v1/ping仔细检查您是否收到错误,或者如果您通过 ssh 进入生产控制台,您可以执行wget 127.0.0.1:3006/api/v1/ping
  • Make sure that node is running properly.确保节点正常运行。 After starting node in pm2 using npm run build then npm run start , make sure pm2 is running correctly with pm2 status在使用npm run build然后npm run start在 pm2 中启动节点后,确保 pm2 在pm2 status正确运行
  • Check the error logs for pm2 with pm2 logs .使用pm2 logs检查 pm2 的错误pm2 logs This should show the normal output from console.log messages in your API.这应该显示 API 中console.log消息的正常输出。 Does it say it is running on the correct port?它是否说它在正确的端口上运行? You can have your api log your port at startup to confirm it is on the correct port.您可以让您的 api 在启动时记录您的端口,以确认它在正确的端口上。 Check other errors as well.也检查其他错误。 You can clear all logs in pm2 using pm2 flush您可以使用pm2 flush pm2 中的所有日志
  • Now make sure that your server is listening to incoming traffic on the port that you specified sudo netstat -plunt will give you a list of ports that are open and the programs that are using them.现在确保您的服务器正在侦听您指定的端口上的传入流量sudo netstat -plunt将为您提供打开的端口列表以及正在使用它们的程序。
  • You can get more info on the node apps using sudo ps aux | grep node您可以使用sudo ps aux | grep node获取有关节点应用程序的更多信息。 sudo ps aux | grep node to get all node programs listening to ports and it will show the full path for the node file path. sudo ps aux | grep node让所有 node 程序监听端口,它会显示 node 文件路径的完整路径。 You can match the PID in the previous command to make sure your ports are matching.您可以匹配上一个命令中的 PID 以确保您的端口匹配。
  • Check the status of nginx to make sure it is running: sudo systemctl nginx status or replace status with start to get it started检查 nginx 的状态以确保它正在运行: sudo systemctl nginx status或用 start 替换 status 以启动它
  • Now make sure that your nginx ports are using that incoming port.现在确保您的 nginx 端口正在使用该传入端口。 Check the nginx error log with sudo vim /var/log/nginx/error.log to confirm your port and server name are correct.使用sudo vim /var/log/nginx/error.log检查 nginx 错误日志以确认您的端口和服务器名称正确。 Every time you send your server a command it will record the 502 error here with an error message: 2021/02/20 03:05:28 [error] 26646#26646: *644 connect() failed (111: Connection refused) while connecting to upstream, client: XXX.XXX.XXX.XX, server:example.com, request: "GET /api/v1/ping HTTP/1.1", upstream: "http://127.0.0.1:3006/api/v1/ping", host: "example.com"每次您向服务器发送命令时,它都会在此处记录 502 错误并带有错误消息: 2021/02/20 03:05:28 [error] 26646#26646: *644 connect() failed (111: Connection refused) while connecting to upstream, client: XXX.XXX.XXX.XX, server:example.com, request: "GET /api/v1/ping HTTP/1.1", upstream: "http://127.0.0.1:3006/api/v1/ping", host: "example.com"
  • You can check the status of your nginx config files to make sure you don't have any errors in the config by doing sudo nginx -t您可以通过执行sudo nginx -t检查 nginx 配置文件的状态以确保配置中没有任何错误
  • Check the Nginx config file in /etc/nginx/nginx.config .检查/etc/nginx/nginx.config的 Nginx 配置文件。 This file probably includes the sites-enabled/default config file.该文件可能包含sites-enabled/default配置文件。 Mind as well check the sites-available/default config file as well to make sure all ports and server names are correct and making sure there are no duplicates (most likely would show up in the error logs).还要注意检查sites-available/default配置文件,以确保所有端口和服务器名称都是正确的,并确保没有重复项(很可能会出现在错误日志中)。
  • For any changes that are made to the config files, you need to restart the nginx system using sudo service nginx restart对于对配置文件所做的任何更改,您需要使用sudo service nginx restart nginx 系统

Hopefully that helps some of you!希望对你们中的一些人有所帮助! :) Happy Coding :) 快乐编码

We got a 502 due to a path not having the correct case in a require call, in this case the file name.由于路径在 require 调用中没有正确的大小写,我们得到了 502,在这种情况下是文件名。 The code executed locally (in VS Code) but not when deployed.代码在本地(在 VS Code 中)执行,但在部署时不执行。

const repoName = require('../data/reponame'); const repoName = require('../data/reponame'); //This should have been repoName //这应该是repoName

In my case, I received the same error, but when I check my logs of ingress-nginx.就我而言,我收到了同样的错误,但是当我检查 ingress-nginx 的日志时。 I found this我找到了这个

upstream sent too big header while reading response header from upstream,
 client: xxx.xxx.xxx.xx, server: sample-site.domain, request:

to overcome this issue I simply increased the size of --max-http-header-size of the node.为了克服这个问题,我只是增加了节点的--max-http-header-size and I was able to fix my issue.我能够解决我的问题。

简单的技巧:杀死您的 nginx 进程并重新启动您的 UI

确保您正在运行npm start或运行应用程序的脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM