Question

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

Posted September 27, 2021 112 views
NginxPHPAPIUbuntu 18.04Ubuntu 20.04

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?

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.

×
Submit an Answer
1 answer

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