nginx 如果在server和http段都写access_log,那么http段的access_log就不记录内容吗?

nginx 如果在server段写了access_log,再在http段写access_log,那么http段的access_log就不记录内容吗?

http段

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                       '"$http_user_agent" "$http_x_forwarded_for"';
 access_log  /home/logs/nginx/access.log main;

server 段

    error_log        /home/hosts_log/2_error.log notice;
    access_log        /home/hosts_log/2_access.log lu_access_log_1 buffer=32k;

每次重启后会生成空的/home/logs/nginx/access.log,但是无论怎么刷新页面,内容都是空。server段的配置就生效。

阅读 7.4k
2 个回答

参考官方文档即可:
https://nginx.org/r/access_log

access_log 可以使用的范围:http, server, location, if in location, limit_except

比如这个配置:

http {
    access_log a.log;
    server {
        access_log b.log;
        location /xx {
            access_log c.log;
            if yy {
                access_log d.log;
            }
        }
        location /zz {
            access_log off;
        }
    }
}

请求最终匹配到哪个 {} block,这个 block 配置了 access_log,就会覆盖上一级的 access_log

  1. 符合 if 的条件,则请求日志写到 d.log
  2. 不符合 if, 那 /xx 开头的请求会写日志到 c.log
  3. 匹配这个 server 的其他日志写到 b.log
  4. 其余日志写到 a.log
  5. 还可以对部分请求关闭日志

对的,nginx的底层配置文件分为三段,http_conf,srv_conf,loc_conf,最终是要合并在一起的,你可以这样理解,如果你想深入了解这一部分的内容,可以看一下深入理解Nginx模块开发与架构解析第二版,里面有这一部分的内容

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题