brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: nginx -t -c tests a new config file before it replaces the live one

Nginx
~/tips/tip-nginx-test-staged-config-c

sudo nginx -t -c /tmp/stage/nginx.conf

Checks a staged nginx.conf for errors while the running config stays untouched.

nginx -t checks the config that is already in place. For a bigger change, I’d test it before it goes into /etc/nginx at all.

sudo nginx -t -c /tmp/stage/nginx.conf
sudo nginx -t -q -c /tmp/stage/nginx.conf && echo "safe to copy"

-c points nginx at a different main config file, and -t only tests it, so nothing reloads. Errors come back with the file and line number, like unknown directive "bogus_directive" in /tmp/stage/nginx.conf:10. -q hides the success messages, which keeps a deploy script quiet unless something is wrong.

Relative include paths are resolved against the directory of the file you pass with -c. include conf.d/*.conf; in /tmp/stage/nginx.conf then reads /tmp/stage/conf.d/, and the check fails until mime.types is there too. That makes a full staged copy easy: copy the whole /etc/nginx folder, edit it, test it. Absolute include paths still point at the live files, so check them.

The test also opens the log files and certificates the config names, which is why it needs sudo. A pass means the syntax and files are fine, not that the routing does what you meant. Add -T to print the full merged config and read the part you changed.

Further reading: nginx command-line parameters

Going deeper: 🛜 Guide: AdGuard Home + Tailscale (Secure Split-Horizon DNS)

More Nginx tips sudo certbot --nginx -d example.com -d www.example.com /data/nginx/custom/server_redirect.conf map $http_upgrade $connection_upgrade { proxy_intercept_errors on; proxy_redirect http://localhost:3000/ /app/; All Nginx tips (50) All topics

Link