一、Nginx访问日志

日志格式

  vim /usr/local/nginx/conf/nginx.conf          //搜索log_format
	

combined_realip //规则名字

  • 除了在主配置文件nginx.conf里定义日志格式外,还需要在虚拟主机配置文件中增加
vim /usr/local/nginx/conf/vhost/test.com.conf

添加  access_log /tmp/1.log combined_realip;

  • 这里的combined_realip就是在nginx.conf中定义的日志格式名字

/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload
 curl -x127.0.0.1:80 test.com -I
 cat /tmp/1.log

二、Nginx日志切割

  • 自定义shell 脚本
 vim /usr/local/sbin/nginx_log_rotate.sh           //写入如下内容

#! /bin/bash
d=`date -d "-1 day" +%Y%m%d` 
logdir="/tmp/"
nginx_pid="/usr/local/nginx/logs/nginx.pid"
cd $logdir
for log in `ls *.log`    //for后面的log是一个变量名,
do
    mv $log $log-$d
done
/bin/kill -HUP `cat $nginx_pid`

  • 任务计划
 crontab -e
 

添加

 0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh  //每天零点切割日志
 

三、静态文件不记录日志和过期时间

vim /usr/local/nginx/conf/vhost/test.com.conf

添加配置如下

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
          expires      7d;  //有效期
          access_log off;
    }
location ~ .*\.(js|css)$
    {
          expires      12h; //有效期
          access_log off;
    }

/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload
echo  "good good study" > /data/wwwroot/test.com/1.jpg
echo  "day day up" > /data/wwwroot/test.com/666.css
curl -x127.0.0.1:80 test.com/1.jpg
curl -x127.0.0.1:80 test.com/666.css
curl -x127.0.0.1:80 test.com/index.html
cat /tmp/1.log

//jpg文件和css文件的访问没有记录log