finid
By:
finid

Nginx 502 Bad Gateway and Node.JS app

March 18, 2014 55.3k views
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.
[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"
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:
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";

    }

}
13 Answers
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.

The Node.JS application is started using Forever - "forever start app.js".

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.

But not via Nginx.

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 httpportt --proto tcp 3000

Hope this helps anyone having the same problem.

  • Command didnt work throwing some value error
    [ec2-user@ip-10-205-139-18 ~]$ sudo semanage port --add --type httpportt --proto tcp 3000
    ValueError: Type httpportt is invalid, must be a port type

you need run the node proyect:

/home/test/nodejs/
>node server.js

Remember: nodejs need run in diferent port at nginx.
How to node: http://nodejs.org/
Is your application serving 127.0.0.1:4567/forum or JUST 127.0.0.1:4567?

The way I see it, you're probably serving it on 127.0.0.1:4567 and want to pass it to nginx on example.com/forum

Removing the /forum from 127.0.0.1:4567/forum might help.
I have actually tried that, but still get the same result. Matter of fact, I have also tried accessing the application via a subdomain, after making the necessary changes, but still getting just about the same error, which is,


014/03/18 23:19:26 [error] 9921#0: *148 connect() failed (111: Connection refused) while connecting to upstream, client: 127.0.0.1, server: forum.example.com, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:4567/", host: "forum.example.com"


You can see from the above error that the app is being served at http://127.0.0.1:4567

The only difference between mine and other setups I've come across is that Nginx is sitting behind Varnish (Varnish[port 80] > Nginx[port 8080]. But I don't see that as an issue because the main site is working well.
Posting this configs hoping that somebody else will spot something that I'm missing:

This is the Nginx config from forum.example.com under sites-available directory, which is properly linked to sites-enabled directory


#
server {
server_name forum.example.com www.forum.example.com;

listen 8080;

## Settings for proxying Node.JS forum installation.

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

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

}

}



And below are the settings from the apps config file:


{
"base_url": "http://127.0.0.1",
"port": "4567",
"use_port": true,
"secret": "secret",
"bind_address": "0.0.0.0",
"database": "redis",
"redis": {
"host": "127.0.0.1",
"port": "6379",
"password": "secret2",
"database": "0"
},
"bcrypt_rounds": 12,
"upload_path": "/public/uploads",
"relative_path": ""
}


Now if I attempt to access the app from forum.example.com, I get:


[error] 9921#0: *148 connect() failed (111: Connection refused) while connecting to upstream, client: 127.0.0.1, server: forum.example.com, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:4567/", host: "forum.example.com"


which is just the same error I get if I try to access it from a subfolder.

Any clues?
And on the browser, the error is still "502 Bad Gateway."

Btw, I running Nginx 1.4.6
Just did an nmap scan of the server and it shows that port 4567, the Node.JS app port, is not open on the server. I think that explains why the connection is being refused.

The server is not listening on that port, even though I started it and can confirm that it is started.

Now the bit question. Why is the app not listening on that port even after it is started? Mystery. Who's gonna help crack this?

Will a simple restart of the VPS solve this?
"502 Bad Gateway" means that NodeJS is not running. Make sure your NodeJS app is running.
Also getting this upstream error.
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.

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?

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 "

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

Have another answer? Share your knowledge.