If you’re making changes to your nginx configuration files, you are running a huge risk if you restart nginx and you made a mistake, a typo, or copied and pasted from Stackoverflow wrong. Luckily there’s an easy built-in way to test your configuration files to make sure they won’t break everything.

The worst part about making a mistake in your config files is that nginx will stop and it won’t start back up again until you fix the problem. So anybody trying to visit your site will get a fun error page.

So first you’ll want to figure out where nginx is located, which you can always do with a quick ps -ef | grep nginx (or many other methods that produce similar output).

So now that we know where the binary is located, just simply run it with the -t argument.

/usr/local/nginx/sbin/nginx -t

If you made a mistake, you’ll get an error message that indicates it failed.

If it succeeded, you’ll get a message that everything is fine.

So now that you know your syntax is correct, you can restart nginx (gracefully) by using service nginx reload. Or… you could chain them together so the reload happens but only if the syntax was correct, using the && operator like this:

/usr/local/nginx/sbin/nginx -t && /usr/local/nginx/sbin/nginx -s reload

It’s worth noting that your nginx might be located in a different folder, so you should adjust the path accordingly.

Profile Photo for Lowell Heddings Lowell Heddings
Lowell is the founder and CEO of How-To Geek. He’s been running the show since creating the site back in 2006. Over the last decade, Lowell has personally written more than 1000 articles which have been viewed by over 250 million people. Prior to starting How-To Geek, Lowell spent 15 years working in IT doing consulting, cybersecurity, database management, and programming work.
Read Full Bio »