Nginx Cheatsheet
Gzip and Performance
Use this Nginx reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.
Gzip Compression
http {
gzip on;
gzip_vary on; # Add Vary: Accept-Encoding header
gzip_proxied any; # Compress responses for proxied requests
gzip_comp_level 6; # 1 (fastest) – 9 (best compression); 5-6 is the sweet spot
gzip_min_length 256; # Don't compress tiny responses
gzip_types
text/plain
text/css
text/javascript
application/javascript
application/json
application/xml
application/rss+xml
image/svg+xml
font/woff2;
# Never add image/jpeg, image/png — already compressed
}Gzip Directive Reference
| Directive | Recommended | What it does |
|---|---|---|
gzip | on | Enable gzip |
gzip_vary | on | Add Vary: Accept-Encoding (required for CDNs/caches) |
gzip_comp_level | 6 | Compression level 1–9 |
gzip_min_length | 256 | Skip tiny files (bytes) |
gzip_types | see above | MIME types to compress (text/html always included) |
gzip_proxied | any | Compress responses for proxied clients |
gzip_buffers | 16 8k | Buffers used for compression |
gzip_http_version | 1.1 | Min HTTP version to compress |
gzip_disable | "msie6" | Skip gzip for matching User-Agent |
Pre-compressed Files (gzip_static)
Serve .gz files that already exist on disk — no CPU overhead per request:
location /assets/ {
root /var/www/app;
gzip_static on; # Serves file.js.gz when file.js is requested
}Build step: gzip -k -9 dist/assets/*.js dist/assets/*.css
Brotli (requires module)
Nginx has no built-in brotli. Debian/Ubuntu package the ngx_brotli module for their distro nginx; the official nginx.org OSS packages do not include it (compile ngx_brotli yourself or use NGINX Plus).
# Debian/Ubuntu distro nginx (auto-loads via /etc/nginx/modules-enabled/)
sudo apt install libnginx-mod-http-brotli-filter libnginx-mod-http-brotli-static# Self-compiled ngx_brotli (nginx.org builds): load the dynamic modules
load_module modules/ngx_http_brotli_filter_module.so;
load_module modules/ngx_http_brotli_static_module.so;brotli on; brotli_comp_level 6; # 0–11 brotli_static on; # Serve .br pre-compressed files brotli_types text/plain text/css application/javascript application/json image/svg+xml font/woff2;
Sendfile and TCP Tuning
http {
sendfile on; # Zero-copy for static files
tcp_nopush on; # Bundle headers + first data in one TCP packet
tcp_nodelay on; # Disable Nagle — reduce latency on keepalives
}Keepalive Connections
http {
keepalive_timeout 65; # How long to keep idle client connection open (s)
keepalive_requests 1000; # Max requests per keepalive connection
}Worker Tuning
# /etc/nginx/nginx.conf worker_processes auto; # One per logical CPU worker_rlimit_nofile 65535; # Raise OS file descriptor limit events { worker_connections 4096; # Simultaneous connections per worker multi_accept on; # Accept all queued connections at once use epoll; # Best I/O event model on Linux }
Max connections = worker_processes × worker_connections.
Open File Cache
http {
open_file_cache max=10000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
}Caches file descriptors, sizes, and mtimes — reduces stat() syscalls on high-traffic static sites.
Client Buffers
http {
client_body_buffer_size 128k;
client_header_buffer_size 1k;
client_max_body_size 50m; # Max upload size
large_client_header_buffers 4 8k;
}Upstream Keepalives
upstream backend {
server 127.0.0.1:3000;
keepalive 32; # Idle keepalive connections to upstream
}
location / {
proxy_pass http://backend;
proxy_http_version 1.1;
proxy_set_header Connection ""; # Required for upstream keepalives
}Rate Limiting
http {
limit_req_zone $binary_remote_addr zone=api:10m rate=10r/s;
server {
location /api/ {
limit_req zone=api burst=20 nodelay;
proxy_pass http://backend;
}
}
}| Parameter | Example | Meaning |
|---|---|---|
rate | 10r/s | Sustained request rate |
burst | 20 | Allow up to 20 extra requests to queue |
nodelay | — | Process burst immediately, not delayed |
delay=N | delay=5 | Delay requests after N, drop excess |
Connection Limiting
http {
limit_conn_zone $binary_remote_addr zone=conn_limit:10m;
server {
location / {
limit_conn conn_limit 20; # Max 20 concurrent connections per IP
}
}
}