Skip to content

检测Nginx配置文件 nginx -t

如果通过命令行管理Nginx服务器,nginx -t 应该是使用频率最高的一个命令了。

主要使用场景:

  1. 配置文件修改后,用来检测所修改的配置文件是否有语法错误,一定要确保nginx -t返回成功了,再去加载配置文件,否则服务无法启动。
  2. 新接手一台Nginx服务器,不知道人家咋安装的,可以使用nginx -t试试,输出里面有主配置文件路径,顺着就能找到所有信息。

检测配置文件后退出 nginx -t

输出主配置文件完整路径,并返回检测结果。

示例:

shell
root@ecs-ms-01:/# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

检测配置文件后输出 nginx -T

输出主配置文件完整路径,并返回检测结果。还会输出配置文件内容到控制台。

从主配置文件 /etc/nginx/nginx.conf开始,把所有配置文件中的内容,按文件全部输出。

如果配置很多,输出内容也会很多,相当于一个配置文件内容的预览,还不如去看文件方便。

一般用不上这个大写-T,用小写的-t就可以。

示例:

root@ecs-ms-01:/# nginx -T
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
# configuration file /etc/nginx/nginx.conf:
user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;

worker_rlimit_nofile 65535;
events {
    worker_connections  65535;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    server_tokens off;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile       on;
    tcp_nopush     on;
    keepalive_timeout  65;
    client_max_body_size 50m;
    gzip on;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    include /etc/nginx/conf.d/*.conf;
}
# configuration file 更多文件,内容太多,没有全贴出来

所有的include文件,都会通过# configuration file <path=文件绝对路径> 换行后跟文件内容的方式,完整输出。