参考资料

  1. nginx -g详细说明以及案例
  2. Nginx集群负载(基于LVS和Keepalived)搭建详细说明以及案例
  3. nginx -e详细说明以及案例
  4. PHP-FPM 性能优化
  5. Nginx反向代理缓存服务器配置详细说明以及案例
  6. Nginx配置修改工具Ansible详细说明以及案例
  7. nginx 配置https
  8. 如何配置Nginx反向代理?

检测Nginx配置文件 nginx -t

nginx -t 命令用于测试 Nginx 配置文件的语法是否正确,并检查配置文件路径是否正确。该命令不会重新加载或重启 Nginx 服务,仅用于验证配置文件的正确性。

命令格式

nginx -t

详细说明

  1. 语法检查:Nginx 会检查配置文件的语法是否正确。

  2. 路径检查:Nginx 会检查配置文件中指定的路径是否存在且可访问。

  3. 输出结果

    • 如果配置文件正确,输出 syntax is oktest is successful

    • 如果配置文件有错误,输出具体的错误信息。

案例

假设 Nginx 配置文件路径为 /etc/nginx/nginx.conf

  1. 配置文件正确的情况

    $ nginx -t
    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/nginx.conf test is successful
  2. 配置文件有错误的情况

    $ nginx -t
    nginx: [emerg] unknown directive "server_namee" in /etc/nginx/nginx.conf:10
    nginx: configuration file /etc/nginx/nginx.conf test failed

注意事项

  • 确保以 root 用户或具有足够权限的用户执行该命令。

  • 如果配置文件路径不是默认的 /etc/nginx/nginx.conf,可以使用 -c 选项指定配置文件路径:

    nginx -t -c /path/to/your/nginx.conf