Nginx Cheatsheet

Configuration Structure

Use this Nginx reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.

Top-level Layout

# Global / main context
user  www-data;
worker_processes  auto;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    server {
        listen 80;
        server_name example.com;

        location / {
            root /var/www/html;
        }
    }
}

Contexts nest strictly: main → events → http → server → location.

Context Reference

ContextPurposeLives inside
mainGlobal settings (user, PID, worker count)top level
events {}Connection-handling tuningmain
http {}All HTTP/HTTPS config; shared defaultsmain
server {}Virtual host — one block per domain/porthttp
location {}URI-specific rulesserver (or another location)
upstream {}Backend pool for proxying/load balancinghttp
stream {}TCP/UDP proxy (separate from http)main
mail {}Mail proxymain

Common Main-context Directives

user  nginx;                    # Worker process OS user
worker_processes  auto;         # Number of workers (auto = CPU count)
worker_rlimit_nofile  65535;    # Max open file descriptors per worker
error_log  /var/log/nginx/error.log  warn;  # Path + minimum level
pid  /run/nginx.pid;

events {} Directives

events {
    worker_connections  1024;   # Max simultaneous connections per worker
    use  epoll;                 # I/O method: epoll (Linux), kqueue (BSD)
    multi_accept  on;           # Accept all new connections at once
}

Max total connections = worker_processes × worker_connections.

http {} Shared Defaults

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    tcp_nopush      on;
    tcp_nodelay     on;
    keepalive_timeout  65;

    # Include all vhost files
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

server {} Block Essentials

server {
    listen       80;                    # IPv4
    listen       [::]:80;               # IPv6
    server_name  example.com www.example.com;

    root   /var/www/example;
    index  index.html index.htm;

    access_log  /var/log/nginx/example.access.log;
    error_log   /var/log/nginx/example.error.log;
}

server_name Matching Order

  1. Exact name (example.com)
  2. Leading wildcard (*.example.com)
  3. Trailing wildcard (example.*)
  4. Regex (~^(www\.)?example\.)
  5. Default server (default_server flag, or first defined)
listen 80 default_server;   # Catch-all for unmatched hostnames

Include and Snippets

# Inline include — path is relative to nginx prefix or absolute
include /etc/nginx/conf.d/*.conf;
include snippets/ssl-params.conf;

# Typical snippet file: /etc/nginx/snippets/ssl-params.conf
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers   HIGH:!aNULL:!MD5;

Inheritance Rules

  • Directives in outer contexts are inherited by inner contexts.
  • Inner context values override the outer value for that directive.
  • Most directives are not merged — the innermost wins entirely.
http {
    gzip on;        # inherited by all server blocks

    server {
        gzip off;   # overrides for this server only
    }
}

Variables

$host           # Host header (or server_name if absent)
$request_uri    # Full URI including query string
$uri            # Current URI (may be rewritten)
$args           # Query string
$remote_addr    # Client IP
$server_name    # Matched server_name value
$scheme         # http or https
$request_method # GET, POST, etc.

Use in directives: return 301 https://$host$request_uri;