How to test Nginx config file without restarting your Web Server

Usually after adding a new website configuration to Nginx we will restart immediately without testing the configuration. If the configuration fails, this will cause the working websites to fail. To fix this before calling restart or reload.

sudo nginx -t

or

sudo nginx -c /etc/nginx/nginx.conf -t

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 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.

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.

#nginx

How to test Nginx config file without restarting your Web Server
17.25 GEEK