Claude
Skills
Sign in
Back

nginx

Included with Lifetime
$97 forever

Nginx reverse proxy and web server configuration skill. USE WHEN: - Configuring Nginx as a reverse proxy in front of application servers (Node.js, Python, Ruby, Java) - Setting up HTTPS/TLS termination with certificates - Configuring virtual hosts, upstream load balancing, and rate limiting - Optimising static file serving, gzip compression, and cache headers - Adding security headers (HSTS, CSP, X-Frame-Options, X-Content-Type-Options) - Setting up WebSocket proxying - Diagnosing 502, 504, 413, or SSL handshake errors DO NOT USE FOR: - Application-level logic (belongs in the app server, not Nginx) - Container orchestration routing (use Kubernetes Ingress or a service mesh skill instead) - Full API gateway features (rate-limit-by-user, auth, JWT — use api-gateway skill) - Apache httpd configuration

Backend & APIs

What this skill does


# Nginx — Reverse Proxy & Web Server

## Core Concepts

Nginx uses a **master/worker** process model. The master reads config and manages workers; workers handle connections. The event loop in each worker is non-blocking, so a single worker can handle thousands of concurrent connections.

Key config hierarchy:

```
http { ... }                # Global HTTP settings
  upstream backend { ... }  # Pool of app servers
  server { ... }            # Virtual host (vhost)
    location / { ... }      # URI matching block
```

Nginx evaluates `server` blocks by `listen` port and `server_name`. Inside a `server`, `location` blocks are matched in order: exact (`=`), prefix longest-match (`^~`), regex (`~`, `~*`), then implicit prefix.

---

## Production HTTPS Server Block (Full Template)

