Sub topic 3
Reusing upstream connections with keepalive
This chapter assumes you can edit Nginx files with sudo, read HTTP status codes and headers, and run a backend app on a local port. Nginx forwards requests to that backend over upstream connections.
Keeping upstream connections open
By default, Nginx opens a new TCP connection to the backend for each request and closes it afterward. The keepalive directive keeps idle connections open for reuse. It works only with HTTP/1.1, and the Connection header must be cleared so Nginx does not send Connection: close.
upstream app_backend {
server 127.0.0.1:3000;
keepalive 32;
}
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://app_backend;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header Host $host;
}
}Terminating TLS and redirecting HTTP to HTTPS
TLS (Transport Layer Security) is the encryption behind HTTPS. Terminating TLS means Nginx decrypts requests, then sends plain HTTP to the backend. The example below reuses app_backend from the previous section.
server {
listen 80;
server_name example.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /etc/ssl/certs/example.com.crt;
ssl_certificate_key /etc/ssl/private/example.com.key;
ssl_protocols TLSv1.2 TLSv1.3;
location / {
proxy_pass http://app_backend;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
}
}Tuning timeouts and buffering
proxy_connect_timeout limits how long Nginx waits to open a connection to the backend. proxy_read_timeout limits the wait between reads from the backend. Buffering stores backend responses in Nginx memory, so a slow client does not hold the backend connection open.
location / {
proxy_pass http://app_backend;
proxy_connect_timeout 5s;
proxy_read_timeout 60s;
proxy_buffering on;
proxy_buffer_size 4k;
proxy_buffers 8 16k;
}Proxying WebSocket connections
A WebSocket starts as an HTTP request with an Upgrade header. Nginx must pass that header and use HTTP/1.1. The map block, placed in the http context, sets Connection to upgrade only when the client asks for it. The example below is a separate server block for illustration.
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
listen 80;
server_name example.com;
location /ws/ {
proxy_pass http://app_backend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Host $host;
proxy_read_timeout 300s;
}
}Caching upstream responses
proxy_cache_path defines an on-disk cache in the http context. proxy_cache turns it on for a location. The X-Cache-Status header shows whether each response was a hit, a miss, or bypassed.
proxy_cache_path /var/cache/nginx/app levels=1:2 keys_zone=app_cache:10m max_size=1g inactive=60m use_temp_path=off;
server {
listen 80;
server_name example.com;
location / {
proxy_cache app_cache;
proxy_cache_valid 200 301 10m;
add_header X-Cache-Status $upstream_cache_status;
proxy_pass http://app_backend;
}
}Reading logs and troubleshooting upstream errors
Always test the configuration before reloading:
sudo nginx -t && sudo systemctl reload nginxThis log format records which backend answered each request and how long it took:
log_format upstream_log '$remote_addr [$time_local] "$request" $status '
'upstream=$upstream_addr upstream_status=$upstream_status '
'urt=$upstream_response_time rt=$request_time';
access_log /var/log/nginx/proxy_access.log upstream_log;| Status | Meaning | Common cause |
|---|---|---|
| 502 Bad Gateway | The backend refused, closed, or returned an invalid response | Backend process is down or crashed |
| 504 Gateway Timeout | The backend did not respond within the timeout | Slow query or a read timeout that is too short |
Check /var/log/nginx/error.log for the exact cause, then compare upstream_status and urt in the access log to find the slow or failing backend.
Exercise
Why does proxysetheader Connection "" matter when keepalive is configured?
Show answer
Nginx sends Connection: close to the backend by default. Clearing the header stops that, so the backend connection stays open and Nginx can reuse it.