Question

Nginx 502 Bad Gateway and Node.JS app

  • Posted March 18, 2014

Folks,

I’m trying to install a Node.JS application in a subfolder of a WordPress site running on Nginx, Varnish and php-fpm.

Any attempt to access the site gives a 502 Bad Gateway browser error and the following message in Nginx’s error log.

<pre> [error] 20247#0: *143 connect() failed (111: Connection refused) while connecting to upstream, client: 127.0.0.1, server: example.com, request: “GET /forum HTTP/1.1”, upstream: “http://127.0.0.1:4567/forum”, host: “example.com” </pre>

I’ve researched the cause of such error messages, but have yet to come up with a solution. It it helps, here’s my Nginx config:

<pre> server {

server_name example.com www.example.com;
  
root /home/public_html/;
listen 8080;
 
index index.html index.htm index.php;

include conf.d/drop;

    location / {
            
        try_files $uri $uri/ /index.php?q=$uri&$args;
    }

    location ~ \.php$ {
        fastcgi_buffers 8 256k;
        fastcgi_buffer_size 128k;
        fastcgi_intercept_errors on;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass unix:/var/run/php5-fpm.sock;


    }

    location ~* \.(css|js|png|jpg|jpeg|gif|ico)$ {
            expires 1d;
    }

Settings for proxying NodeBB forum installation. Added March 14 2014.

    location ^~ /forum {

    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://127.0.0.1:4567/forum;
    proxy_redirect off;

    # Socket.IO Support
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";

}

} </pre>

Subscribe
Share

Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

I was having this same issue with running multiple apps/websites on one server along with node.js app running on port 3000.

It turned out to be SELinux permission issue and solved by running this command: sudo semanage port --add --type http_port_t --proto tcp 3000

Hope this helps anyone having the same problem.

I was having this same issue with running multiple apps/websites on one server along with node.js app running on port 3000.

It turned out to be SELinux permission issue and solved by running this command: sudo semanage port --add --type http_port_t --proto tcp 3000

Hope this helps anyone having the same problem.

I was having this same issue with running multiple apps/websites on one server along with node.js app running on port 3000.

It turned out to be SELinux permission issue and solved by running this command: sudo semanage port --add --type http_port_t --proto tcp 3000

Hope this helps anyone having the same problem.

As you can see from the config, the Node.JS app is running on port 4567, which is different from the Nginx port, running on a www port. <br> <br>The Node.JS application is started using Forever - “forever start app.js”. <br> <br>The thing is, without Nginx, I can access the app using the IP/URL of the main site and the Node apps port, like this: http://example.com:4567. <br> <br>But not via Nginx.

With node/nginx proxy and ajax requests get 502:

I fixed this issue by simply stopping and starting pm2 and ensuring only 1 pm2 process was running.

I had similar issue and spent a some time on it fugiuring out why it happened and the thing was, that I simply forgot to do npm install, because one of the core dependencies wasn’t installed on the server. What a shame…

You need to be running a node program that is a server on port 4567.

This can be because of build architecture, so please make sure to provide the correct build architecture when building for your deployment server, for instance for meteor: "meteor build --architecture=os.linux.x86_64 "

Having the same problem on my server. I got it working fine for one node.js application but adding a second application (with a different domain) to the VPS is giving me 502 Bad Gateway error and a similar error as above in my nginx error log. <br> <br>I am using port 3000 for the first app and am trying to get the second running on 4000 but just get connection refused. Could it be something to do with the port being blocked? How can I open the port?

Also getting this upstream error.