How do I correct a 502 Bad Gateway, nginx/1.18.0 error?

The 502 Bad Gateway error began after I updated some application code and did a restart on nginx on my linode but made no changes to nginx or gunicorn config files. Prior to this, it had been running for over a year without a problem.

I'm running a flask application.

nginx error.log:
2024/04/12 07:11:52 [warn] 91781#91781: conflicting server name "170.187.207.132" on 0.0.0.0:80, ignored
2024/04/12 07:11:52 [warn] 91781#91781: conflicting server name "talk-time.org" on 0.0.0.0:80, ignored
2024/04/12 07:11:52 [warn] 91781#91781: conflicting server name "www.talk-time.org" on 0.0.0.0:80, ignored
2024/04/12 07:11:52 [warn] 91782#91782: conflicting server name "170.187.207.132" on 0.0.0.0:80, ignored
2024/04/12 07:11:52 [warn] 91782#91782: conflicting server name "talk-time.org" on 0.0.0.0:80, ignored
2024/04/12 07:11:52 [warn] 91782#91782: conflicting server name "www.talk-time.org" on 0.0.0.0:80, ignored
2024/04/12 07:12:23 [error] 91784#91784: *1 connect() failed (111: Unknown error) while connecting to upstream, client: 24.16.175.241, server: 170.187.207.132, request: "GET / HTTP/1.1", upstream: "http://[::1]:8000/", host: "talk-time.org"
2024/04/12 07:12:25 [error] 91784#91784: *1 recv() failed (104: Unknown error) while reading response header from upstream, client: 24.16.175.241, server: 170.187.207.132, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8000/", host: "talk-time.org"
2024/04/12 07:12:25 [error] 91784#91784: *1 no live upstreams while connecting to upstream, client: 24.16.175.241, server: 170.187.207.132, request: "GET /favicon.ico HTTP/1.1", upstream: "http://ttproxy/favicon.ico", host: "talk-time.org", referrer: "https://talk-time.org/"

The pertinent information seems to be "no live upstreams while connecting to upstream" but I don't know where to go from there.

Here is /etc/nginx/sites-enabled/app.conf:

upstream ttproxy {
server localhost:8000;
}

server {
listen 80;
server_name 170.187.207.132;
server_name talk-time.org;
server_name www.talk-time.org;

location / {
    proxy_pass http://ttproxy;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host;
    proxy_redirect off;
    proxy_ssl_server_name on;
}

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

if ($host = talk-time.org) {
    return 301 https://$host$request_uri;
} # managed by Certbot

if ($host = 170.187.207.132) {
    return 301 https://$host$request_uri;
}

listen 80;
server_name 170.187.207.132;
server_name talk-time.org;
server_name www.talk-time.org;
return 404; # managed by Certbot

location / {
    proxy_pass http://ttproxy;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host;
    proxy_redirect off;
    proxy_ssl_server_name on;
}

}

I have applied every fix that I have found in the community posts and anything I have been able to find in stackoverflow, etc.but they have made no difference.

Any help would be appreciated.

1 Reply

Most of my research on this has led to NGINX suggestions that I assume you've already tried or issues with Flask applications. However, I did this searching without mentioning Flask, which leads me to believe something about your application may be causing the errors.

I also found several discussions, including one above that mentions buffer size as a place to investigate.

While there could be an issue with NGINX, since the application changed just before the problem started, I think it's worth investigating there. I'd recommend looking into any logs relevant to Flask or into any debugging you can do to find errors that may help you get your site back up.

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct