How to Test NGINX Config
It’s important to test your NGINX configuration to prevent downtime. If you make a syntax error, your restart will fail and your site will go down. Fortunately it’s really easy to test your configuration before reloading.
Test Default Config
> nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
If NGINX detects an error in your configuration file, you will get output like the following:
> nginx -t nginx: [emerg] unexpected "}" in /etc/nginx/nginx.conf:119 nginx: configuration file /etc/nginx/nginx.conf test failed
This error tells us there is something unexpected at /etc/nginx/nginx.conf on line 119. If we open that file we should be able to track down the issue and try again.
There are few things to check when you get an error:
- Missing braces: This is one of the most common issues. Ensure each block has an opening and closing brace. For example, ensure your server block starts with a brace and ends with one.
- Missing semicolon: Each of your directives (e.g. server_name) should end with a semicolon. It’s just like writing C code.
Testing Custom Config File
You can also test config files in non-standard locations. This can be especially helpful if you’re running Docker. Simply pass the “-c” option to your NGINX command. See below for an example:
> nginx -t -c /opt/mynginx.conf nginx: the configuration file /opt/mynginx.conf syntax is ok nginx: configuration file /opt/mynginx.conf test is successful