Putting Nginx in front of an application server gives you TLS termination, static file serving, connection buffering, and a clean place to enforce upload limits. A minimal proxy_pass block is only five lines, which makes it tempting to copy and forget. Unfortunately, the defaults inside Nginx were designed for an older web, and several subtle behaviors still catch people out in production.
A solid baseline server block
Here is the baseline reverse proxy configuration I start from for an HTTP backend listening on 127.0.0.1:8080:
server {
listen 80;
server_name app.example.com;
client_max_body_size 20M;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_connect_timeout 5s;
proxy_read_timeout 60s;
proxy_send_timeout 60s;
}
}
Every line there prevents a specific failure mode. Let's walk through the ones that cause the most confusion.
Pitfall 1: The trailing slash on proxy_pass
This is the classic Nginx gotcha. Compare these two blocks:
# Passes /api/users -> http://127.0.0.1:8080/api/users
location /api/ {
proxy_pass http://127.0.0.1:8080;
}
# Strips /api/ and passes /api/users -> http://127.0.0.1:8080/users
location /api/ {
proxy_pass http://127.0.0.1:8080/;
}
When proxy_pass has no URI path (not even a trailing slash), Nginx forwards the original request URI as-is. When proxy_pass includes a URI path—even a single /—Nginx replaces the matched location prefix with that URI. Missing or adding that single slash breaks routing and OAuth callback paths constantly.
Pitfall 2: HTTP/1.0 by default
Nginx still defaults to proxy_http_version 1.0 when talking to upstreams. That disables keepalive connections to your backend and breaks WebSockets and Server-Sent Events (SSE). Always set proxy_http_version 1.1.
If your app uses WebSockets, you also need to forward the Upgrade and Connection headers conditionally. Put this map block in /etc/nginx/nginx.conf inside http {}:
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
Then inside the location block:
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
Using a map instead of hardcoding Connection "upgrade" ensures normal HTTP requests still close or reuse connections properly without confusing upstreams.
Pitfall 3: proxy_set_header inheritance
In Nginx, array-like directives such as proxy_set_header and add_header do not merge across scopes. If you define four proxy_set_header lines in server {} and then add one extra proxy_set_header inside a nested location /admin/ {}, all four parent headers are silently dropped in that location.
To avoid this, put shared proxy headers in a snippet file such as /etc/nginx/snippets/proxy-params.conf (Debian/Ubuntu even ships a basic proxy_params file) and include it whenever you override headers in a nested block.
Pitfall 4: The 1 MB upload limit
Nginx defaults client_max_body_size to 1m. Any file upload or large JSON payload over 1 MB fails immediately with 413 Request Entity Too Large before it ever reaches your application. Set client_max_body_size explicitly at the server or location level based on what your app actually accepts.
Pitfall 5: Buffering breaking streaming responses
By default, Nginx buffers the entire upstream response in memory or temporary files before sending it to slow clients. For normal pages and JSON APIs, buffering is great because it frees your app worker immediately. For SSE streams or large dynamically generated downloads, buffering stalls the stream until the buffer fills.
location /events {
proxy_pass http://127.0.0.1:8080;
proxy_http_version 1.1;
proxy_buffering off;
proxy_cache off;
proxy_read_timeout 3600s;
}
Pitfall 6: Upstream keepalive pools
Under higher local concurrency, opening a brand-new TCP connection to 127.0.0.1:8080 for every request can exhaust ephemeral ports. Defining an upstream block with keepalive 32; lets Nginx reuse idle backend connections cleanly, as long as you pair it with proxy_http_version 1.1 and clear the Connection header for normal HTTP requests.
Always test before reloading
Never run systemctl reload nginx blindly. Always validate the configuration syntax first:
sudo nginx -t && sudo systemctl reload nginx
If nginx -t fails, the existing running workers keep serving traffic untouched while you fix the typo. And if a request returns 502 Bad Gateway, check /var/log/nginx/error.log immediately—it will tell you whether the backend refused the connection, timed out, or closed early.