nginx.conf 中的 log_format 被忽略

问题描述 投票:0回答:4

这里是 NGINX 初学者。

我的日志目前如下所示:

92.21.236.47 - - [08/Jan/2017:00:48:10 +0000] "GET / HTTP/1.1" 200 148 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:50.0) Gecko/ 20100101 火狐/50.0"

当我在默认的 /etc/nginx/nginx.conf 中添加以下行时

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

access_log /var/log/nginx/access.log; 
error_log /var/log/nginx/error.log;

(默认配置中已经存在 access_log 和 error_log 行,我将它们放在这里仅用于上下文)。

然后我用以下命令重新启动 NGINX:

systemctl restart nginx

我现在希望我的日志能够更改,特别是显示使用的 xxx 文字值 .. xxx[$time_local]xxx .. 但我的更改没有任何区别。

如果我将 log_format main 更改为 log_format合并 那么服务器将不会重新启动。

nginx
4个回答
42
投票

改变..

access_log /var/log/nginx/access.log;

到..

access_log /var/log/nginx/access.log main;

修好了。

其中 main 是 ..

处定义的 log_format 的名称
log_format main '$remote_addr - $remote_user xxx[$time_local]xxx '
                '"$request" $status $body_bytes_sent '
                '"$http_referer" "$http_user_agent"';

19
投票

回复已经晚了,但对于那些仍然困扰这个问题的人来说可能会有用。

log_format
 之外编写 
server {}

指令

整个代码是这样的

log_format main '$remote_addr - $remote_user xxx[$time_local]xxx '
                '"$request" $status $body_bytes_sent '
                '"$http_referer" "$http_user_agent"';
server {
      listen 80;
      access_log /var/log/nginx/<your_domain>_access.log main; 
}

这里main表示nginx在写入日志时应该包含哪些字段。

更详细的信息请查看官方文档 使用 NGINX 条件日志记录采样请求


0
投票

以上2个答案已尝试但没有帮助。我意识到

在我的 nginx.conf 中的

log_format
行上方,
server
支架未闭合。 它给了我同样的错误。


0
投票

就我而言,最终发现我的 nginx 使用了不同的配置文件:

/usr/local/nginx/conf/nginx.conf
进行了更改,但实际上
/usr/local/nginx/nginx.conf
正在使用中。

© www.soinside.com 2019 - 2024. All rights reserved.