Home > Notes > Nginx访问记录的配置和分割

Nginx访问记录的配置和分割

Mon, 15 Jun 2009 05:27:48 +0800

Nginx目前不支持pipe输出日志文件,所以我们没法像用cronlog分割apache的日志那样来处理。
不过我们还有另一种方法来进行分割:即使用脚本来移动日志文件。以下脚本是shell脚本:

nginx_log_cron.sh

#!/bin/bash

site_log="/logs/www.lewphee.com/"
#创建当月目录,我使用这样的格式/sitelog/YYYYMM/access_YYYY-MM-DD.log
mkdir -p ${site_log}$(date -d "yesterday" +"%Y")$(date -d "yesterday" +"%m")/
mv ${site_log}access.log ${site_log}$(date -d "yesterday" +"%Y")$(date -d "yesterday" +"%m")/access_$(date -d "yesterday" +"%Y-%m-%d").log
kill -USR1 `cat /usr/local/nginx/sbin/nginx.pid`

在Nginx方面,我们设置日志输出:
logformat main '$remote_addr - $remote_user [$time_local] "$request" '
  '$status $body_bytes_sent "$http_referer" '
  '"$http_user_agent" "$http_x_forwarded_for"';

access_log /logs/www.lewphee.com/access.log main;

把脚本存在一个地方,比如/logs/nginx_log_cron.sh
把它设置为每天0时进行分割
crontab -e
00 00 * * * (/bin/bash /logs/nginx_log_cron.sh)

Tag:UNIX, WEB
Hots
评论
发表评论:


  [TIPS:首次发表评论的朋友,需要验证]
Tags
Recent Post
Recent Comments
Links
Copyright Notes
You can reship all of these articles without permission but MUST mark the original link in your post. Please contact with me() if u have advice or other arrangements.
Copyright©2007-2011 lewphee.com All rights reserved.