Question

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

Hi all,

Followed this tutorial to get Baserow working on my Ubuntu server.

Everything works - running sudo nginx -t returns:

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

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

However, accessing my domain gives me a 502 Bad Gateway error. Specifically (from /var/log/nginx):

2021/09/27 11:42:21 [error] 614151#614151: *48 connect() failed (111: Connection refused) while connecting to upstream, client: 185.140.255.82, server: baserow.contentpepper.dev, request: “GET / HTTP/1.1”, upstream: “http://127.0.0.1:3000/”, host: “baserow.contentpepper.dev” 2021/09/27 11:42:21 [error] 614151#614151: *48 connect() failed (111: Connection refused) while connecting to upstream, client: 185.140.255.82, server: baserow.contentpepper.dev, request: “GET /favicon.ico HTTP/1.1”, upstream: “http://127.0.0.1:3000/favicon.ico”, host: “baserow.contentpepper.dev”, referrer: “https://baserow.contentpepper.dev/

Here is my /etc/nginx/sites-enabled/baserow.conf file:

# Backend
server {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    gzip on;
    gzip_disable "msie6";
    server_name "api.baserow.contentpepper.dev";

    proxy_read_timeout 1800s;

    client_max_body_size 0; # avoid HTTP 413 for large image uploads
    # required to avoid HTTP 411: see Issue #1486 (https://github.com/dotcloud/docker/issues/1486)
    chunked_transfer_encoding on;

    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_http_version 1.1;
        proxy_pass_request_headers on;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/api.baserow.contentpepper.dev/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/api.baserow.contentpepper.dev/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

}

# Web frontend
server {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    gzip on;
    gzip_disable "msie6";
    server_name "baserow.contentpepper.dev";

    proxy_read_timeout 1800s;

    client_max_body_size 0; # avoid HTTP 413 for large image uploads
    # required to avoid HTTP 411: see Issue #1486 (https://github.com/dotcloud/docker/issues/1486)
    chunked_transfer_encoding on;

    location / {
        proxy_pass http://127.0.0.1:3000;
        proxy_pass_request_headers on;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }

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

}

# Media
server {
    server_name "media.baserow.contentpepper.dev";
    autoindex off;

    gzip on;
    gzip_disable "msie6";

    location / {
       root /srv/baserow/media;
    }

    location /user_files {
        root /srv/baserow/media;
        add_header Content-disposition "attachment; filename=$1";
    }

    location /export_files {
        root /srv/baserow/media;
        add_header Content-disposition "attachment; filename=$1";
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/media.baserow.contentpepper.dev/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/media.baserow.contentpepper.dev/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 = baserow.contentpepper.dev) {
        return 301 https://$host$request_uri;
    } # managed by Certbot



    listen 80;
    server_name "baserow.contentpepper.dev";
    return 404; # managed by Certbot


}
server {
    if ($host = api.baserow.contentpepper.dev) {
        return 301 https://$host$request_uri;
    } # managed by Certbot



    listen 80;
    server_name "api.baserow.contentpepper.dev";
    return 404; # managed by Certbot


}
server {
    if ($host = media.baserow.contentpepper.dev) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80;
    server_name "media.baserow.contentpepper.dev";
    return 404; # managed by Certbot


}

Any ideas please?


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.

Hello,

Just wanted to post my thoughts on the matter.

Usually, when you have a connect() failed (111: Connection refused) while connecting to upstream error, it’s related to your Application somehow. It’s not Nginx but it’s what follows after it. It can be a reverse proxy or just calling your website/app but it’s not related to Nginx. It’s the app that is the problem. Most of times is just some configurational error there.

Hello,

I can see that your website is already working now. This is not usually a problem with the Nginx service itself, but this happens when the backend service is not running so Nginx is not able to connect to it.

The fix is to start the backend service or if the backend service is listening on different port, you need to adjust the Nginx proxy configuration to match the port.

For more information, there has been a similar question asked here:

https://www.digitalocean.com/community/questions/connect-failed-111-connection-refused-while-connecting-to-upstream

Regards, Bobby