Basic Configuration
3 snippetsCore directives
Main Structure
# /etc/nginx/nginx.conf
worker_processes auto;
events {
worker_connections 1024;
}
http {
include /etc/nginx/conf.d/*.conf;
}Server Block
server {
listen 80;
server_name example.com;
root /var/www/html;
index index.html;
}Test & Reload
nginx -t # Test config
nginx -s reload # Reload
nginx -s stop # Stop
systemctl restart nginxLocation Blocks
4 snippetsURL routing
Prefix Match
location /images/ {
root /var/www;
}Exact Match
location = /favicon.ico {
log_not_found off;
}Regex Match
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php-fpm.sock;
}
location ~* \.(jpg|png|gif)$ {
expires 30d;
}Priority
# 1. = exact match
# 2. ^~ prefix (stops search)
# 3. ~ or ~* regex
# 4. prefix match (longest)Reverse Proxy
4 snippetsForward requests to backends
Basic Proxy
location / {
proxy_pass http://localhost:3000;
}With Headers
location /api/ {
proxy_pass http://backend:8080/;
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;
}WebSocket Proxy
location /ws/ {
proxy_pass http://backend:8080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}Upstream
upstream backend {
server 10.0.0.1:8080 weight=3;
server 10.0.0.2:8080;
server 10.0.0.3:8080 backup;
}
server {
location / {
proxy_pass http://backend;
}
}Tired of looking up syntax?
DocuWriter.ai generates documentation and explains code using AI.
SSL/TLS
4 snippetsHTTPS configuration
Basic SSL
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /etc/ssl/certs/example.crt;
ssl_certificate_key /etc/ssl/private/example.key;
}SSL Settings
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256;
ssl_prefer_server_ciphers off;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 1d;Redirect HTTP
server {
listen 80;
server_name example.com;
return 301 https://$server_name$request_uri;
}Let's Encrypt
# Certbot location
location /.well-known/acme-challenge/ {
root /var/www/certbot;
}Caching
3 snippetsCache static content
Browser Cache
location ~* \.(css|js|jpg|png|gif|ico)$ {
expires 30d;
add_header Cache-Control "public, no-transform";
}Proxy Cache
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m;
server {
location / {
proxy_cache my_cache;
proxy_cache_valid 200 1h;
proxy_cache_valid 404 1m;
proxy_pass http://backend;
}
}Disable Cache
location /api/ {
add_header Cache-Control "no-store, no-cache";
expires -1;
}Security
5 snippetsHarden your server
Hide Version
server_tokens off;Security Headers
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
add_header X-XSS-Protection "1; mode=block";
add_header Referrer-Policy "strict-origin-when-cross-origin";Rate Limiting
limit_req_zone $binary_remote_addr zone=mylimit:10m rate=10r/s;
server {
location /api/ {
limit_req zone=mylimit burst=20 nodelay;
}
}Deny Access
location /admin {
allow 192.168.1.0/24;
deny all;
}Basic Auth
location /admin {
auth_basic "Admin Area";
auth_basic_user_file /etc/nginx/.htpasswd;
}Rewrites & Redirects
4 snippetsURL manipulation
Redirect
# Temporary redirect
return 302 https://example.com$request_uri;
# Permanent redirect
return 301 https://example.com$request_uri;Rewrite
# Internal rewrite
rewrite ^/old-page$ /new-page permanent;
rewrite ^/blog/(\d+)$ /posts/$1 last;Try Files
location / {
try_files $uri $uri/ /index.html;
}Remove Trailing Slash
rewrite ^/(.*)/$ /$1 permanent;Logging
4 snippetsAccess and error logs
Access Log
access_log /var/log/nginx/access.log;
access_log off; # DisableError Log
error_log /var/log/nginx/error.log warn;
# Levels: debug, info, notice, warn, error, critCustom Format
log_format main '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent"';
access_log /var/log/nginx/access.log main;Conditional Logging
map $status $loggable {
~^[23] 0;
default 1;
}
access_log /var/log/nginx/access.log combined if=$loggable;