Skip to content
Elephant House Logo

Gunicorn Keepalive and AWS ELB 502 Errors

I ran into an issue on AWS recently. Intermittent 502 errors with Gunicorn behind an ELB. Page rendering worked fine, but HTMX calls would randomly fail. It would work for a couple of minutes, then break, then work again. The requests never even reached the app server - ELB itself was returning the 502.

Took a while to figure out, but it came down to a mismatch between Gunicorn’s keepalive timeout and ELB’s idle timeout. Gunicorn defaults to 2 seconds, ELB defaults to 60.

When a request comes through, Gunicorn handles it and starts its keepalive timer. After 2 seconds of inactivity, Gunicorn closes the connection. But ELB doesn’t know this happened. It keeps that connection in its pool, assuming it’s still good for up to 60 seconds. When ELB tries to reuse that now-dead connection for another request, it fails. 502 Bad Gateway.

The fix is simple: set Gunicorn’s keepalive higher than ELB’s idle timeout.

# Set to 75s to safely exceed ALB's 60s default.
keepalive = 75

This keeps the connection alive long enough that ELB will always be the one to close it first, avoiding the race condition entirely.

Further reading

Gunicorn Configuration
My go-to Gunicorn configuration with extensive comments.
See all thoughts