Home  >  Article  >  Backend Development  >  Nginx common log splitting methods nginx apache nginx php nginx rewrite

Nginx common log splitting methods nginx apache nginx php nginx rewrite

WBOY
WBOYOriginal
2016-07-29 08:55:151321browse
方式一:
nginx cronolog日志分割配置文档,根据下面方法,每分钟分割一次NGINX访问日志。

<span>1</span>.nginx日志配置
 access_log access_log /data/access_log_pipe main;
<span>2</span>.先创建一个命名管道
mkfifo /www/<span>log</span>/access_log_pipe
<span>3</span>.配置cronolog:
nohup cat /data/access_log_pipe | <span>/usr/local</span><span>/sbin/cronolog</span> /data/<span>log</span>/domain.access<span>_</span><span>%Y</span><span>%m</span><span>%d</span><span>%H</span><span>%M</span>.<span>log</span> &
<span>4</span>.启动Nginx
/usr/<span>local</span>/nginx/sbin/nginx

注意:
cronolog必须在nginx启动前启动
没有安装cronolog的话,需要先安装
wget http:<span>//cronolog</span>.org/download/cronolog-<span>1.6</span>.<span>2</span>.tar.gz
tar zxvf cronolog-<span>1.6</span>.<span>2</span>.tar.gz
cd cronolog-<span>1.6</span>.<span>2</span>
.<span>/configure
 make
make install
方式二:

定时任务中每小时添加定时任务,执行一下脚本,可以实现小时日志分割
log_dir="/var</span><span>/log/nginx</span><span>"
date_dir=`date +<span>%Y</span>/<span>%m</span>/<span>%d</span>/<span>%H</span>`
/bin/mkdir -p <span>${log_dir}</span>/<span>${date_dir}</span> > /dev/null 2>&1
/bin/mv <span>${log_dir}</span>/access.log   <span>${log_dir}</span>/<span>${date_dir}</span>/access.log
kill -USR1 `cat /opt/nginx/logs/nginx.pid`

方式三:
使用logrotate做nginx日志轮询
logrotate看名字就知道是专门做日志轮询的,只把任务配置放在/etc/logrotate.d/下,任务就会自动完成,而且无需安装,系统自带,比较推荐使用.
vi /etc/logrotate.d/nginx  

    /usr/local/nginx/logs/www.willko.cn.log /usr/local/nginx/logs/nginx_error.log {
    notifempty
    daily
    sharedscripts
    postrotate
    /bin/kill -USR1 `/bin/cat /usr/local/nginx/nginx.pid`
    endscript
    } 

多个日志以空格分开,
notifempty 如果日志为空则不做轮询
daily 每天执行一次
postrotate 日志轮询后执行的脚本
这样,每天都会自动轮询,生成nginx.log.1-n
 后话:

用Jmeter对方法一进行了测试,新建线程组:线程数200,循环150,新建http请求,请求nginx上的图片,请求30000次,失败326次,后台形成5个文件,行数如下:

   2868 access_201112182030.log
   7777 access_201112182031.log
   7626 access_201112182032.log
   7873 access_201112182033.log
   3530 access_201112182034.log
  29674 total

并没有因为切分文件而丢失日志。</span>

The above introduces the common log splitting methods of Nginx, including nginx content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn