This is a short guide on how to test your Nginx configuration file without restarting your web server. This can help to prevent a situation where your web server fails to restart or reload because of a nasty “[emerg]” configuration error.
The command is as follows:
sudo nginx -c /etc/nginx/nginx.conf -t
The command above consists of two important flags:
- -c: This tells Nginx which configuration file to test. In the case above, we chose “/etc/nginx/nginx.conf”
- -t: This flag tells Nginx to test the file instead of actually running it.
Note that if you run the command above, Nginx will also test for errors in your sites-enabled files. i.e. If you have a syntax error in one of your server blocks, this test should make you aware of it.
If you want to test your config before reloading Nginx, then you can use the following command:
sudo nginx -t && sudo service nginx reload
If the config test fails, then Nginx will not attempt to reload the web server.