标签:lamp 访问日志 访问日志切割 设置静态文件不记录日志 设置缓存静态文件过期时间
访问日志不记录指定类型的文件在网站大多数元素为静态文件,比如图片、css、js等,这些静态文件都可以不用记录,我们只需要记录哪个ip访问了那个网页就可以;
vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
在 ErrorLog "logs/111.com-error_log"的下面加入;
SetEnvIf Request_URI ".*\.gif$" img
SetEnvIf Request_URI ".*\.jpg$" img
SetEnvIf Request_URI ".*\.png$" img
SetEnvIf Request_URI ".*\.bmp$" img
SetEnvIf Request_URI ".*\.swf$" img
SetEnvIf Request_URI ".*\.js$" img
SetEnvIf Request_URI ".*\.css$" img
在CustomLog "logs/111.com-access_log" common 增加env=!img
<VirtualHost *:80>
DocumentRoot "/data/wwwroot/111.com"
ServerName 111.com
ServerAlias www.111.com
ErrorLog "logs/111.com-error_log"
SetEnvIf Request_URI ".*\.gif$" img
SetEnvIf Request_URI ".*\.jpg$" img
SetEnvIf Request_URI ".*\.png$" img
SetEnvIf Request_URI ".*\.bmp$" img
SetEnvIf Request_URI ".*\.swf$" img
SetEnvIf Request_URI ".*\.js$" img
SetEnvIf Request_URI ".*\.css$" img
CustomLog "logs/111.com-access_log" common env=!img
</VirtualHost>
/usr/local/apache2.4/bin/apachectl -t
/usr/local/apache2.4/bin/apachectl graceful
访问111.com/baidu.png会被日志记录下来
curl -x127.0.0.1:80 111.com/baidu.png
tail /usr/local/apache2.4/logs/111.com-access_log
127.0.0.1 - - [05/Mar/2018:16:41:31 +0800] "GET HTTP://111.com/baidu.png HTTP/1.1" 200 7877
curl -x127.0.0.1:80 111.com/baidu.png
tail /usr/local/apache2.4/logs/111.com-access_log
127.0.0.1 - - [05/Mar/2018:16:41:31 +0800] "GET HTTP://111.com/baidu.png HTTP/1.1" 200 7877
除了生效前被记录下来的一条日志,生效后访问的却并未被记录下来;
日志文件会一直记录着,总有一天会把磁盘空间给占满,一个文件上100G如何查看删除?那么我们可以设置日志按天切割成一个日志文件,一来方便查看日志,而来方便删除老旧日志;
vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
修改这一条配置CustomLog "logs/111.com-access_log" common env=!img
没有配置过不记录静态文件日志的小伙伴,请忽略掉后面的env=!img
修改成
CustomLog "|/usr/local/apache2.4/bin/rotatelogs -l logs/111.com-access_%Y%m%d.log" common env=!img
<VirtualHost *:80>
DocumentRoot "/data/wwwroot/111.com"
ServerName 111.com
ServerAlias www.111.com
ErrorLog "logs/111.com-error_log"
SetEnvIf Request_URI ".*\.gif$" img
SetEnvIf Request_URI ".*\.jpg$" img
SetEnvIf Request_URI ".*\.png$" img
SetEnvIf Request_URI ".*\.bmp$" img
SetEnvIf Request_URI ".*\.swf$" img
SetEnvIf Request_URI ".*\.js$" img
SetEnvIf Request_URI ".*\.css$" img
CustomLog "|/usr/local/apache2.4/bin/rotatelogs -l logs/111.com-access_%Y%m%d.log" common env=!img
</VirtualHost>
/usr/local/apache2.4/bin/apachectl -t
/usr/local/apache2.4/bin/apachectl graceful
ls /usr/local/apache2.4/logs/
[root@shu-test 111.com]# ls /usr/local/apache2.4/logs/
111.com-access_log access_log httpd.pid szl.com-error_log
111.com-error_log error_log szl.com-access_log
[root@shu-test 111.com]#
curl -x127.0.0.1:80 111.com/sdfhskjadfh.df
[root@shu-test 111.com]# curl -x127.0.0.1:80 111.com/sdfhskjadfh.df
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /sdfhskjadfh.df was not found on this server.</p>
</body></html>
ls /usr/local/apache2.4/logs/目录下面多出了111.com-access_20180305.log按年月日的log文件
[root@shu-test 111.com]# ls /usr/local/apache2.4/logs/
111.com-access_20180305.log 111.com-error_log error_log szl.com-access_log
111.com-access_log access_log httpd.pid szl.com-error_log
[root@shu-test 111.com]#
浏览器访问网站图片时,会把静态文件下载到电脑上,这样下次再访问时就不用去远程下载,达到节约服务器带宽的作用;
然而每个网站都是有内容更新的,我们一般设置一天更新一次,不设置更新那么本机访问的网页就是以前的旧网页,而不是更新后的网页;
vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
增加配置
<IfModule mod_expires.c>
ExpiresActive on //打开该功能的开关
ExpiresByType image/gif "access plus 1 days"
ExpiresByType image/jpeg "access plus 24 hours"
ExpiresByType image/png "access plus 24 hours"
ExpiresByType text/css "now plus 2 hour"
ExpiresByType application/x-javascript "now plus 2 hours"
ExpiresByType application/javascript "now plus 2 hours"
ExpiresByType application/x-shockwave-flash "now plus 2 hours"
ExpiresDefault "now plus 0 min"
</IfModule>
<VirtualHost *:80>
DocumentRoot "/data/wwwroot/111.com"
ServerName 111.com
ServerAlias www.111.com
<IfModule mod_expires.c>
ExpiresActive on
ExpiresByType image/gif "access plus 1 days"
ExpiresByType image/jpeg "access plus 24 hours"
ExpiresByType image/png "access plus 24 hours"
ExpiresByType text/css "now plus 2 hour"
ExpiresByType application/x-javascript "now plus 2 hours"
ExpiresByType application/javascript "now plus 2 hours"
ExpiresByType application/x-shockwave-flash "now plus 2 hours"
ExpiresDefault "now plus 0 min"
</IfModule>
ErrorLog "logs/111.com-error_log"
SetEnvIf Request_URI ".*\.gif$" img
SetEnvIf Request_URI ".*\.jpg$" img
SetEnvIf Request_URI ".*\.png$" img
SetEnvIf Request_URI ".*\.bmp$" img
SetEnvIf Request_URI ".*\.swf$" img
SetEnvIf Request_URI ".*\.js$" img
SetEnvIf Request_URI ".*\.css$" img
CustomLog "|/usr/local/apache2.4/bin/rotatelogs -l logs/111.com-access_%Y%m%d.log 86400" common env=!img
</VirtualHost>
由于增加了内容,调用了新模块expires,那么我们也要让apache加载对应模块才能生效
vim /usr/local/apache2.4/conf/httpd.conf
取消注释
LoadModule expires_module modules/mod_expires.so
查询expires模块是否正常加载(加载成功)
/usr/local/apache2.4/bin/apachectl -M |grep expires
expires_module (shared)
/usr/local/apache2.4/bin/apachectl -t
/usr/local/apache2.4/bin/apachectl graceful
curl -x127.0.0.1:80 111.com/baidu.png -I
HTTP/1.1 200 OK
Date: Mon, 05 Mar 2018 09:42:30 GMT
Server: Apache/2.4.29 (Unix) PHP/5.6.30
Last-Modified: Mon, 05 Mar 2018 09:39:35 GMT
ETag: "8bfa-566a71a942179"
Accept-Ranges: bytes
Content-Length: 35834
Cache-Control: max-age=86400
Expires: Tue, 06 Mar 2018 09:42:30 GMT
Content-Type: image/png
其中Cache-Control: max-age=86400 表示缓存时间为86400秒
Expires代表缓存过期时间;
lamp-日志设置(访问日志切割、不记录静态文件日志、缓存文件过期时间)
标签:lamp 访问日志 访问日志切割 设置静态文件不记录日志 设置缓存静态文件过期时间
原文地址:http://blog.51cto.com/shuzonglu/2083176