Nginx - 用日期命名日志文件并实现自动切割

nginx 日志默认文件名:

  • access.log
  • error.log

为了方便查询及归档,将日志文件设置为按照日期来分割,配置如下:

[root@nginx /usr/local/nginx/conf]#cat location.conf 
map $time_iso8601 $logdate {
    default 'date-not-found';
    '~^(?<ymd>\d{4}-\d{2}-\d{2})' $ymd;
}

map 指令是由 ngx_http_map_module 模块提供的,默认情况下安装 nginx 都会安装该模块。

上面这配置文件使用关键字 map 来定义一个变量 $logdate

如果 nginx 内置变量 $time_iso8601 通过正则能匹配到则 获取到 $logdate = $ymd 否则 $logdate = 'date-not-found'

接下来,在主配置文件中引用:

...
http {
    include       mime.types;
    include       location.conf;
    default_type  application/octet-stream;

    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  logs/access-$logdate.log  main;
    ...

重载 nginx

[root@nginx /usr/local/nginx/conf]#nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@nginx /usr/local/nginx/conf]#nginx -s reload

注意:reload后日志文件不会马上生成,因为日期是通过 $time_iso8601  内置变量获取的。需要访问一下才会生成日志文件。

[root@nginx ~]#curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@nginx ~]#ls /usr/local/nginx/logs/
access.log  error.log  nginx.pid

当访问触发以后,如果还没有生成日期格式的日志文件,则检查下 nginx 运行用户及目录属主属组!

[root@nginx ~]#ll /usr/local/nginx/
total 4
drwx------ 2 root root    6 Nov 26  2016 client_body_temp
drwxr-xr-x 2 root root 4096 Jul 30 16:27 conf
drwx------ 2 root root    6 Nov 26  2016 fastcgi_temp
drwxr-xr-x 2 root root   40 Nov 26  2016 html
drwxr-xr-x 2 root root   87 Jul 30 16:20 logs
drwx------ 2 root root    6 Nov 26  2016 proxy_temp
drwxr-xr-x 2 root root   19 Nov 26  2016 sbin
drwx------ 2 root root    6 Nov 26  2016 scgi_temp
drwx------ 2 root root    6 Nov 26  2016 uwsgi_temp

[root@nginx ~]#ps -ef | egrep nginx
root     12916     1  0 16:20 ?        00:00:00 nginx: master process nginx
nginx    12933 12916  0 16:27 ?        00:00:00 nginx: worker process
root     12966 12879  0 17:47 pts/0    00:00:00 grep -E --color=auto nginx

当访问触发以后,如果还没有生成日期格式的日志文件,则检查下 nginx 运行用户及目录属主属组!

[root@nginx ~]#chown -R nginx:root /usr/local/nginx/
[root@nginx ~]#ll /usr/local/nginx/
total 4
drwx------ 2 nginx root    6 Nov 26  2016 client_body_temp
drwxr-xr-x 2 nginx root 4096 Jul 30 16:27 conf
drwx------ 2 nginx root    6 Nov 26  2016 fastcgi_temp
drwxr-xr-x 2 nginx root   40 Nov 26  2016 html
drwxr-xr-x 2 nginx root   87 Jul 30 16:20 logs
drwx------ 2 nginx root    6 Nov 26  2016 proxy_temp
drwxr-xr-x 2 nginx root   19 Nov 26  2016 sbin
drwx------ 2 nginx root    6 Nov 26  2016 scgi_temp
drwx------ 2 nginx root    6 Nov 26  2016 uwsgi_temp

# 再次访问,生成日志文件
[root@nginx ~]#curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>


[root@nginx ~]#ll /usr/local/nginx/logs/
total 12
-rw-r--r-- 1 nginx root 270 Jul 30 17:48 access-2020-07-30.log
-rw-r--r-- 1 nginx root   0 Nov 26  2016 access.log
-rw-r--r-- 1 nginx root 412 Jul 30 16:27 error.log
-rw-r--r-- 1 nginx root   6 Jul 30 16:20 nginx.pid

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值