How to upgrade nginx safely on Ubuntu and Debian (config test, reload, rollback)
nginx security fixes arrive as ordinary package updates and rarely break anything, but a configuration that worked on the old version can be refused by the new one. Testing before the reload keeps your sites up.
Note the version and save the configuration
nginx -T prints the full effective configuration, which is a perfect backup.
nginx -v sudo nginx -T > ~/nginx-config-$(date +%F).txt 2>&1 sudo cp -a /etc/nginx ~/nginx-etc-backupSee what the update will change
Only nginx and its modules should be listed.
apt list --upgradable 2>/dev/null | grep -i nginxUpgrade the package
Upgrade only nginx, not the whole system, so you know what changed.
sudo apt-get install --only-upgrade nginx nginx-commonTest the configuration with the new binary
If this fails, do not reload: fix it first. The running nginx keeps serving the old configuration.
sudo nginx -tReload, do not restart
A reload keeps existing connections and applies the new configuration.
sudo systemctl reload nginx systemctl is-active nginx nginx -vCheck the sites
Request the main pages and read the error log.
curl -sI https://your-domain.com | head -n 1 sudo tail -n 20 /var/log/nginx/error.log
Common mistakes
- Restarting instead of reloading and dropping live connections.
- Mixing the distribution package with the nginx.org repository.
- Third-party modules built for one nginx version failing on the next.
How to undo it
Install the previous version with sudo apt-get install nginx=VERSION nginx-common=VERSION, restore /etc/nginx from the backup and reload.
Sources
Commands are for Debian and Ubuntu unless noted. Test on a copy first and keep a backup: you are responsible for changes on your own systems.