Production Nginx setup for 2026.
nginx.conf
user www-data;
worker_processes auto;
worker_rlimit_nofile 65536;
pid /run/nginx.pid;
events {
worker_connections 10240;
use epoll;
multi_accept on;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
server_tokens off;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
keepalive_requests 1000;
client_body_buffer_size 16k;
client_header_buffer_size 1k;
client_max_body_size 10m;
large_client_header_buffers 4 16k;
types_hash_max_size 2048;
server_names_hash_bucket_size 64;
open_file_cache max=10000 inactive=60s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
# TLS
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers off;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305;
ssl_session_cache shared:SSL:50m;
ssl_session_timeout 1d;
ssl_session_tickets off;
ssl_stapling on;
ssl_stapling_verify on;
resolver 1.1.1.1 8.8.8.8 valid=300s;
resolver_timeout 5s;
# Real IP
set_real_ip_from 10.0.0.0/8;
set_real_ip_from 172.16.0.0/12;
real_ip_header X-Forwarded-For;
real_ip_recursive on;
# Logging
log_format json escape=json '{'
'"time":"$time_iso8601",'
'"remote_addr":"$remote_addr",'
'"method":"$request_method",'
'"path":"$request_uri",'
'"status":$status,'
'"bytes":$body_bytes_sent,'
'"referer":"$http_referer",'
'"ua":"$http_user_agent",'
'"rt":$request_time,'
'"urt":"$upstream_response_time",'
'"upstream":"$upstream_addr",'
'"request_id":"$request_id"'
'}';
access_log /var/log/nginx/access.log json buffer=64k flush=5s;
error_log /var/log/nginx/error.log warn;
# Compression
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_min_length 256;
gzip_types text/plain text/css application/javascript application/json application/xml image/svg+xml application/wasm application/manifest+json;
# Rate limiting
limit_req_zone $binary_remote_addr zone=ip:10m rate=10r/s;
limit_req_zone $http_x_api_key zone=api:10m rate=100r/s;
limit_conn_zone $binary_remote_addr zone=conn_ip:10m;
# Headers (defaults; override per-server if needed)
map $sent_http_content_type $hsts_max_age {
default 31536000;
}
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
Site config (sites-available/example.com)
upstream app {
server 10.0.0.1:8000;
server 10.0.0.2:8000;
keepalive 64;
keepalive_requests 1000;
}
# HTTP redirect
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
location /.well-known/acme-challenge/ {
root /var/www/html;
}
location / {
return 301 https://example.com$request_uri;
}
}
# www → apex
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name www.example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
return 301 https://example.com$request_uri;
}
# Main HTTPS
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
# Security headers
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
add_header X-Frame-Options "DENY" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Permissions-Policy "camera=(), microphone=(), geolocation=()" always;
add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'nonce-$request_id'; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:; connect-src 'self' https://api.example.com" always;
# Rate limits
limit_req zone=ip burst=20 nodelay;
limit_conn conn_ip 50;
# Block hidden files
location ~ /\.(?!well-known) {
deny all;
access_log off;
log_not_found off;
}
# Health endpoint
location = /health {
access_log off;
return 200 "ok\n";
add_header Content-Type text/plain;
}
# Static
location ~* \.(css|js|woff2|svg|png|jpg|webp|ico)$ {
expires 1y;
add_header Cache-Control "public, immutable";
access_log off;
try_files $uri @app;
}
# API
location /api/ {
limit_req zone=api burst=200 nodelay;
proxy_pass http://app;
proxy_http_version 1.1;
proxy_set_header Connection "";
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_set_header X-Request-ID $request_id;
proxy_connect_timeout 10s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
proxy_next_upstream error timeout http_502 http_503 http_504;
proxy_next_upstream_tries 2;
}
# SSE
location /events {
proxy_pass http://app;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_buffering off;
proxy_cache off;
proxy_read_timeout 24h;
}
# WebSocket
location /ws {
proxy_pass http://app;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 86400;
}
# SPA / app
location / {
try_files $uri @app;
}
location @app {
proxy_pass http://app;
proxy_http_version 1.1;
proxy_set_header Connection "";
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;
}
}
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
logrotate
# /etc/logrotate.d/nginx
/var/log/nginx/*.log {
daily
missingok
rotate 14
compress
delaycompress
notifempty
create 0640 www-data adm
sharedscripts
postrotate
[ -f /var/run/nginx.pid ] && kill -USR1 `cat /var/run/nginx.pid`
endscript
}
sysctl
# /etc/sysctl.d/nginx.conf
net.core.somaxconn = 65535
net.ipv4.tcp_max_syn_backlog = 65535
net.ipv4.ip_local_port_range = 1024 65535
fs.file-max = 2097152
ulimit
# /etc/security/limits.d/nginx.conf
nginx soft nofile 65536
nginx hard nofile 65536
CI deployment
- run: nginx -t -c /etc/nginx/nginx.conf # validate
- run: rsync -avz nginx/ server:/etc/nginx/
- run: ssh server "nginx -t && nginx -s reload"
Monitoring
- Prometheus + nginx-prometheus-exporter at
/nginx_status. - Grafana dashboards (ID 12708 etc).
- Alert on: 5xx rate, latency, active conns, cert expiry.
Backup
tar czf nginx-config-$(date +%F).tar.gz /etc/nginx /etc/letsencrypt
Store off-site.
Read this next
That’s 20 Nginx cheatsheets. Next category: Linux.
If you want my full production nginx setup, it’s at rajpoot.dev .
Building something AI-, backend-, or data-heavy and want a second pair of eyes? I do consulting and freelance work — see my projects and ways to reach me at rajpoot.dev .