Skip to content

📋 Nginx Cheat Sheet

Common Commands

OperationCommand
Check config syntaxnginx -t
Reload confignginx -s reload
Stop Nginxnginx -s stop
Graceful shutdownnginx -s quit
Reopen log filesnginx -s reopen
Show versionnginx -v
Show compile flagsnginx -V

Location Matching Rules

SyntaxMeaningExample
=Exact matchlocation = /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

VariableMeaning
$hostRequest hostname
$request_uriFull original request URI (with parameters)
$uriCurrent URI (without parameters)
$remote_addrClient IP
$remote_portClient port
$server_portNginx listening port
$schemehttp or https
$request_methodGET/POST/PUT/DELETE
$content_typeRequest Content-Type
$upstream_cache_statusCache 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;