nginx日志不记录静态文件访问和缓存

nginx访问日志
nginx和apache的访问日志一样可以记录的指定信息,如记录服务器时间,访问的客户端ip、访问的url和访问状态码等信息,这些信息会规律的记录到访问日志中
主配置文件中定义的日志格式,记录的格式参数解释如下

$remote_addr                    客户端访问IP(公网IP)
$http_x_forwarded_for           记录代理服务器的IP
$time_local                     日志中服务器本地时间
$host                           记录客户端访问的主机名(域名)
$request_url                    记录访问的URL地址(域名后的路径信息)
$status                         状态码,记录客户端访问时返回的访问状态码,如200、302、404这些状态
$http_referer                   HTTP Referer是header的一部分,当浏览器向web服务器发送请求的时候,一般会带上Referer,告诉服务器我是从哪个页面链接过来的,服务器基此可以获得一些信息用于处理。
$http_user_agent                记录客户端访问浏览器的标识,可用于限制一些浏览器禁止访问,如网络爬虫的访问类型

在nginx主配置文件nginx.conf配置记录的日志类型,combined_realip是定义记录的引用名字,这个名称会在虚拟主机配置文件中用到。日志记录的格式是以单引号扩起来的,可以换行输入,这里nginx定义了虚拟主机配置文件,还需要在虚拟主机配置文件中定义日志的保存路径

    log_format zidingyi '$remote_addr $http_x_forwarded_for [$time_local]'
    ' $host "$request_uri" $status'
    ' "$http_referer" "$http_user_agent"';

配置完主配置文件后,还需要定义下日志记录信息的保存路径,这里的日志保存路径是分虚拟主机的,不同的虚拟主机可以把日志存放到不同的路径下,这样也方便区分,日志定义在server模块的下面

   if ($host != 'test.com') {
          rewrite ^/(.*)$ http://aaa.com/$1 permanent;
   }
      access_log /data/wwwroot/log/aaa.com.log zidingyi;
}
重新reload下nginx的配置,使用curl测试访问并查看记录的日志格式
[root@localhost conf]# /usr/local/nginx/sbin/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@localhost conf]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost conf]# curl -x127.0.0.1:80 ccc.com -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.15.2
Date: Tue, 14 Aug 2018 08:32:14 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://aaa.com/
[root@localhost conf]# curl -x127.0.0.1:80 ddd.com -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.15.2
Date: Tue, 14 Aug 2018 08:32:22 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://aaa.com/
[root@localhost conf]# cat /data/wwwroot/log/aaa.com.log 
127.0.0.1 - [14/Aug/2018:16:32:14 +0800] ccc.com "/" 301 "-" "curl/7.29.0"
127.0.0.1 - [14/Aug/2018:16:32:22 +0800] ddd.com "/" 301 "-" "curl/7.29.0"

nginx日志切割
nginx日志切割的话没有apache那种方便的工具,需要借助切割工具或者要么我们编写一个定时执行的清理脚本,脚本功能把日志文件每天定时移动重命名并重新让nginx生成新的日志文件(使用/bin/kill -HUP操作) ,把日志以日期的形式重命名存放,再使用find配合-exec查找并删除多少天之前的日志文件,并把执行脚本执行命令写入计划任务,每天凌晨执行一次,其切割脚本参考内容如下:

[root@localhost nginx]# vim /usr/local/sbin/nginx_logrotate.sh 
#!/bin/bash
date=`date -d "-1 day " +%Y%m%d`
logdir="/data/wwwroot/log/"
nginx_pid="/usr/local/nginx/logs/nginx.pid"
cd $logdir
for log in `ls *.log`
do
    mv $log $log-$date
done
/bin/kill -HUP `cat $nginx_pid`
find $logdir -name *.log-.* -type f -mtime +30 -exec rm -rf {} \;

日志切割后需要重新让nginx生成新的日志,这样nginx才能继续记录的日志
脚本中涉及for循环操作,有关shell循环语法请查阅其他相关资料

nginx不记录文件访问日志和静态文件过期时间
nginx和apache一样支持访问某些文件类型时URL中不记录其文件记录的日志信息,同样也支持静态文件在浏览器中缓存的过期时间,过期时间也是通过location来定义配置的,其配置文件内容如下,统配文件名称和以类型结尾来匹配:

[root@localhost nginx]# vim conf/vhost/aaa.conf
   if ($host != 'test.com') {
          rewrite ^/(.*)$ http://aaa.com/$1 permanent;
   }
      location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
               expires 7d;              ---------图片文件设置7天的过期时间
               access_log off;
        }
      location ~ .*\.(js|css)$
        {
               expires 8h;              -------设置8小时的过期时间
               access_log off;
        }
[root@localhost nginx]# /usr/local/nginx/sbin/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@localhost nginx]# /usr/local/nginx/sbin/nginx -s reload

nginx配置文件确认配置没有错误重启后,创建以css结尾的文件访问测试下日志是否记录

[root@localhost aaa]# curl -x127.0.0.1:80 -I aaa.com/index.php
HTTP/1.1 200 OK
Server: nginx/1.15.2
Date: Tue, 14 Aug 2018 10:29:00 GMT
Content-Type: application/octet-stream
Content-Length: 26
Last-Modified: Fri, 10 Aug 2018 17:28:28 GMT
Connection: keep-alive
ETag: "5b6dcb3c-1a"
Accept-Ranges: bytes                             --------访问php页面不进行静态文件缓存
[root@localhost aaa]# curl -x127.0.0.1:80 -I aaa.com/2.css
HTTP/1.1 200 OK
Server: nginx/1.15.2
Date: Tue, 14 Aug 2018 10:29:12 GMT
Content-Type: text/css
Content-Length: 11
Last-Modified: Tue, 14 Aug 2018 10:04:01 GMT
Connection: keep-alive
ETag: "5b72a911-b"
Expires: Tue, 14 Aug 2018 18:29:12 GMT
Cache-Control: max-age=28800
Accept-Ranges: bytes                              ----------这里访问css文件时cache的时间为28800秒/8小时
[root@localhost aaa]# curl -x127.0.0.1:80 -I aaa.com/1.gif
HTTP/1.1 200 OK
Server: nginx/1.15.2
Date: Tue, 14 Aug 2018 10:29:24 GMT
Content-Type: image/gif
Content-Length: 6
Last-Modified: Tue, 14 Aug 2018 10:11:07 GMT
Connection: keep-alive
ETag: "5b72aabb-6"
Expires: Tue, 21 Aug 2018 10:29:24 GMT
Cache-Control: max-age=604800
Accept-Ranges: bytes                             -----------图片文件过期时间问604800秒,即7天
[root@localhost aaa]# cat /data/wwwroot/log/aaa.com.log
127.0.0.1 - [14/Aug/2018:18:29:00 +0800] aaa.com "/index.php" 200 "-" "curl/7.29.0"

通过测试看出指定的文件被缓存到浏览器中,过期时间是服务器上指定的,如果再次访问该文件时则浏览器标识则会显示为304,而日志记录中不会对该类型的文件访问做记录

转载于:https://blog.51cto.com/8844414/2159968

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值