访问日志不记录指定类型的文件
在网站大多数元素为静态文件,比如图片、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
测试
配置前log文件
ls /usr/local/apache2.4/logs/
[[email protected] 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
[[email protected] 111.com]#
配置生效,访问后log文件
curl -x127.0.0.1:80 111.com/sdfhskjadfh.df
[[email protected] 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文件
[[email protected] 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
[[email protected] 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模块
由于增加了内容,调用了新模块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代表缓存过期时间;
原文地址:http://blog.51cto.com/shuzonglu/2083176