Carry の Blog Carry の Blog
首页
  • Nginx
  • Prometheus
  • Iptables
  • Systemd
  • Firewalld
  • Docker
  • Sshd
  • DBA工作笔记
  • MySQL
  • Redis
  • TiDB
  • Elasticsearch
  • Python
  • Shell
  • MySQL8-SOP手册
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Carry の Blog

好记性不如烂键盘
首页
  • Nginx
  • Prometheus
  • Iptables
  • Systemd
  • Firewalld
  • Docker
  • Sshd
  • DBA工作笔记
  • MySQL
  • Redis
  • TiDB
  • Elasticsearch
  • Python
  • Shell
  • MySQL8-SOP手册
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 工作笔记

  • K8S

  • Systemd

  • Nginx

    • Nginx添加用户认证
    • 利用nginx+sftp实现一个可供用户下载的服务
    • nginx配置文件及模块
    • 通过脚本按天切割nginx的日志
    • nginx通过四层代理实现端口转发
    • NGINX基于cookie针对同一域名进行分流转发
    • nginx利用内置模块配置限速限流
    • 利用NGINX内置模块mirror进行流量复制等操作
    • 使用$remote_user字段记录访问NGINX的用户
    • 从NGINX自身配置文件中定义访问日志按时间切割
    • NGINX配置单独代理百度的sitemap文件
    • nginx配置微信小程序校验及其他
    • nginx配置gzip压缩
    • 由Nginx集中代理分散的PHP集群的实践
    • http状态码详解
    • OpenResty-1-13-6-2-新增ldap模块儿
    • 排查NGINX的open_file_cache导致发布后访问404的问题
    • 制作OpenResty-1-19-9-1的RPM包
    • nginx-proxy-managar使用笔记
    • Nginx 的端口复用:提升服务器并发能力
  • Supervisord

  • OpenLdap

  • OpenVPN

  • GitLab

  • Sshd

  • WebDev

  • Docker

  • Prometheus

  • Rclone

  • Iptables

  • Firewalld

  • Linux笔记
  • Nginx
Carry の Blog
2019-12-18

从NGINX自身配置文件中定义访问日志按时间切割收藏(二丫讲梵)备查

以往NGINX日志不会进行切割的操作,而都是把切割的工作交给logrotate来做了,这没啥问题,但是如果遇到NGINX是容器来跑的,日志只是挂载出来的情况,就有点不科学了,毕竟logrotate在切割日志的时候还需要发一个平滑滚动的信号给 NGINX 进程。

那么这里就分享基于 NGINX 自身配置文件来进行日志的切割工作:

if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})") {
    set $year $1;
    set $month $2;
    set $day $3;
}
access_log /var/log/nginx/$year-$month-$day-access.log;
1
2
3
4
5
6

以上配置基于 $time_iso8601这一时间戳取出时间变量,从而满足自由定义的方式,生成的日志将会按天自动进行切割。

img

如果精确到秒,可以用如下配置:

if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})") {
    set $year $1;
    set $month $2;
    set $day $3;
    set $hour $4;
    set $minutes $5;
    set $seconds $6;
}
access_log /data/log/test/nginx-access-$year-$month-$day-$hour-$minutes-$seconds.log json;
1
2
3
4
5
6
7
8
9

这样就已经可以实现需求了,还有 Perl 的表现方式,这里就不讲解了。

注意:这里的 if 语句只能放到 server 区块下,所以不能直接放在全局引用。

于是,可以先创建一个日志格式化专用的配置文件:

$ vim /usr/local/nginx/conf/log_format.conf
log_format json escape=json '{"remote_addr": "$remote_addr",'
                                 '"@timestamp": "$time_iso8601",'
                                 '"request_uri": "$request_uri",'
                                 '"verb": "$request_method",'
                                 '"httpversion": "$server_protocol",'
                                 '"response": "$status", '
                                 '"body_bytes_sent": "$body_bytes_sent", '
                                 '"referrer": "$http_referer", '
                                 '"user_agent": "$http_user_agent", '
                                 '"http_x_forwarded_for": "$http_x_forwarded_for", '
                                 '"server_name": "$host",'
                                 '"request_time": "$request_time",'
                                 '"upstream_response_time": "$upstream_response_time",'
                                 '"realpath_root": "$realpath_root",'
                                 '"request_body": "$request_body",'
                                 '"nginx_version": "$nginx_version",'
                                 '"scheme": "$scheme"}';
if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})") {
        set $year $1;
        set $month $2;
        set $day $3;
        set $hour $4;
}
access_log /home/nginx/logs/${server_name}-${year}-${month}-${day}-${hour}_access.log json;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

然后再在配置当中引用进来:

$ cat doc.conf
server {
    listen       80;
    server_name  doc.eryajf.net;
    charset utf-8;
    include log_format.conf;
    location / {
        try_files /_not_exists_ @backend;
    }
    location @backend {
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host            $http_host;
        proxy_set_header   X-Forwarded-Proto $scheme;
        proxy_pass http://127.0.0.1:8180;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

这样日志就会根据不同域名,按每小时一个文件的进行分割了。

注意:如果配置之后日志没有新生成,检查一下 NGINX 进程是否有对应目录的写入权限,并请求一下 NGINX,应该就会有日志产生了。

#nginx
上次更新: 4/24/2025

← 使用$remote_user字段记录访问NGINX的用户 NGINX配置单独代理百度的sitemap文件→

最近更新
01
tidb fast ddl
04-04
02
TiDB配置文件调优 原创
04-03
03
如何移除TiDB中的表分区 原创
04-03
更多文章>
Theme by Vdoing
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式