参考资料

  1. Nginx 基于SSL的TCP代理服务器详细说明以及案例
  2. Nginx的配置与管理
  3. Nginx监控工具(Prometheus)配置详细说明以及案例
  4. Nginx长连接负载均衡详细说明以及案例
  5. Nginx Logrotate:日志归档配置详细说明以及案例
  6. 如何设置Nginx的SSL/TLS加密?
  7. nginx 配置ssl证书
  8. Nginx Keepalived配置详细说明以及案例

检测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