Question

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

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
}

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.

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.

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