一聚教程网:一个值得你收藏的教程网站

热门教程

nginx1.2.4: [warn] "log_format" directive used only on "http" level 解决方法

时间:2022-06-30 18:48:46 编辑:袖梨 来源:一聚教程网

nginx升级到1.2.4稳定版之后,会发现之前的vhost/*.conf中的日志配置都报了如下的warn:

nginx: [warn] the “log_format” directive may be used only on “http” level

上网搜索解决方案如下:

将/vhost/xxx.conf里server段里的下面代码移出该server段即可。

但是这样的又会产生一个问题,就是各子域名的日志文件都会记录所有请求的日志,等了好久都没找到解决方案,后来请教了飞飞之后终于找到解决的方法了。

原来log_format需要在nginx.conf的http层定义,然后在分域名下面就不用定义log_format,直接引用即可,即:
在nginx.conf中http层添加:

 

 代码如下 复制代码
log_format Main ‘$remote_addr – $remote_user [$time_local] “$request” ‘
‘$status $body_bytes_sent “$http_referer” ‘
‘”$http_user_agent” $http_x_forwarded_for $request_time’;

然后在vhost/*.conf中server中直接写:

 代码如下 复制代码

access_log ./logs/blog.log Main;

但是注意include vhost/*.conf要放在log_format之后哦,不然会找不到Main的

参考文档:http://wiki.nginx.org/NginxHttpLogModule#open_log_file_cache又得到一个答案

解决方法:

将/usr/local/nginx/conf/nginx.conf 里server段里的下面代码移出放到该server段的前面即可。
 

 代码如下 复制代码

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

如果有其的虚拟主机开启了日志,也按上面的要求移出server段放在server段的前面即可。
再/usr/local/nginx/sbin/nginx -t 测试一下,没有warn警告信息了。

nginx提示的很明显了,要放到 http 层,而不是server层里

热门栏目