The original post: /r/nginx by /u/yegor-usoltsev on 2024-12-04 11:20:20.

Hi all,

I’ve been experimenting with HTTP keep-alive in NGINX as a reverse proxy and documented my findings in this GitHub repo.

The one thing that caught my attention is that NGINX does require additional configuration in order for it to reuse upstream connections, unlike other proxies such as HAProxy, Traefik, or Caddy, which all enable HTTP keep-alive by default. So here’s my final configuration that came out of this:

server {
    location / {
        proxy_pass http://backend/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
    }
}

map $http_upgrade $connection_upgrade {
    default upgrade;
    "" "";
}

upstream backend {
    server 127.0.0.1:8080;
    keepalive 16;
}

To the community:

  1. Why keep-alive isn’t enabled by default in NGINX?
  2. Are there any edge cases I might have overlooked?
  3. What would you suggest for simplifying or improving those configurations?

Looking forward to hearing your thoughts!