Question

Nginx - 502 Bad Gateway - No Upstreams

Hi Guys,

I am getting the following error when accessing domain linked droplet IP.

Droplet Ip address is opening but the domain linked to it is giving the below error.

2023/08/06 10:04:09 [error] 30587#30587: *15 no live upstreams while connecting to upstream, client: IP, server: example.com, request: "GET / HTTP/1.1", upstream: "http://localhost/", host: "example.com"

2023/08/06 10:07:06 [error] 30587#30587: *16 connect() failed (111: Unknown error) while connecting to upstream, client: IP, server: example.com, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8080/", host: "example.com"

Please note that it’s a simple HTML page. I want to use with shopify app built in node js, but as of now checking with normal html page

Below is the code of sites-enabled/example.com:

upstream example.com {
        server example.com:443;
        server www.example.com:443;
}
server {
        server_name example.com;
        location / {
                proxy_pass http://localhost:8080;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_cache_bypass $http_upgrade;
        }

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.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 = example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


        listen 80;
        listen [::]:80;
        server_name example.com;
    return 404; # managed by Certbot
}

Below is the code of sites-enabled/default

server {
        listen 80 default_server;
        listen [::]:80 default_server;

        root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        server_name _;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
                # proxy_pass http://localhost:8080;
                # proxy_http_version 1.1;
                # proxy_set_header Upgrade $http_upgrade;
                # proxy_set_header Connection 'upgrade';
                # proxy_set_header Host $host;
                # proxy_cache_bypass $http_upgrade;
        }
}

Any help would be really appreciated.

Thanks in advance :)


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.

Bobby Iliev
Site Moderator
Site Moderator badge
August 6, 2023

Hi there,

From the error messages you provided and the configuration you’ve shown, I believe that the problem is due to the misconfigured upstream definition.

Your upstream block in sites-enabled/example.com is pointing to example.com:443 for both www and non-www versions, which doesn’t make much sense.

This block is meant to define backend servers where requests will be forwarded to. Since you’re trying to proxy to a localhost server running on port 8080, this block is unnecessary. Remove the entire upstream block.

After that run an Nginx config test with sudo nginx -t and then restart Nginx if you get Syntax OK:

sudo systemctl restart nginx

Let me know how it goes!

Best,

Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel