侧边栏壁纸
博主头像
Awesome Devin 博主等级

行动起来,活在当下

  • 累计撰写 345 篇文章
  • 累计创建 26 个标签
  • 累计收到 3 条评论

目 录CONTENT

文章目录

nginx access log日志分割

Administrator
2026-01-16 / 0 评论 / 0 点赞 / 3 阅读 / 0 字

问题

access.log 大的网络访问下没有几天文件就变得非常大了,一直累计也不是办法啊。单个文件达到了20g。

解决方案

按照时间每天进行切割

找到nginx.conf

在http添加这部分

	map $time_iso8601 $logdate {
	'~^(?<ymd>\d{4}-\d{2}-\d{2})' $ymd;
	default                       'date-not-found';
	}
	access_log  logs/access-$logdate.log ;

http部分如下

http {


    #log_format main    ' "<$http_Cdn_Src_IP>" "{$http_x_real_ip}" "[$http_x_forwarded_for]" "$remote_addr" '
    #                   '$http_user_agent - $remote_user [$time_local] "$request" '
    #                   ' $status $body_bytes_sent "$http_referer" ';
    #access_log  logs/access.log  main;
    #按照日期分割log

        map $time_iso8601 $logdate {
        '~^(?<ymd>\d{4}-\d{2}-\d{2})' $ymd;
        default                       'date-not-found';
        }
        access_log  logs/access-$logdate.log;

    include       mime.types;
    default_type  application/octet-stream;
    proxy_headers_hash_max_size 51200;
    proxy_headers_hash_bucket_size 6400;

    proxy_buffer_size 128k;
    proxy_buffers   32 128k;
    proxy_busy_buffers_size 128k;

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



    sendfile        on;
    tcp_nopush     on;

    client_max_body_size 20m;
    #keepalive_timeout  0;
    keepalive_timeout  65;
    server_tokens off;
    include front/*.conf;
...

实现效果

[root@devin-test logs]#ll
total 71536
-rw-r--r-- 1 root root  3128249 Jan 16 10:55 access-2026-01-16.log
-rw-r--r-- 1 root root  2366228 Jan 16 10:52 access.log
-rw-r--r-- 1 root root 67743002 Jan 15 18:11 error.log

参考链接:
https://blog.csdn.net/wochunyang/article/details/132638911

0
博主关闭了所有页面的评论