Nginx: connect() failed (111: Connection refused) while connecting to upstream

August 4, 2019 372 views
DigitalOcean Nginx DNS Ubuntu 18.04

Hi,

I’m trying to use Nginx to forward all requests to my domain (sofy.site) to a Node app running on my droplet. I also want to use SSL because the app can only work over https due to some limiting factors.

I’ve been on this task for the past 3-4 hours and I have no idea what I’m doing wrong. If I follow some question somewhere it fixes that issue but brings up another issue. Any help would be appreciated.

Here is my server configuration:
NOTE: I am not using the Nginx config file but instead have a file named ‘node’ in /etc/nginx/sites-available/ which I have linked with a file with the same name under /etc/nginx/sites-enabled/.

upstream sofy {
    server 127.0.0.1:3000;
}

server {
     listen 80;
     listen 433 ssl;

     server_name sofy.site;
     return 302 $scheme://www.sofy.site/$request_uri;
}

server {
    listen 80;
    listen 443 ssl; # managed by Certbot

    server_name www.sofy.site;  

    ssl_certificate /etc/letsencrypt/live/sofy.site/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/sofy.site/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

    # pass the request to the node.js server with the correct headers
    # and much more can be added, see nginx config options

    location / {
      proxy_set_header X-Forwarded-Proto https;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect off;
      proxy_pass http://sofy;
      proxy_ssl_server_name on;
    }
}

When I try to open the page https://sofy.site it shows the error '502 Bad gateway’ with another text saying 'nginx/1.14.0 (Ubuntu)’

This is what /var/log/nginx/error.log looks like:

2019/08/04 15:54:35 [error] 27684#27684: *28 connect() failed (111: Connection refused) while connecting to upstream, client: 183.87.41.80, server: sofy.site, request: "GET /favicon.ico HTTP/1.1", upstream: "https://127.0.0.1:3000/favicon.ico", host: "www.sofy.site", referrer: "https://www.sofy.site/"
2019/08/04 15:55:04 [error] 27684#27684: *28 connect() failed (111: Connection refused) while connecting to upstream, client: 183.87.41.80, server: sofy.site, request: "GET / HTTP/1.1", upstream: "https://127.0.0.1:3000/", host: "www.sofy.site"
2019/08/04 15:55:05 [error] 27684#27684: *28 connect() failed (111: Connection refused) while connecting to upstream, client: 183.87.41.80, server: sofy.site, request: "GET /favicon.ico HTTP/1.1", upstream: "https://127.0.0.1:3000/favicon.ico", host: "www.sofy.site", referrer: "https://www.sofy.site/"
2019/08/04 15:56:22 [error] 27684#27684: *35 connect() failed (111: Connection refused) while connecting to upstream, client: 183.87.41.80, server: sofy.site, request: "GET / HTTP/1.1", upstream: "https://127.0.0.1:3000/", host: "www.sofy.site"
2019/08/04 15:56:22 [error] 27684#27684: *35 connect() failed (111: Connection refused) while connecting to upstream, client: 183.87.41.80, server: sofy.site, request: "GET /favicon.ico HTTP/1.1", upstream: "https://127.0.0.1:3000/favicon.ico", host: "www.sofy.site", referrer: "https://www.sofy.site/"
2019/08/04 15:56:22 [error] 27684#27684: *35 connect() failed (111: Connection refused) while connecting to upstream, client: 183.87.41.80, server: sofy.site, request: "GET / HTTP/1.1", upstream: "https://127.0.0.1:3000/", host: "www.sofy.site"
2019/08/04 15:56:23 [error] 27684#27684: *35 connect() failed (111: Connection refused) while connecting to upstream, client: 183.87.41.80, server: sofy.site, request: "GET /favicon.ico HTTP/1.1", upstream: "https://127.0.0.1:3000/favicon.ico", host: "www.sofy.site", referrer: "https://www.sofy.site/"

Any help would be appreciated, thank you

1 Answer

I was able to fix it using this as the config file ‘node":

upstream sofy {
    server 127.0.0.1:3000
}

server {
    listen 80;
    listen 443 ssl; # managed by Certbot

    server_name sofy.site www.sofy.site;  

    # pass the request to the node.js server with the correct headers
    # and much more can be added, see nginx config options

    location / {
     proxy_pass http://sofy;
     proxy_ssl_session_reuse off;
    }

    ssl_certificate /etc/letsencrypt/live/sofy.site/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/sofy.site/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}


edited by MattIPv4
Have another answer? Share your knowledge.