首页 > 技术文章 > LNMP-Nginx配置不记录静态文件、过期时间

douyi 2019-10-08 16:36 原文

用户访问web网站,通常日志文件会记录很多web站点上的一些静态文件信息,如果长期不处理,日志文件会越来越大,占用的系统资源也越大,此时就需要我们配置不记录静态文件和过期时间,减少日志文件记录过多不必要的内容信息和系统资源占用。
 
一、配置
 
1:编写conf文件
[root@host ~]# vim /usr/local/nginx/conf/vhosts/test.conf
说明:expires定义的是过期时间,
加入下面内容
 location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
    access_log 0ff;
    expires 15d; 
}
    location ~ (js|css)
{
    access_log off;
    expires 15h; 
}
 
2:重新加载Nginx
[root@host ~]# nginx -s reload
 
 
 
二、测试
 
1:创建两个测试文件,分别后缀为js、jpg
[root@host ~]# echo "test js" > /data/www/1.js
[root@host ~]# echo "test jpg" > /data/www/2.jpg
 
 
2:用curl访问测试
[root@host ~]# curl -x127.0.0.1:80 www.test.com/1.js
test js
[root@host ~]# curl -x127.0.0.1:80 www.test.com/2.jpg
test jpg
[root@host ~]# curl -x127.0.0.1:80 www.test.com/index.htm
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.14.2</center>
</body>
</html>
 
3:查看日志是否记录
[root@host ~]# cat /tmp/access.log
127.0.0.1 - - [08/Oct/2019:05:54:10 +0800] "GET HTTP://www.test.com/index.htm HTTP/1.1" 404 169 "-" "curl/7.29.0"
上面可以看出只记录了一条index.htm的URL访问记录
 
4:验证是否显示过期时间
[root@host ~]# curl -x127.0.0.1:80 www.test.com/2.jpg -I
说明:max-age=604800显示的是过期时间,如果没有设置则这行不显示。
HTTP/1.1 200 OK
Server: nginx/1.14.2
Date: Mon, 07 Oct 2019 21:56:05 GMT
Content-Type: image/jpeg
Content-Length: 9
Last-Modified: Mon, 07 Oct 2019 21:48:32 GMT
Connection: keep-alive
ETag: "5d9bb2b0-9"
Expires: Mon, 14 Oct 2019 21:56:05 GMT
Cache-Control: max-age=604800
Accept-Ranges: bytes
 

推荐阅读