Nginx Server Block Directives, Location Matching & Proxy Reference
Developer cheat sheet for Nginx location block matching order, reverse proxy headers, WebSocket upgrades, SSL hardening, and gzip compression.
Need to generate an Nginx virtual host configuration?
Build complete reverse proxy configs with SSL, gzip, security headers, and WebSocket support.
Location Block Modifiers & Match Priority
Order of evaluation Nginx uses to select matching location blocks.
| Syntax / Token | Name / Type | Description | Example / Notes |
|---|---|---|---|
location = /exact/path | 1. Exact Match (=) | Highest priority. Matches only exact URI string. Processing stops immediately upon match. | — |
location ^~ /images/ | 2. Preferential Prefix (^~) | Matches prefix URI. If matched, Nginx skips regular expression checking. | — |
location ~ \.php$ | 3. Case-Sensitive RegEx (~) | Evaluated in order of appearance in configuration file. | — |
location ~* \.(jpg|png)$ | 4. Case-Insensitive RegEx (~*) | Evaluates regex without case sensitivity. | — |
location /prefix/ | 5. Standard Prefix (None) | Longest matching prefix is used if no regex matches. | — |
Standard Reverse Proxy Directives
Essential proxy headers for Node.js, Python, and Go backends.
| Syntax / Token | Name / Type | Description | Example / Notes |
|---|---|---|---|
proxy_pass http://127.0.0.1:3000; | Upstream Target | Passes request to specified backend application server | — |
proxy_set_header Host $host; | Host Header | Forwards the original request Host header to backend | — |
proxy_set_header X-Real-IP $remote_addr; | Client IP Header | Forwards the actual visitor IP address | — |
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; | Forwarded For | Appends client IP to proxy chain list | — |
proxy_set_header X-Forwarded-Proto $scheme; | Protocol Header | Informs backend whether client connected over http or https | — |
SPA & Static Asset Fallbacks
Clean URL routing for React, Vue, Next.js, and static sites.
| Syntax / Token | Name / Type | Description | Example / Notes |
|---|---|---|---|
try_files $uri $uri/ /index.html; | SPA History Fallback | Serves file, directory, or falls back to index.html for client-side routing | — |
expires 1y;
add_header Cache-Control "public, immutable"; | Static Asset Caching | Long-term caching for hashed JS/CSS/image assets | — |
Frequently Asked Questions
Common questions and developer tips regarding nginx location directives & reverse proxy guide.
Explore More Cheat Sheets
All Cheat Sheets →Regular Expression (RegEx) Syntax & Tokens
A complete developer cheat sheet for RegEx tokens, character classes, anchors, lookaheads, lookbehinds, flags, and common production patterns with 1-click copy.
Crontab Syntax & Schedule Reference
Quick reference guide for Unix/Linux crontab schedule syntax, 5-field time breakdown, step values, special characters, and common cron presets.
HTTP Status Codes (1xx–5xx) Quick Reference
Comprehensive quick reference for all standard RFC HTTP status codes, REST API conventions, caching behaviors, and client/server error definitions.