Nginx Configuration Cheatsheet
Quick reference for Nginx directives covering basic config, location blocks, reverse proxy, SSL/TLS, caching, security, performance, and logging
64 commands
server { }Define a server block
server { listen 80; server_name example.com; }listenSpecify listening port
listen 80;listen (IPv6)Listen on IPv6
listen [::]:80;server_nameSet server name (domain)
server_name example.com www.example.com;rootSet document root
root /var/www/html;indexSet default index files
index index.html index.htm;worker_processesSet number of worker processes
worker_processes auto;worker_connectionsMax connections per worker
worker_connections 1024;includeInclude external config files
include /etc/nginx/conf.d/*.conf;nginx -tTest configuration syntax
nginx -tnginx -s reloadReload configuration
nginx -s reloadnginx -s stopStop Nginx
nginx -s stoplocation /Prefix match location
location / { try_files $uri $uri/ =404; }location = /pathExact match location
location = /favicon.ico { log_not_found off; }location ~ regexRegex match (case-sensitive)
location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; }location ~* regexRegex match (case-insensitive)
location ~* \.(jpg|jpeg|png|gif)$ { expires 30d; }location ^~ /pathPriority prefix match
location ^~ /images/ { root /data; }try_filesTry files in order
try_files $uri $uri/ /index.html;aliasReplace location path with alias
location /static/ { alias /var/www/assets/; }returnReturn specified status code
return 301 https://$host$request_uri;rewriteRewrite URL
rewrite ^/old/(.*)$ /new/$1 permanent;proxy_passForward requests to backend
proxy_pass http://127.0.0.1:3000;proxy_set_header HostSet Host header
proxy_set_header Host $host;proxy_set_header X-Real-IPForward client IP to backend
proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-ForForward proxy chain IPs
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-ProtoForward original protocol
proxy_set_header X-Forwarded-Proto $scheme;upstreamDefine upstream server group
upstream backend { server 127.0.0.1:3000; server 127.0.0.1:3001; }proxy_read_timeoutBackend read timeout
proxy_read_timeout 90s;proxy_connect_timeoutBackend connect timeout
proxy_connect_timeout 30s;proxy_bufferingEnable/disable proxy buffering
proxy_buffering off;listen 443 sslListen on 443 with SSL
listen 443 ssl;ssl_certificateSSL certificate file path
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;ssl_certificate_keySSL private key file path
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;ssl_protocolsAllowed SSL/TLS protocols
ssl_protocols TLSv1.2 TLSv1.3;ssl_ciphersSpecify cipher suites
ssl_ciphers HIGH:!aNULL:!MD5;ssl_prefer_server_ciphersPrefer server cipher suites
ssl_prefer_server_ciphers on;ssl_session_cacheSet SSL session cache
ssl_session_cache shared:SSL:10m;ssl_session_timeoutSSL session timeout
ssl_session_timeout 1d;expiresSet response expiration
expires 30d;add_header Cache-ControlAdd Cache-Control header
add_header Cache-Control 'public, max-age=31536000';proxy_cache_pathSet proxy cache path
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m;proxy_cacheSpecify proxy cache zone
proxy_cache my_cache;proxy_cache_validSet cache validity period
proxy_cache_valid 200 60m;proxy_cache_bypassConditions to bypass cache
proxy_cache_bypass $http_pragma;denyDeny access from specified IP
deny 192.168.1.100;allowAllow access from specified IP
allow 10.0.0.0/8;auth_basicEnable Basic authentication
auth_basic 'Restricted Area';auth_basic_user_fileSpecify Basic auth user file
auth_basic_user_file /etc/nginx/.htpasswd;add_header X-Frame-OptionsClickjacking protection header
add_header X-Frame-Options SAMEORIGIN;add_header X-Content-Type-OptionsMIME sniffing prevention header
add_header X-Content-Type-Options nosniff;limit_req_zoneDefine rate limit zone
limit_req_zone $binary_remote_addr zone=one:10m rate=10r/s;limit_reqApply rate limiting
limit_req zone=one burst=20 nodelay;gzipEnable gzip compression
gzip on;gzip_typesSpecify MIME types for gzip
gzip_types text/plain text/css application/json application/javascript;gzip_min_lengthMinimum size for gzip
gzip_min_length 256;sendfileEnable sendfile (fast file transfer)
sendfile on;tcp_nopushEnable TCP_NOPUSH
tcp_nopush on;tcp_nodelayEnable TCP_NODELAY
tcp_nodelay on;keepalive_timeoutSet Keep-Alive timeout
keepalive_timeout 65;access_logSet access log path
access_log /var/log/nginx/access.log;error_logSet error log path and level
error_log /var/log/nginx/error.log warn;log_formatDefine custom log format
log_format main '$remote_addr - $remote_user [$time_local] "$request" $status';access_log offDisable access log
access_log off;open_log_file_cacheSet log file cache
open_log_file_cache max=1000 inactive=20s valid=1m min_uses=2;