Question

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

Posted September 21, 2020 344 views
DigitalOceanAPILet's EncryptUbuntu 18.04

I saw a similar topic to this one with the same title, but i rekon the issue is a lot different, and i thought it would be better to make a separate question rather than filling up responses from that one. So let’s go:

Full error: 
[error] 1209#1209: *2283 connect() failed (111: Connection refused) while connecting to upstream, client: 178.222.123.190, server: app.multiunitpros.com, request: "GET /api/ HTTP/1.1", upstream:http://127.0.0.1:1337/", host: "app.multiunitpros.com"

After running sudo nginx -t i get this set of messages

nginx: [warn] conflicting server name “app.multiunitpros.com” on 0.0.0.0:80, ignored

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

nginx: configuration file /etc/nginx/nginx.conf test is successful

Accessing this domain with /api gives 502 bad gateway

Error log provides this

[error] 1209#1209: *2283 connect() failed (111: Connection refused) while connecting to upstream, client: 178.222.123.190, server: app.multiunitpros.com, request: “GET /api/ HTTP/1.1”, upstream: “http://127.0.0.1:1337/”, host: “app.multiunitpros.com”

Here is my /etc/nginx/sites-available/app.multiunitpros.com

server {
  server_name app.multiunitpros.com www.app.multiunitpros.com app.multiunitpros.com;
  error_log /var/www/app.multiunitpros.com/logs/app.multiunitpros.com.error.log;
  access_log /var/www/app.multiunitpros.com/logs/app.multiunitpros.com.access.log;
  root /var/www/app.multiunitpros.com/public/dist;
  index index.php index.html;
  large_client_header_buffers 4 16k;

  location /api {
    proxy_pass http://localhost:1337;
    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_request_headers      on;
    client_max_body_size 100M;
    rewrite ^/api/?(.*) /$1 break;
  }

  location / {
    try_files $uri $uri/ @rewrites;
  }

  location @rewrites {
    rewrite ^((?!/api/).+)$ /index.html last;
  }


    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/app.multiunitpros.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/app.multiunitpros.com/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


}


server {
    if ($host = www.app.multiunitpros.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = app.multiunitpros.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


  listen 80;
  server_name app.multiunitpros.com www.app.multiunitpros.com app.multiunitpros.com;
    return 404; # managed by Certbot
}

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.

×
1 answer

Hi there @amarjanovic26,

Indeed there is a question with the same error here:

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

Basically what this error means is that your backend service is not running so Nginx is failing to connect.

What you need to do is make sure that either your backend service is running, one way to do that is to use the netstat -plant command and look for your backend service. Or another thing that you could do is make sure that you are using the correct port in your proxy_pass rule.

Here is a link to an answer on how to troubleshoot common Nginx problems:

https://www.digitalocean.com/community/questions/how-to-troubleshoot-common-nginx-issues-on-linux-server

And here is also a quick video demo on how to do that as well:

Hope that this helps!
Regards,
Bobby

  • Thanks for the response, it helps a bit. I’ve done some research and i have got this message with the command sudo nginx -s reload

    nginx: [warn] conflicting server name “app.multiunitpros.com” on 0.0.0.0:80, ignored
    nginx: [warn] conflicting server name “app.multiunitpros.com” on 0.0.0.0:80, ignored

    Where can i check this conflict?

    • Hello @amarjanovic26,

      You can check that in the /etc/nginx/sites-enabled folder. In that folder, you will be able to see all of your Nginx server blocks and you need to make sure that you don’t use the same hostname in multiple files.

      Note that using the same hostname for port 80 and port 443 is OK.

      Let me know how it goes!
      Regards,
      Bobby

Submit an Answer