📋 Nginx Cheat Sheet
Common Commands
| Operation | Command |
|---|---|
| Check config syntax | nginx -t |
| Reload config | nginx -s reload |
| Stop Nginx | nginx -s stop |
| Graceful shutdown | nginx -s quit |
| Reopen log files | nginx -s reopen |
| Show version | nginx -v |
| Show compile flags | nginx -V |
Location Matching Rules
| Syntax | Meaning | Example |
|---|---|---|
| = | Exact match | location = /api {} |
| ^~ | Prefix match (skip regex) | location ^~ /static {} |
| ~ | Regex match (case-sensitive) | location ~ .php$ {} |
| ~* | Regex match (case-insensitive) | location ~* .(jpg|png)$ {} |
| / | Prefix match (longest match) | location /api/ {} |
💡 Tip: 💡 Priority:
=>^~>~/~*(in order) > longest prefix
Common Variables
| Variable | Meaning |
|---|---|
| $host | Request hostname |
| $request_uri | Full original request URI (with parameters) |
| $uri | Current URI (without parameters) |
| $remote_addr | Client IP |
| $remote_port | Client port |
| $server_port | Nginx listening port |
| $scheme | http or https |
| $request_method | GET/POST/PUT/DELETE |
| $content_type | Request Content-Type |
| $upstream_cache_status | Cache status (HIT/MISS/EXPIRED) |
Security Headers Template
nginx
# One-click security hardening (recommended in the http block)
add_header X-Content-Type-Options "nosniff" always;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Content-Security-Policy "default-src 'self'" always;
# Hide Nginx version number
server_tokens off;