```nginx
# /etc/nginx/sites-available/myapp.conf

# --- Upstream pool -----------------------------------------------------------
upstream app_backend {
    least_conn;                        # Route to least-busy worker
    server 127.0.0.1:3000;
    server 127.0.0.1:3001;
    keepalive 64;                      # Persistent connections to upstream
}

# --- Rate limiting zones (defined in http{} context in nginx.conf) -----------
# limit_req_zone $binary_remote_addr zone=api:10m rate=20r/s;
# limit_req_zone $binary_remote_addr zone=login:10m rate=5r/m;

# --- HTTP → HTTPS redirect ---------------------------------------------------
server {
    listen 80;
    listen [::]:80;
    server_name example.com www.example.com;

    # Allow Let's Encrypt ACME challenge
    location /.well-known/acme-challenge/ {
        root /var/www/certbot;
    }

    location / {
        return 301 https://$host$request_uri;
    }
}

# --- Main HTTPS server -------------------------------------------------------
server {
    listen 443 ssl;
    listen [::]:443 ssl;
    http2 on;                          # Nginx ≥ 1.25.1 directive (older: listen 443 ssl http2)
    server_name example.com www.example.com;

    # --- TLS -----------------------------------------------------------------
    ssl_certificate     /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;

    ssl_protocols       TLSv1.2 TLSv1.3;
    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:DHE-RSA-AES128-GCM-SHA256;
    ssl_prefer_server_ciphers off;     # Let TLS 1.3 clients pick

    ssl_session_cache   shared:SSL:10m;
    ssl_session_timeout 1d;
    ssl_session_tickets off;           # Disable for forward secrecy

    ssl_stapling        on;
    ssl_stapling_verify on;
    resolver            1.1.1.1 8.8.8.8 valid=300s;
    resolver_timeout    5s;

    ssl_dhparam         /etc/nginx/ssl/dhparam.pem;  # openssl dhparam -out dhparam.pem 2048

    # --- Security headers ----------------------------------------------------
    add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" always;
    add_header X-Frame-Options           "SAMEORIGIN" always;
    add_header X-Content-Type-Options    "nosniff" always;
    add_header X-XSS-Protection          "1; mode=block" always;
    add_header Referrer-Policy           "strict-origin-when-cross-origin" always;
    add_header Permissions-Policy        "geolocation=(), microphone=(), camera=()" always;
    add_header Content-Security-Policy   "default-src 'self'; script-src 'self' 'unsafe-inline' https://cdn.example.com; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:; font-src 'self'; connect-src 'self' https://api.example.com; frame-ancestors 'none';" always;

    # --- Gzip ----------------------------------------------------------------
    gzip              on;
    gzip_vary         on;
    gzip_proxied      any;
    gzip_comp_level   6;
    gzip_types        text/plain text/css text/xml text/javascript
                      application/json application/javascript application/xml+rss
                      application/atom+xml image/svg+xml;
    gzip_min_length   1024;

    # --- Logging -------------------------------------------------------------
    access_log /var/log/nginx/myapp.access.log combined buffer=4k flush=5s;
    error_log  /var/log/nginx/myapp.error.log warn;

    # --- Static assets with long-lived cache ---------------------------------
    location /static/ {
        alias /var/www/myapp/static/;
        expires 1y;
        add_header Cache-Control "public, immutable";
        access_log off;
    }

    # --- Rate-limited API endpoints ------------------------------------------
    location /api/auth/ {
        limit_req zone=login burst=10 nodelay;
        limit_req_status 429;
        proxy_pass http://app_backend;
        include /etc/nginx/proxy_params;
    }

    location /api/ {
        limit_req zone=api burst=50 nodelay;
        limit_req_status 429;
        proxy_pass http://app_backend;
        include /etc/nginx/proxy_params;
    }

    # --- WebSocket endpoint --------------------------------------------------
    location /ws/ {
        proxy_pass         http://app_backend;
        proxy_http_version 1.1;
        proxy_set_header   Upgrade $http_upgrade;
        proxy_set_header   Connection "upgrade";
        proxy_set_header   Host $host;
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_read_timeout 3600s;      # Keep WS connections open
        proxy_send_timeout 3600s;
    }

    # --- Default proxy -------------------------------------------------------
    location / {
        proxy_pass http://app_backend;
        include /etc/nginx/proxy_params;
    }

    # --- Error pages ---------------------------------------------------------
    error_page 404             /404.html;
    error_page 500 502 503 504 /50x.html;

    location = /404.html {
        root /var/www/myapp/errors;
        internal;
    }

    location = /50x.html {
        root /var/www/myapp/errors;
        internal;
    }
}
```

---

## /etc/nginx/proxy_params (shared include file)

```nginx
proxy_http_version 1.1;
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 Connection        "";   # Required for keepalive upstream
proxy_connect_timeout 10s;
proxy_send_timeout    60s;
proxy_read_timeout    60s;
proxy_buffering       on;
proxy_buffer_size     8k;
proxy_buffers         8 8k;
```

---

## Worker / Global Tuning (/etc/nginx/nginx.conf)

```nginx
user  www-data;
worker_processes      auto;           # One per CPU core
worker_rlimit_nofile  65535;          # Match system ulimit -n

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  4096;         # Max connections per worker
    multi_accept        on;
    use                 epoll;        # Linux only; Nginx selects automatically
}

http {
    sendfile            on;
    tcp_nopush          on;           # Batch send headers + start of file
    tcp_nodelay         on;           # Disable Nagle for keepalive connections
    keepalive_timeout   75s;
    keepalive_requests  1000;

    server_tokens       off;          # Don't reveal Nginx version in headers

    client_max_body_size 50m;         # Increase if file uploads are needed
    client_body_timeout  30s;
    client_header_timeout 30s;

    # Rate limit zones (referenced in server blocks)
    limit_req_zone $binary_remote_addr zone=api:10m   rate=20r/s;
    limit_req_zone $binary_remote_addr zone=login:10m rate=5r/m;
    limit_conn_zone $binary_remote_addr zone=addr:10m;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;   # Not text/html — avoids MIME sniffing

    include /etc/nginx/conf.d/*.conf;
   

Related in Backend & APIs