0
0
NginxHow-ToBeginner · 3 min read

How to Use nginx -t to Test Configuration Files

Use the nginx -t command to test your nginx configuration files for syntax errors without starting the server. This command checks the configuration and reports if it is valid or if there are errors that need fixing.
📐

Syntax

The basic syntax of the nginx -t command is:

  • nginx -t: Tests the default nginx configuration file.
  • nginx -t -c /path/to/nginx.conf: Tests a specific configuration file.
  • nginx -t -g 'directive': Tests configuration with additional directives.

This command does not start nginx; it only checks the configuration files for errors.

bash
nginx -t
nginx -t -c /etc/nginx/nginx.conf
nginx -t -g 'daemon off;'
💻

Example

This example shows how to test the default nginx configuration file for syntax errors. It helps ensure your configuration is correct before restarting nginx.

bash
sudo nginx -t
Output
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
⚠️

Common Pitfalls

Common mistakes when using nginx -t include:

  • Running the command without sufficient permissions, causing permission denied errors.
  • Not specifying the correct configuration file path if you use a custom config.
  • Ignoring error messages that indicate syntax or path problems.

Always run nginx -t with sudo if your nginx config files require root access.

bash
nginx -t
# May fail if no permission

sudo nginx -t
# Correct way to test with root permissions
📊

Quick Reference

Use this quick reference to remember key points about nginx -t:

  • Purpose: Check nginx config syntax without starting the server.
  • Run as root: Use sudo if needed.
  • Custom config: Use -c to specify config file.
  • Output: Shows syntax OK or error details.

Key Takeaways

Always run nginx -t to verify config syntax before restarting nginx.
Use sudo if your config files require root permissions.
Specify a custom config file with -c /path/to/file if needed.
Read error messages carefully to fix configuration issues.
The command does not start nginx; it only tests the configuration.