服务器 
首页 > 服务器 > 浏览文章

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

(编辑:jimmy 日期: 2025/3/2 浏览:3 次 )

将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的

上一篇:nginx下gzip配置参数详解
下一篇:nginx禁止访问.git文件的设置教程
一句话新闻
Windows上运行安卓你用过了吗
在去年的5月23日,借助Intel Bridge Technology以及Intel Celadon两项技术的驱动,Intel为PC用户带来了Android On Windows(AOW)平台,并携手国内软件公司腾讯共同推出了腾讯应用宝电脑版,将Windows与安卓两大生态进行了融合,PC的使用体验随即被带入到了一个全新的阶段。