javascript 将 ExpressJS 与 nginx 一起使用时出现 502 错误网关

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5385036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 17:01:41  来源:igfitidea点击:

502 Bad Gateway when using ExpressJS with nginx

javascriptnginxwebsocketexpressnowjs-sockets

提问by Mark

If I run my expressjs app like so: coffee server.coffeeand navigate to localhost:8080, everything works just fine.

如果我像这样运行我的 expressjs 应用程序:coffee server.coffee并导航到localhost:8080,一切正常。

However, when I reverse proxy 8080 with nginx with the following configuration:

但是,当我使用以下配置使用 nginx 反向代理 8080 时:

server {
    listen 0.0.0.0:80;
    server_name localhost;
    access_log /var/log/nginx/nodetest.log;

    location / {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_set_header X-NginX-Proxy true;

      proxy_pass http://node/;
      proxy_redirect off;
    }
}

upstream node {
    server 127.0.0.1:8080;
}

I get the following error in the Chrome Dev console:

我在 Chrome Dev 控制台中收到以下错误:

GET http://184.73.217.204/socket.io/xhr-polling//1300750540040 502 (Bad Gateway)

GET http://184.73.217.204/socket.io/xhr-polling//1300750540040 502 (Bad Gateway)

and the following in nginx's error.log

以及 nginx 中的以下内容 error.log

2011/03/22 13:07:59 [error] 10269#0: *18 upstream prematurely closed connection while
reading response header from upstream, client: 168.229.58.68, server: localhost, 
request:     "GET /socket.io/xhr-polling//1300799281533 HTTP/1.1", upstream:     
"http://127.0.0.1:8080/socket.io/xhr-polling/1300799281533", host: "184.73.217.204",    
referrer: "http://184.73.217.204/"

Any guidance appreciated!

任何指导表示赞赏!

回答by CyberDem0n

Try this patch...

试试这个补丁...

    -proxy_pass http://node/;
    +proxy_pass http://node;

回答by darrendb

I am new to setting up a VMS using nginxand foreverwith nodejs/meanjs. My goal was to configure a proxy to be able to serve my app on the default port 80 rather than from port 3000.

我是新来使用建立VMSnginxforevernodejs/ meanjs。我的目标是配置一个代理,以便能够在默认端口 80 而不是端口 3000 上为我的应用程序提供服务。

While fumbling about with nginx server block snippets from a variety of different online examples I started to hit a wall with "502 Bad Gateway" errors. Through a lot of trial and error I was finally able to resolve this condition.

在从各种不同的在线示例中摸索 nginx 服务器块片段时,我开始遇到“502 Bad Gateway”错误。通过大量的反复试验,我终于能够解决这个问题。

In the end, what seemed like equivalent ways of using foreverto start nodeended up with very different results.

最终,看似相同的forever开始使用方式node最终得到了截然不同的结果。

What results in 502 Bad Gateway:

什么导致 502 Bad Gateway:

When issued from the server's root using an absolute path to the node script, this command failed with a 502:

当使用节点脚本的绝对路径从服务器的根发出时,此命令失败并显示 502:

$ cd /
$ sudo forever start --spinSleepTime 10000 /var/www/mydomain.com/server.js

Actually, for that matter if nodeis not running at all I was also getting a 502 error.

实际上,就此而言,如果node根本没有运行,我也会收到 502 错误。

What works:

什么工作:

Two ways that did work were either starting foreverfrom my project's root directory without specifying a path to the node script (just the filename) or again from the server's root but using the sourceDiroption:

两种有效的方法是forever从我的项目的根目录开始而不指定节点脚本的路径(只是文件名),或者再次从服务器的根目录开始但使用sourceDir选项:

$ cd /var/www/mydomain.com
$ sudo forever start --spinSleepTime 10000 server.js

Or:

或者:

$ cd /
$ sudo forever start --spinSleepTime 10000 --sourceDir /var/www/shotplot.info/ server.js

Just for completeness I will also include the nginx server block in case that helps provide some additional context:

为了完整起见,我还将包含 nginx 服务器块,以防有助于提供一些额外的上下文:

upstream nodejs \{
        server 127.0.0.1:3000;
}
server {
        root /var/www/mydomain.com;
        server_name mydomain.com www.mydomain.com;

        location / {
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $remote_addr;
                proxy_set_header Host $host;
                proxy_pass http://nodejs;
                proxy_redirect off;
        }
}