关于Nginx升级后提示日志格式错误的解决方法

Nginx升级到1.1.1*版本后

会出现如下警告

nginx: [warn] the "log_format" directive may be used only on "http" level in /etc/nginx/nginx.conf:84

主要是Nginx新版本的日志声明代码位置发生了变化,原来旧版本中,需要在每一段虚拟主机server里写如下日志格式声明

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

新版本只需要将以上代码移动到http层级里,将nginx.conf里如下代码代移到http花括号内即可。

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

再测试一下,没问题了

[root@test01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
分享

TITLE: 关于Nginx升级后提示日志格式错误的解决方法

LINK: https://www.qttc.net/15-nginx-upgrade-log-format-error.html

NOTE: 原创内容,转载请注明出自琼台博客