nginx 502 bad gateway in ubuntu

4.9k views Asked by At

Ubuntu and nginx in my server. I have multiple subdomain for this server, which port 3000 it is alraedy be use by other website. Whole of this node app will be serve from pm2.

I guess cause the 502 gateway error, is the port is not be serve from my server.

// /etc/nginx/sites-available/projectB

server {
    root /var/www/projectB;
    server_name projectB.domain.com www.projectB.domain.com;

    location / {
      proxy_pass http://localhost:3001; // this will be fine if it is localhost:3000;
      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;
    }
}

This is my backend service port. It's not exist port 3001 inside there. I'll porvide the nginx error.log at below too.

// sudo netstat -plant

Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      494/systemd-resolve 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      787/sshd: /usr/sbin 
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      80730/nginx: master 
tcp        0      0 127.0.0.1:27017         0.0.0.0:*               LISTEN      32831/mongod        
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      80730/nginx: master 
tcp        0      0 127.0.0.1:27017         127.0.0.1:39994         ESTABLISHED 32831/mongod        
tcp        0      0 127.0.0.1:39994         127.0.0.1:27017         ESTABLISHED 81007/node          
tcp        0    364 139.59.107.31:22        124.13.195.75:50855     ESTABLISHED 82341/sshd: root@pt 
tcp        0      0 127.0.0.1:27017         127.0.0.1:39984         ESTABLISHED 32831/mongod        
tcp        0      0 127.0.0.1:27017         127.0.0.1:39986         ESTABLISHED 32831/mongod        
tcp        0      0 127.0.0.1:39986         127.0.0.1:27017         ESTABLISHED 81007/node          
tcp        0      0 127.0.0.1:39984         127.0.0.1:27017         ESTABLISHED 81007/node          
tcp6       0      0 :::22                   :::*                    LISTEN      787/sshd: /usr/sbin 
tcp6       0      0 :::3000                 :::*                    LISTEN      81007/node          
tcp6       0      0 :::443                  :::*                    LISTEN      80730/nginx: master 
tcp6       0      0 :::80                   :::*                    LISTEN      80730/nginx: master 

nginx/error.log

[error] 80731#80731: *434 connect() failed (111: Connection refused) while connecting to upstream, client: clientPort, server: projectB.domain.com, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:3001/", host: "projectB.domain.com"
1

There are 1 answers

4
JorgenPhi On

It looks like your application itself isn't listening on port 3001, which is where nginx is trying to forward your requests to.

Check your application is configured to listen on that port and that it is running. You can check if it's working by trying to connect without nginx in the way, by running this command on your server.

curl http://localhost:3001/

If you get a response, then your application is working. See if Nginx is able to serve the page. It looks like it should based on your config.