Skip to content

Menu

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.

Try it: Nginx Config GeneratorFrom the docs, not run by us
server_name example.com www.example.com;

Names the virtual host; nginx picks the server by comparing this to the Host header.

Try it: Nginx Config GeneratorFrom the docs, not run by us
return 444;

Non-standard code that closes the connection without a response, handy in a catch-all default server.

From the docs, not run by us
root /var/www/site;

Base directory; the full request URI is appended to it to find the file.

From the docs, not run by us
location = / { }

Exact match; when it hits, the search stops at once and no other location is checked.

Try it: nginx-location-testerFrom the docs, not run by us
location ^~ /images/ { }

If this is the longest matching prefix, regex and predicate locations are skipped.

Try it: nginx-location-testerFrom the docs, not run by us
location ~* \.(gif|jpg|jpeg)$ { }

Case-insensitive regex; regexes are tried in file order and the first match wins (~ is case-sensitive).

Try it: nginx-location-testerFrom the docs, not run by us
location /documents/ { }

Plain prefix; the longest one is remembered and used only if no regex matches.

Try it: nginx-location-testerFrom the docs, not run by us
location @backend { }

Named location for internal redirects such as try_files or error_page; can't be nested.

From the docs, not run by us
location /user/ + proxy_pass

A request for /user without the slash gets a 301 to /user/; add location = /user to avoid it.

From the docs, not run by us
proxy_pass http://127.0.0.1/remote/;

With a URI: the part matching the location is replaced, so /name/x becomes /remote/x.

From the docs, not run by us
proxy_pass http://127.0.0.1;

Without a URI: the request URI is passed through unchanged; required in regex and named locations.

From the docs, not run by us
proxy_set_header Host $host;

Sends the client's Host; by default nginx sends Host $proxy_host and Connection close.

From the docs, not run by us
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

Appends the client address to any existing X-Forwarded-For list.

From the docs, not run by us
proxy_set_header inheritance

Inherited from the outer level only if the current level defines none; one header in a location drops all outer ones.

From the docs, not run by us
proxy_http_version 1.1;

Default since 1.29.7; needed for upstream keepalive.

From the docs, not run by us

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.

From the docs, not run by us
try_files $uri $uri/index.html $uri.html =404;

Last argument can be a status code instead of a URI.

From the docs, not run by us
return 301 https://$host$request_uri;

Permanent redirect; a URL is allowed with 301, 302, 303, 307 and 308.

Try it: htaccess-to-nginxFrom the docs, not run by us
rewrite ^/old/(.*)$ /new/$1 permanent;

Regex rewrite that sends a 301; redirect sends a 302.

Try it: htaccess-to-nginxFrom the docs, not run by us
rewrite ... last;

Stop rewrite directives here and search locations again with the new URI.

From the docs, not run by us
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.

From the docs, not run by us
gzip on;

Turn on response compression for the matching MIME types.

From the docs, not run by us
gzip_types text/css application/javascript application/json;

Add types to compress; text/html is always included and is the default.

From the docs, not run by us
gzip_min_length 1000;

Skip tiny responses; the default threshold is 20 bytes, judged from Content-Length.

From the docs, not run by us
gzip_comp_level 5;

Compression level 1-9; default is 1.

From the docs, not run by us
gzip_proxied any;

Also compress proxied responses; the default off skips them.

From the docs, not run by us
gzip_vary on;

Adds Vary: Accept-Encoding so caches keep compressed and plain copies apart; default off.

From the docs, not run by us

Directive defaults are from the SSL module reference.

listen 443 ssl;

Enable TLS on the listening socket.

Try it: Nginx Config GeneratorFrom the docs, not run by us
ssl_certificate / ssl_certificate_key

Paths to the certificate chain and private key for this server.

From the docs, not run by us
ssl_protocols TLSv1.2 TLSv1.3;

The current default; you only need the line to narrow it further.

From the docs, not run by us
ssl_session_cache shared:SSL:10m;

Shares session cache across workers so returning clients skip a full handshake.

From the docs, not run by us
http2 on;

Enable HTTP/2 for the server (directive since 1.25.1).

From the docs, not run by us
nginx -t

Check syntax and try to open the files the config refers to, without touching the running server.

From the docs, not run by us
nginx -T

Same check, then print the whole merged config, including every include.

From the docs, not run by us
nginx -s reload

Start workers with the new config and gracefully stop the old ones.

From the docs, not run by us
nginx -s quit

Graceful shutdown; -s stop shuts down quickly.

From the docs, not run by us
nginx -s reopen

Reopen log files after log rotation moved them.

From the docs, not run by us
nginx -V

Print version, compiler and configure arguments, which shows the modules built in.

From the docs, not run by us

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/users arrives as /users.

    A URI in proxy_pass replaces the matched location part. Drop the slash to pass the path unchanged.

  • Adding one proxy_set_header inside 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 break inside that location, or a plain return for redirects.

  • Expecting a regex location to win over location /static/ for .js files.

    It does win unless the prefix has ^~. Add ^~ to the prefix if static files must always be served from it.

  • Running systemctl restart nginx after every edit.

    A restart drops connections and a bad config leaves you down. Run nginx -t, then nginx -s reload.

  • Turning on gzip and wondering why JSON isn't compressed.

    Only text/html is compressed by default. List application/json and other types in gzip_types.