想檢查一個配置文件是否正確,-c 指定之后發現有報錯,如下:
[root@op-2:~# nginx -t -c /etc/nginx/conf.d/default.conf nginx: [emerg] "server" directive is not allowed here in /etc/nginx/conf.d/default.conf:1 nginx: configuration file /etc/nginx/conf.d/default.conf test failed
有時候文件是正確無誤,但是也會報錯。
實際問題是進行語法檢測的對象有問題;
要檢測現有的修改過的Nginx配置是否有錯誤,不是只檢測 .conf文件,而是不管任何時候,始終都是去檢測主文件/etc/nginx/nginx.conf,只有這樣,才能順利的在對應的模塊加載.conf 文件。
這樣保證了配置的前后語境的正確性,這樣才是真正的檢測。
所以正確的檢測修改的Nginx的語法是否錯誤的命令應該是: nginx -t -c /etc/nginx/nginx.conf
,如果配置文件有異常,會直接報出,否則就是
[root@op-2:~# nginx -t -c /etc/nginx/nginx.conf
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@op-2:~# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful