Nginx Cheat Sheet: Server Blocks, Location Matching, proxy_pass and TLS
Checked against nginx nginx 1.30.8 stable / 1.31.10 mainline (docs on nginx.org) on 28 November 2026. Each section links to the official docs it was checked against.
This sheet is for developers who run nginx as a web server or reverse proxy in front of an app. It follows the nginx.org reference for the 1.30.8 stable and 1.31.10 mainline releases from 23 Nov 2026. nginx isn't installed in my test shell, so every row is checked against the docs, not run.
When to use what
Use return for redirects whenever you can. It is one line, it can't loop, and the status code is explicit. Keep rewrite for cases where you need regex captures to build a new internal path, and remember its flags decide whether nginx searches locations again. Our redirects and rewrites post goes deeper, and the .htaccess to nginx converter helps when you are moving off Apache.
For static sites, try_files beats if (-f ...) checks: it tries paths in order and falls back to the last argument, which can be a URI, a named location or =404. For apps, put the app behind proxy_pass and decide deliberately whether to include a URI in it, because that one slash changes the path your backend sees.
Location order is where most surprises come from. Exact = matches win immediately, then the longest prefix is remembered, then regex locations are tried top to bottom. Use ^~ on a prefix like /static/ when you don't want a regex further down to steal those requests. The location tester shows which block a URI lands in, and the config generator writes a starting server block. Whatever you change, run nginx -t before nginx -s reload.
listen 80 default_server;Handles requests whose Host header matches no server_name on that address and port.
server_name example.com www.example.com;Names the virtual host; nginx picks the server by comparing this to the Host header.
return 444;Non-standard code that closes the connection without a response, handy in a catch-all default server.
root /var/www/site;Base directory; the full request URI is appended to it to find the file.
location = / { }Exact match; when it hits, the search stops at once and no other location is checked.
location ^~ /images/ { }If this is the longest matching prefix, regex and predicate locations are skipped.
location ~* \.(gif|jpg|jpeg)$ { }Case-insensitive regex; regexes are tried in file order and the first match wins (~ is case-sensitive).
location /documents/ { }Plain prefix; the longest one is remembered and used only if no regex matches.
location @backend { }Named location for internal redirects such as try_files or error_page; can't be nested.
location /user/ + proxy_passA request for /user without the slash gets a 301 to /user/; add location = /user to avoid it.
proxy_pass http://127.0.0.1/remote/;With a URI: the part matching the location is replaced, so /name/x becomes /remote/x.
proxy_pass http://127.0.0.1;Without a URI: the request URI is passed through unchanged; required in regex and named locations.
proxy_set_header Host $host;Sends the client's Host; by default nginx sends Host $proxy_host and Connection close.
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;Appends the client address to any existing X-Forwarded-For list.
proxy_set_header inheritanceInherited from the outer level only if the current level defines none; one header in a location drops all outer ones.
proxy_http_version 1.1;Default since 1.29.7; needed for upstream keepalive.
try_files is documented in the core module.
try_files $uri $uri/ /index.html;Try the file, then the directory, then fall back to the SPA entry point with an internal redirect.
try_files $uri $uri/index.html $uri.html =404;Last argument can be a status code instead of a URI.
return 301 https://$host$request_uri;Permanent redirect; a URL is allowed with 301, 302, 303, 307 and 308.
rewrite ^/old/(.*)$ /new/$1 permanent;Regex rewrite that sends a 301; redirect sends a 302.
rewrite ... last;Stop rewrite directives here and search locations again with the new URI.
rewrite ... break;Stop rewrite directives and keep serving in the current location; use it inside the target location to avoid 10 loops and a 500.
gzip on;Turn on response compression for the matching MIME types.
gzip_types text/css application/javascript application/json;Add types to compress; text/html is always included and is the default.
gzip_min_length 1000;Skip tiny responses; the default threshold is 20 bytes, judged from Content-Length.
gzip_comp_level 5;Compression level 1-9; default is 1.
gzip_proxied any;Also compress proxied responses; the default off skips them.
gzip_vary on;Adds Vary: Accept-Encoding so caches keep compressed and plain copies apart; default off.
Directive defaults are from the SSL module reference.
listen 443 ssl;Enable TLS on the listening socket.
ssl_certificate / ssl_certificate_keyPaths to the certificate chain and private key for this server.
ssl_protocols TLSv1.2 TLSv1.3;The current default; you only need the line to narrow it further.
ssl_session_cache shared:SSL:10m;Shares session cache across workers so returning clients skip a full handshake.
http2 on;Enable HTTP/2 for the server (directive since 1.25.1).
nginx -tCheck syntax and try to open the files the config refers to, without touching the running server.
nginx -TSame check, then print the whole merged config, including every include.
nginx -s reloadStart workers with the new config and gracefully stop the old ones.
nginx -s quitGraceful shutdown; -s stop shuts down quickly.
nginx -s reopenReopen log files after log rotation moved them.
nginx -VPrint version, compiler and configure arguments, which shows the modules built in.
Worked example: Reverse proxy an app with HTTPS redirect and a static folder
Goal: send all HTTP traffic to HTTPS, serve /static/ from disk, and proxy everything else to an app on port 3000.
server {
listen 80;
server_name example.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
http2 on;
server_name example.com;
ssl_certificate /etc/nginx/certs/example.com.pem;
ssl_certificate_key /etc/nginx/certs/example.com.key;
location ^~ /static/ {
root /var/www/example;
try_files $uri =404;
}
location / {
proxy_pass http://127.0.0.1:3000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
sudo nginx -t && sudo nginx -s reload
The first server only redirects, so nothing on port 80 reaches the app. In the second, ^~ /static/ means a request like /static/logo.png never falls through to a regex location you add later, and root plus the URI gives /var/www/example/static/logo.png. Missing files return 404 instead of hitting the app. proxy_pass has no URI, so the app receives the original path untouched. The three proxy_set_header lines live in the same block, which matters because a single header set at location level would hide any set at server level. Chaining -t before the reload means a typo stops at the test and the running server keeps its old config. I couldn't run nginx here; a passing test prints that the syntax is ok and the test is successful.
Common mistakes
Writing
proxy_pass http://app/;with a trailing slash and finding/api/usersarrives as/users.A URI in
proxy_passreplaces the matched location part. Drop the slash to pass the path unchanged.Adding one
proxy_set_headerinside a location and losing the headers set at server level.Inheritance is all or nothing. Repeat every header in that location, or move them all into an include file.
Putting
rewrite ... last;inside the location it rewrites to.nginx loops ten times and returns a 500. Use
breakinside that location, or a plainreturnfor redirects.Expecting a regex location to win over
location /static/for.jsfiles.It does win unless the prefix has
^~. Add^~to the prefix if static files must always be served from it.Running
systemctl restart nginxafter every edit.A restart drops connections and a bad config leaves you down. Run
nginx -t, thennginx -s reload.Turning on gzip and wondering why JSON isn't compressed.
Only
text/htmlis compressed by default. Listapplication/jsonand other types ingzip_types.
Related cheat sheets
Sources
- https://nginx.org/en/docs/http/ngx_http_core_module.html
- https://nginx.org/en/docs/http/ngx_http_proxy_module.html
- https://nginx.org/en/docs/http/ngx_http_rewrite_module.html
- https://nginx.org/en/docs/http/ngx_http_gzip_module.html
- https://nginx.org/en/docs/http/ngx_http_ssl_module.html
- https://nginx.org/en/docs/http/configuring_https_servers.html
- https://nginx.org/en/docs/http/request_processing.html
- https://nginx.org/en/docs/switches.html
- https://nginx.org/
How we research: where our facts come from, how often we re-check them, and why no one can pay for a review or a verdict.