NGINX Configuration Cheatsheet
1. Basic NGINX Setup
user www-data;
worker_processes auto;
pid /run/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
}
2. Virtual Hosts (Server Blocks)
server {
listen 80;
server_name example.com www.example.com;
root /var/www/html;
index index.html index.htm index.php;
location / {
try_files $uri $uri/ =404;
}
}
3. Redirects & URL Rewrites
301 Redirect HTTP → HTTPS
server {
listen 80;
server_name example.com www.example.com;
return 301 https://$host$request_uri;
}
Disable Direct Access
server {
listen 80;
server_name _;
location / {
return 301 $scheme://example.com$request_uri;
}
}
Redirect non-www to www
server {
listen 80;
server_name example.com;
return 301 http://www.example.com$request_uri;
}
Rewrite URL
location /old-page {
rewrite ^/old-page$ /new-page permanent;
}
4. Reverse Proxy & WebSocket Support
Basic Reverse Proxy
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Reverse Proxy with WebSocket
location /ws/ {
proxy_pass http://127.0.0.1:8080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
5. Load Balancing
Round Robin (Default)
upstream backend {
server backend1.example.com;
server backend2.example.com;
}
server {
location / {
proxy_pass http://backend;
}
}
Least Connections
upstream backend {
least_conn;
server backend1.example.com;
server backend2.example.com;
}
IP Hash (Sticky Sessions)
upstream backend {
ip_hash;
server backend1.example.com;
server backend2.example.com;
}
6. Advanced TLS Security (HTTPS)
server {
listen 443 ssl http2;
server_name example.com;
# SSL Certificate & Key
ssl_certificate /etc/nginx/ssl/example.com.crt;
ssl_certificate_key /etc/nginx/ssl/example.com.key;
# Strong TLS Protocols
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
# Secure Cipher Suites (No weak ciphers)
ssl_ciphers "TLS_AES_256_GCM_SHA384:TLS_AES_128_GCM_SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256";
# Enable OCSP Stapling (Performance & Security)
ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 10s;
# HSTS (Strict Transport Security)
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
# Security Headers
add_header X-Frame-Options DENY;
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options nosniff;
# TLS Session Resumption (Performance)
ssl_session_cache shared:SSL:50m;
ssl_session_timeout 1d;
# Serve Content
location / {
root /var/www/html;
}
}
7. Rate Limiting & Connection Control
http {
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
}
server {
location / {
limit_req zone=one burst=5 nodelay;
}
}
8. Gzip Compression
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
gzip_vary on;
9. PHP-FPM (FastCGI)
server {
listen 80;
server_name example.com;
root /var/www/html;
index index.php index.html;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
10. Caching
Static Content Caching
location ~* \.(jpg|jpeg|png|gif|ico|css|js|woff2?|ttf|svg|mp4|webp)$ {
expires 30d;
add_header Cache-Control "public, max-age=2592000";
}
Proxy Cache
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=cache:10m inactive=60m use_temp_path=off;
server {
location / {
proxy_cache cache;
proxy_pass http://backend;
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;
add_header X-Cache-Status $upstream_cache_status;
}
}
11. Security Headers & Blocking Bad Bots
server {
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options nosniff;
add_header Referrer-Policy "no-referrer-when-downgrade";
add_header Content-Security-Policy "default-src 'self';";
location / {
if ($http_user_agent ~* (badbot|evilbot|scraper|crawler)) {
return 403;
}
}
deny 192.168.1.1;
allow 192.168.1.0/24;
}
12. HTTP/2 Support
server {
listen 443 ssl http2;
server_name example.com;
ssl_certificate /etc/nginx/ssl/example.com.crt;
ssl_certificate_key /etc/nginx/ssl/example.com.key;
}