tips
50 tips Nginx
One command, setting or habit at a time about Nginx: what it does, the line to copy and a caveat or two.
Nginx Proxy Manager 12
-
A custom file that Nginx Proxy Manager includes at the end of every redirection server block.
/data/nginx/custom/server_redirect.conf -
Replaces the default weekly log rotation of Nginx Proxy Manager with your own file.
services: -
Explains what the Block Common Exploits switch blocks and why it can cause a 403 in your app.
include conf.d/include/block-exploits.conf; -
Blocks visitors from outside the countries you choose on selected Nginx Proxy Manager hosts.
load_module /usr/lib/nginx/modules/ngx_http_geoip2_module.so; -
Removes the nginx version number from the Server header and error pages.
server_tokens off; -
Skips chosen requests, such as health checks, when writing the access log.
access_log /var/log/nginx/access.log combined if=$loggable; -
Adds request and upstream timing to each access log line.
log_format timed '$remote_addr "$request" $status $body_bytes_sent ' -
Explains why headers set in the server block disappear inside a location with its own add_header.
add_header X-Content-Type-Options nosniff always; # server block -
Tells browsers to keep static files for 30 days and skip repeat downloads.
expires 30d; -
Compresses CSS, JavaScript and JSON responses, which nginx skips by default.
gzip on; -
Limits how often one client address may request a path and returns 429 beyond that.
location = /login { limit_req zone=login burst=5 nodelay; -
Prints the complete configuration nginx would load, with each included file marked.
sudo nginx -T | less
Proxying 11
-
Sends Connection upgrade only when the client asks for it, and close otherwise.
map $http_upgrade $connection_upgrade { -
Replaces backend 502, 503 and 504 responses with a friendly page that nginx serves itself.
proxy_intercept_errors on; -
Rewrites Location headers from a backend so redirects stay on your public domain.
proxy_redirect http://localhost:3000/ /app/; -
Replaces strings in HTML from a backend, like hardcoded internal URLs, before it reaches the browser.
sub_filter 'http://127.0.0.1:8081/' 'https://example.com/app/'; -
Removes headers that reveal backend software versions before responses reach the client.
proxy_hide_header X-Powered-By; -
Looks up upstream host names at run time so nginx starts even when a backend is missing.
upstream app { -
Stops nginx from buffering a proxied response, so streamed data reaches the client as it arrives.
proxy_buffering off; -
Keeps a few connections from nginx to the backend open for reuse instead of reconnecting per request.
upstream app_backend { -
Takes a failing backend out of rotation for a while and uses a spare server meanwhile.
upstream app { -
Forwards the original host name and scheme to the application behind the proxy.
proxy_set_header Host $host; proxy_set_header X-Forwarded-Proto $scheme; -
Shows how the slash at the end of proxy_pass strips or keeps the location prefix.
location /app/ { proxy_pass http://127.0.0.1:3000; } # backend sees /app/page
Routing 8
-
Keeps hundreds of old-to-new redirects in one lookup table instead of many location blocks.
map $request_uri $new_uri { -
Creates an htpasswd line with openssl and puts a password on one nginx location.
printf 'admin:%s\n' "$(openssl passwd -apr1)" | sudo tee -a /etc/nginx/htpasswd -
Lets a backend approve a download and hand the actual file transfer to nginx.
location /protected/ { -
Blocks POST, PUT and DELETE on paths that should only ever be read.
location /api/public/ { -
Explains which file path nginx builds with root and with alias, so static files stop returning 404.
location /i/ { -
Makes a prefix location final so no regex location can take over its requests.
location ^~ /uploads/ { root /var/www; } -
Sends a permanent redirect that keeps the path and query string, without a regex.
return 301 https://example.com$request_uri; -
Falls back to one HTML file when the requested path does not exist on disk.
try_files $uri $uri/ /index.html;
Speed and limits 6
-
Serves the last cached copy of a page when the backend errors out or is busy refreshing.
proxy_cache app; -
Throttles big downloads per connection after the first part, so small files stay quick.
limit_rate_after 500k; -
Sends ready-made .gz versions of static files, compressed once at maximum level.
gzip -k -9 dist/app.css dist/app.js -
Limits how many connections one IP address may keep open at once.
limit_conn addr 1; -
Restricts a path to chosen addresses and refuses everyone else.
allow 192.168.1.0/24; allow 10.8.0.2; -
Raises the 1 MB upload limit for one path and keeps it low everywhere else.
location /upload/ {
Timeouts and proxies 5
-
Tells nginx which errors should send a request on to another upstream server.
upstream app { -
Stops nginx from waiting a full minute to connect to a backend that is not there.
proxy_connect_timeout 3s; -
Keeps idle TCP and UDP streams open longer than the ten minute default.
proxy_timeout 1h; -
Makes nginx read the real visitor address from a header when another proxy sits in front.
set_real_ip_from 192.168.1.0/24; -
Keeps quiet WebSocket connections open longer than the 60 second default.
proxy_read_timeout 1h;
Config and testing 4
-
Writes config from templates with your environment values when the official container starts.
server { -
Adds a tiny endpoint that answers ok without touching any backend or file.
return 200 "ok\n"; -
Shows how many connections nginx has open and what they are doing, on a small status page.
location = /basic_status { -
Tests the nginx configuration for errors, so a typo does not take your sites down.
sudo nginx -t && sudo systemctl reload nginx
Commands 3
-
Checks a staged nginx.conf for errors while the running config stays untouched.
sudo nginx -t -c /tmp/stage/nginx.conf -
Shows the configure flags and modules of the installed nginx binary.
nginx -V 2>&1 | tr ' ' '\n' | grep -- --with- -
Applies a changed configuration while connections that are in progress finish normally.
sudo nginx -s reload
HTTP and nginx 1
-
Get a Let's Encrypt certificate and enable it in nginx with one command
sudo certbot --nginx -d example.com -d www.example.com
Nothing in this filter yet.