Question

GET http://localhost:5000/user net::ERR_CONNECTION_REFUSED when running application

Posted August 4, 2021 884 views
NginxNode.jsDigitalOceanReact

I’ve been following https://marketplace.digitalocean.com/apps/nodejs?getting-started tutorial to host my web application on the Droplet/IP I have.

I’ve been able to have the front end of my website successfully load however the backend with the HTTP requests are not working. I keep getting a 'GET http://localhost:5000/user net::ERR_CONNECTION_REFUSED' error. I’m using React for the frontend and Node for the backend.

This is the error I get from /var/log/nginx/error.log:

2021/08/04 03:03:58 [error] 20156#20156: *17 connect() failed (111: Connection refused) while connecting to upstream, client: 173.72.3.10, server: workoutlabnode, request: "GET /favicon.ico HTTP/1.1", upstream: "http://127.0.0.1:3000/favicon.ico", host: "161.35.100.44", referrer: "http://161.35.100.44/login"
2021/08/04 03:05:15 [error] 20156#20156: *65 connect() failed (111: Connection refused) while connecting to upstream, client: 173.72.3.10, server: workoutlabnode, request: "GET /login HTTP/1.1", upstream: "http://127.0.0.1:3000/login", host: "161.35.100.44"
2021/08/04 03:11:08 [error] 20156#20156: *301 connect() failed (111: Connection refused) while connecting to upstream, client: 173.72.3.10, server: workoutlabnode, request: "GET /login HTTP/1.1", upstream: "http://127.0.0.1:3000/login", host: "161.35.100.44", referrer: "http://161.35.100.44/login"
2021/08/04 03:13:11 [error] 20156#20156: *346 connect() failed (111: Connection refused) while connecting to upstream, client: 173.72.3.10, server: workoutlabnode, request: "GET /login HTTP/1.1", upstream: "http://127.0.0.1:3000/login", host: "161.35.100.44", referrer: "http://161.35.100.44/workout-tracker"
2021/08/04 03:48:15 [alert] 20156#20156: *457 open socket #16 left in connection 7
2021/08/04 03:48:15 [alert] 20156#20156: *458 open socket #15 left in connection 9
2021/08/04 03:48:15 [alert] 20156#20156: aborting

This is my netstat -plant output:

Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      22722/nginx: master 
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      624/systemd-resolve 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      975/sshd: /usr/sbin 
tcp        0      0 0.0.0.0:3000            0.0.0.0:*               LISTEN      22644/node          
tcp        0      0 161.35.100.44:80        173.72.3.10:59033       ESTABLISHED 22723/nginx: worker 
tcp        0      0 127.0.0.1:35252         127.0.0.1:3000          ESTABLISHED 22723/nginx: worker 
tcp        0      0 127.0.0.1:3000          127.0.0.1:35252         ESTABLISHED 22644/node          
tcp        0    340 161.35.100.44:22        173.72.3.10:55528       ESTABLISHED 16951/sshd: root@pt 
tcp6       0      0 :::5000                 :::*                    LISTEN      22688/node /root/wo 
tcp6       0      0 :::80                   :::*                    LISTEN      22722/nginx: master 
tcp6       0      0 :::22                   :::*                    LISTEN      975/sshd: /usr/sbin

This is part of my /etc/nginx/sites-available/default file:

location / {
                proxy_http_version 1.1;
                proxy_cache_bypass $http_upgrade;

                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;

                proxy_pass http://localhost:3000;
        }

        location /server {
                proxy_http_version 1.1;
                proxy_cache_bypass $http_upgrade;

                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;

                proxy_pass http://localhost:5000;
}

The frontend react app is supposed to be running on port 3000 and the backend server is running on port 5000.

Any help on how to solve this issue would be greatly appreciated!

edited by bobbyiliev

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.

×
Submit an Answer
1 answer

Hi there,

Your server configuration looks good!

The only change that you need to make is for React app to connect to http://your_ip/server rather than localhost:5000.

That way the Nginx proxy will forward the traffic from the /server endpoint to the service that is running on port 5000.

Let me know how it goes!
Regards,
Bobby

  • When I change the React app to make a axios.get request to http://161.35.100.44/server/user/ I get a http://161.35.100.44/server/user/ 404 (Not Found) error in my console log.

    • Hello,

      I could suggest checking your express server access logs to see how exactly the request is being handled. At the moment if you visit https://www.workoutlab.live/server/user you get 404 as well, which could mean that either the /user endpoint is not present or that the Node app is receiving /server/user as the request and fails to match it with your /user route.

      • Hi,

        Thank you for your reply.

        I checked the pm2 logs for the server and there is this error:

        Error: listen EADDRINUSE: address already in use 0.0.0.0:80
            at Server.setupListenHandle [as _listen2] (net.js:1316:16)
            at listenInCluster (net.js:1364:12)
            at doListen (net.js:1501:7)
            at processTicksAndRejections (internal/process/task_queues.js:85:21) {
          code: 'EADDRINUSE',
          errno: 'EADDRINUSE',
          syscall: 'listen',
          address: '0.0.0.0',
          port: 80
        }
        

        I checked what is running on port 80 and it is

        COMMAND    PID     USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
        nginx   107260     root    6u  IPv4 1341840      0t0  TCP *:http (LISTEN)
        nginx   107261 www-data    6u  IPv4 1341840      0t0  TCP *:http (LISTEN)
        

        If I kill these two processes then the frontend of the website doesn’t work anymore so not sure what to do about this. I’m running a pm2 instance for the react app and one for the server. The nginx sites-enabled/default is configured so that http is redirected to https:

        server {
                listen 80;
                server_name www.workoutlab.live;
                rewrite ^/(.*) https://www.workoutlab.live/$1 permanent;
        }
        
        server {
                # SSL configuration
                #
                listen 443 ssl default_server;
        

        Do you have any ideas on how to fix this?

        • Hi there,

          It looks like that your Node service is also trying to listen on port 80 and it gets in a conflict with Nginx, as Nginx is already listening on that port.

          Keep in mind that you can not have multiple services listening on the same port.

          What you need to do in order to fix this is to change the port for your Node service from port 80 to another port like 5000 so that it matches your Nginx config.

          Let me know how it goes!
          Regards,
          Bobby