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

最近阿里云上运行的网站都无端各种的 502 Bad Gateway,相当的蛋疼,于是一咬牙重装了LNMP,各种问题解决中……

重启 LNMP 的时候提示如下:

nginx: [warn] "log_format" directive used only on "http" level

首先查看一下 nginx 版本信息

# /usr/local/nginx/sbin/nginx -V

返回:nginx version: nginx/1.6.0

在 nginx 1.6.0 版本中,日志格式化部分有点小变化,在 nginx.conf 中的 http 层内部、server 层外部统一规定出来:

http{
……

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

server{……}
}

log_format 在 nginx.conf 的http层定义后,各域名下面就不用再定义log_format了。

在 vhost/*.conf 的域名中 server 中直接写:

access_log ./logs/blog.log access;

注:log_format 与 access_log 中的 access 要对应,否则会报错:

nginx: [emerg] unknown log format "abc" in /usr/local/nginx/conf/vhost/yangjunwei.conf:33