Copy · paste · nginx -t · reload

Nginx config you can actually paste.

Complete server and location blocks for the things you actually configure — reverse-proxy to an app, proxy WebSockets, redirect HTTP → HTTPS and www → apex, fall back to index.html for a SPA, compress and cache, rate-limit, add Basic Auth, raise the upload size, and set up modern TLS — each with the nginx -t test and the reload command.

19 snippets

Why this exists

You know Nginx can do it. You don't remember the exact directive.

Nginx reverse-proxies, terminates TLS, redirects, rewrites, compresses, caches, and rate-limits — but the precise block (the WebSocket Upgrade headers? try_files vs rewrite? the trailing-slash rule on proxy_pass?) never sticks, so you paste a half-right snippet from a forum and hope. nginxsnippets.pages.dev is a library of focused, copy-paste config blocks for real tasks, each with a one-line explanation, the nginx -t / reload commands, and the gotchas (location matching order, header forwarding, the 1m upload default).

How it works

Find the task, copy the block, test and reload

  1. Pick a snippet. Browse by topic in the full snippet list.
  2. Paste the block into the right server (one-click copy on every block).
  3. Run nginx -t then reload — each snippet gives the exact commands and where each directive belongs.

FAQ

Frequently asked questions

Are these Nginx snippets free?

Yes. Every snippet on nginxsnippets.pages.dev is free to read and copy, with no account, paywall, or sign-up. Some outbound links (for example to VPS hosting, a CDN, or courses) may be affiliate links, which never change the price you pay.

Where do I put these in my Nginx config?

A location block goes inside a server { } block. On Debian/Ubuntu that server block lives in /etc/nginx/sites-available/<site> (symlinked into sites-enabled/); on RHEL/Fedora it lives in /etc/nginx/conf.d/<site>.conf. Directives that must sit at the http { } level — limit_req_zone, map, proxy_cache_path, upstream — go in nginx.conf or a file included from conf.d.

How do I apply a change without downtime?

Always run "sudo nginx -t" first to test the syntax, then "sudo nginx -s reload" (or "sudo systemctl reload nginx") to apply it. A reload re-reads the config and starts new worker processes while finishing existing requests — zero downtime. A restart drops connections, so reload is what you want for config edits.

Will these work on my Nginx version?

The standard directives here work on Nginx 1.18+ and most work on much older builds. A couple of things are version-sensitive — the "http2 on;" directive (1.25.1+; older builds use "listen ... http2") and brotli (a separate module, ngx_brotli) — and the snippets call those out. Check your version with "nginx -v".

Reverse proxy or serve static files — which do I need?

If your app (Node, Python, Go, PHP-FPM, etc.) listens on a port or socket, reverse-proxy to it with proxy_pass. If you just have built files (HTML/CSS/JS, images), serve them statically with root + try_files. A single-page app does both: serve the static build and fall back to /index.html for client-side routes.

How does Nginx choose which location block matches?

Nginx checks an exact match ("location = /path") first, then prefix matches keeping the longest, then regex matches ("~" case-sensitive, "~*" case-insensitive) in the order they appear — and a regex match wins over a plain prefix unless that prefix used "^~". When matching order matters for a snippet (e.g. a static-asset regex vs a SPA fallback), the recipe says so.