502 Bad Gateway when using ExpressJS with nginx
Asked Answered
G

2

11

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

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

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:

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

and the following in nginx's 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!

Galloot answered 21/3, 2011 at 23:38 Comment(2)
What do you see in <code>error_log</code>?Keyte
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: "127.0.0.1:8080/socket.io/xhr-polling/1300799281533", host: "184.73.217.204", referrer: "184.73.217.204"Galloot
K
24

Try this patch...

    -proxy_pass http://node/;
    +proxy_pass http://node;
Keyte answered 22/3, 2011 at 13:19 Comment(4)
wiki.nginx.org/HttpProxyModule#proxy_pass -- here is documentation If it is necessary to transmit URI in the unprocessed form then directive proxy_pass should be used without URI part: location /some/path/ { proxy_pass _http://127.0.0.1; }Keyte
wow hours of debugging and it's a freaking slash that threw off my entire app. upvoted!Rollmop
Genius!!!! I just want to say thanks, this took me hours and hours and hours of debugging!Know
I tried this but it just doesn't seem to work. Any words of wisdom? #9256954Uhland
P
2

I am new to setting up a VMS using nginx and forever with 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.

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.

In the end, what seemed like equivalent ways of using forever to start node ended up with very different results.

What results in 502 Bad Gateway:

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

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

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

What works:

Two ways that did work were either starting forever from 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 sourceDir option:

$ 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:

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;
        }
}
Panslavism answered 18/11, 2014 at 4:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.