nginx访问日志

查看nginx.conf文件

[root@KevinfeideMacBook ~]# vim /usr/local/nginx/conf/nginx.conf

中间有一行是定义log的格式

log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
    ' $host "$request_uri" $status'
    ' "$http_referer" "$http_user_agent"';
其中’’包围起来的不算一行,只是为了美观才分行显示,分行显示就必须加上’’号;
含义:

combined_realip:定义日志格式别名,默认为combined_realip,这里我会改为test001;
$remote_addr:客户端ip也就是出口公网ip;
$http_x_forwarded_for:代理服务器ip;
$time_local:服务器本地时间;
$host:访问主机名(域名);
$request_uri:访问的url地址;
$status:状态码,比如404;
$http_referer:referer;
$http_user_agent:也就是访问的浏览器类型,比如傲游7;

编辑虚拟web配置

[root@KevinfeideMacBook ~]# vim /usr/local/nginx/conf/vhost/test.com.conf

增加access_log /tmp/test.com.log combined_realip;
其中combined_realip是定义名称,记得与nginx.conf匹配;

nginx检错与重新加载
[root@KevinfeideMacBook ~]# /usr/local/nginx/sbin/nginx -t
[root@KevinfeideMacBook ~]# /usr/local/nginx/sbin/nginx -s reload
访问与测试
[root@KevinfeideMacBook ~]# curl -x127.0.0.1:80 test.com/1.html -I

访问成功200

查看日志
[root@KevinfeideMacBook ~]# cat /tmp/test.com.log
127.0.0.1 - [15/Mar/2018:03:13:28 +0800] test.com "/1.html" 200 "-" "curl/7.29.0"
日志切割

创建shell脚本

[root@KevinfeideMacBook ~]# vim /usr/local/sbin/nginx_log_rotate.sh
#! /bin/bash
假设nginx的日志存放路径为/tmp/
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`
do
    mv $log $log-$d
done
/bin/kill -HUP `cat $nginx_pid`
执行脚本(可不执行)
sh -x /usr/local/sbin/nginx_log_rotate.sh

[root@KevinfeideMacBook ~]# sh -x /usr/local/sbin/nginx_log_rotate.sh

++ date -d '-1 day' +%Y%m%d
+ d=20180314
+ logdir=/tmp/
+ nginx_pid=/usr/local/nginx/logs/nginx.pid
+ cd /tmp/
++ ls php_errors.log test.com.log
+ for log in '`ls *.log`'
+ mv php_errors.log php_errors.log-20180314
+ for log in '`ls *.log`'
+ mv test.com.log test.com.log-20180314
++ cat /usr/local/nginx/logs/nginx.pid
+ /bin/kill -HUP 1574
任务计划执行脚本
[root@KevinfeideMacBook ~]#  crontab -e
写入脚本

0 0 * /bin/bash /usr/local/sbin/nginx_log_rotate.sh
意思:每天凌晨0点执行脚本/usr/local/sbin/nginx_log_rotate.sh

如何脚本删除log

静态文件不记录日志和过期时间和编辑web虚拟文件

[root@KevinfeideMacBook ~]# find /tmp/ -name *.log-* -type f -mtime +30 |xargs rm
[root@KevinfeideMacBook ~]# 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;
    }
所有代码预览
server
{
    listen 80;
    server_name test.comtest1.comtest2.com;
    index index.html index.htm index.php;
    root /data/wwwroot/test.com;
    if ($host != 'test.com' ) {
        rewrite  ^/(.*)$  http://test.com/$1  permanent;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
          expires      7d;
          access_log off;
    }
    location ~ .*\.(js|css)$
    {
          expires      12h;
          access_log off;
    }
    access_log /tmp/test.com.log test;
}

检错与重新加载

[root@KevinfeideMacBook ~]# /usr/local/nginx/sbin/nginx -t
[root@KevinfeideMacBook ~]# /usr/local/nginx/sbin/nginx -s reload