AI Generate Nginx docs instantly

Nginx Cheat Sheet

Quick reference guide with copy-paste ready code snippets

Try DocuWriter Free

Basic Configuration

3 snippets

Core 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 nginx

Location Blocks

4 snippets

URL 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 snippets

Forward 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.

Try Free

SSL/TLS

4 snippets

HTTPS 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 snippets

Cache 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 snippets

Harden 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 snippets

URL 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 snippets

Access and error logs

Access Log

access_log /var/log/nginx/access.log;
access_log off;  # Disable

Error Log

error_log /var/log/nginx/error.log warn;
# Levels: debug, info, notice, warn, error, crit

Custom 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;

More Cheat Sheets

FAQ

Frequently asked questions

What is a Nginx cheat sheet?

A Nginx cheat sheet is a quick reference guide containing the most commonly used syntax, functions, and patterns in Nginx. It helps developers quickly look up syntax without searching through documentation.

How do I learn Nginx quickly?

Start with the basics: variables, control flow, and functions. Use this cheat sheet as a reference while practicing. For faster learning, try DocuWriter.ai to automatically explain code and generate documentation as you learn.

What are the most important Nginx concepts?

Key Nginx concepts include variables and data types, control flow (if/else, loops), functions, error handling, and working with data structures like arrays and objects/dictionaries.

How can I document my Nginx code?

Use inline comments for complex logic, docstrings for functions and classes, and README files for projects. DocuWriter.ai can automatically generate professional documentation from your Nginx code using AI.

Code Conversion Tools

Convert Nginx to Other Languages

Easily translate your Nginx code to other programming languages with our AI-powered converters

Related resources

Stop memorizing. Start shipping.

Generate Nginx Docs with AI

DocuWriter.ai automatically generates comments, docstrings, and README files for your code.

Auto-generate comments
Create README files
Explain complex code
API documentation
Start Free - No Credit Card

Join 33,700+ developers saving hours every week