502 Bad Gateway Right After I Enabled HTTPS, But The Django App Swears Its Up

Solved502nginxreverse-proxy5 replies · 169 views · started
Accepted solution by Malik Bauer — Start with the nginx error log, it tells u which kind of 502 it is: [code] connect() failed (111: Connection refused) means nothing…
  1. This one is driving me crazy. right after I enabled HTTPS, a chunk of requests fail with this page:

    502 Bad Gateway

    502 Bad Gateway
    nginx
    

    Not all of them, maybe one in ten. The Django app health check is green the whole time. My manager asked if we 'turned it off and on again'. We did. Several times.

    load is not crazy, a few hundred requests a minute at peak. id realy like to understand what nginx is complaining about before i start randomly changing timeouts and buffer sizes and make things worse. thx in advance!!

    facepalm

    3
  2. wait what exactly does /var/log/nginx/error.log say at the moment of a 502??

    0 2 helpful
  3. oh ive been there lol check what address the app binds to: ss -ltnp | grep <port> (the port in proxy_pass). if it listens on 127.0.0.1 but proxy_pass uses the servers LAN IP (or the other way around, or IPv6 [::1] vs 127.0.0.1 via localhost), nginx will get refused while ur curl works. lmk how it goes

    6 0 helpful
  4. Accepted solution

    Start with the nginx error log, it tells u which kind of 502 it is:

    sudo tail -f /var/log/nginx/error.log
    

    connect() failed (111: Connection refused) means nothing listens where proxy_pass points. upstream prematurely closed connection means the app accepted and then dropped the request. no live upstreams means all backends were marked down. good luck man!

    14 4 helpful
  5. ehh you could also drop nginx for this single-app setup and let a managed load balancer handle it, but thats a bigger chagne than the fix you probably need.

    5 1 helpful

Reply to this topic

Sign in or create an account to reply.