NGINX "502 bad gateway request" when making a 'GET' request

April 1, 2018 905 views
Nginx Node.js Ubuntu

Hello,

I'm running nodejs app on a droplet and everything works fine, but when I try to make a GET request to my server I end up with a 502 error. The get is pointed to www.example.com/down. Everything worked fine, when I run the server locally with npm start.

This is the error log of nginx:

2018/04/01 20:17:02 [error] 22119#22119: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 95.49.110.41, server: saveinsta.me, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:5000/", host: "saveinsta.me"

And my nginx config:

server_name saveinsta.me www.saveinsta.me;

        location / {
                proxy_pass http://localhost:5000;
                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;

        }

I tried the advice on another thread to change "localhost" to "127.0.0.1 ", but nothing has changed. It is possible that something is wrong with my node app code, so I'll link it here: (https://github.com/bikowalczyk/saveinsta.me/blob/gif_server/index.js).

Any help would be greatly appreciated.

Thank you!

1 Answer

You may want to update your node.js app to specify the IP address to listen on. This may resolve the issue but will also ensure that your node.js app is not listening publicly and only to the local requests from nginx.

app.listen(5000,'127.0.0.1',function(){
Have another answer? Share your knowledge.