Question

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

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!


Submit an answer

This textbox defaults to using Markdown to format your answer.

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

Sign In or Sign Up to Answer

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.

Accepted 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

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.