1、apache日记:由于网站大多元素为静态文件,如图片、css、js等,这些每次访问都是一个请求,会造成日记文件变大,消耗磁盘空间和IO,而文件本身也不大,没有意义,可以不用记录日记的:
1:修改apache的配置文件: /usr/local/apapche2.4/conf/extra/httpd-vhosts.conf
[root@localhost_002 ~]# vim /usr/local/apapche2.4/conf/extra/httpd-vhosts.conf
<VirtualHost *:80>
ServerAdmin yuanhh@foreb.com
DocumentRoot "/data/wwwroot/111.com"
ServerName www.111.com
ServerAlias www.example.com www.2111.com
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.111.com$
RewriteRule ^/(.*)$ http://www.111.com/$1 [R=301,L]
</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
#setenvif为定义变量,把所有变量定义为img:
CustomLog "logs/111.com-access_log" combined env=!img
#"env=!img"表示非img的变量,本行命令的含义,不记录关于变量img的请求日记:
</VirtualHost>
注释:" SetEnvIf Request_URI ".*\.gif$" img"表示用setenvif 定义URL的请求 “.*\.gif$”表示匹配正则,
匹配到".gif/jpg/png/bmp/swf/js/css结尾"都定义变量名为img:
“env=!ing”叹号表示非的意思,表示排除img的访问请求:即表示变量img的在日记格式中不记录:
2:检测并重新启动服务: -t graceful
[root@localhost_002 ~]# /usr/local/apapche2.4/bin/apachectl -t
Syntax OK
[root@localhost_002 ~]# /usr/local/apapche2.4/bin/apachectl graceful
3:用rz命令给xshell上传一条命令,并测试:
[root@localhost_002 111.com]# ls #用rz命令上传了7.png:
123.php 7.png index.php
[root@localhost_002 111.com]# curl -x127.0.0.1:80 www.111.com/7.png -I #用curl命令去请求7.png:
HTTP/1.1 200 OK
Date: Tue, 28 Aug 2018 15:24:46 GMT
Server: Apache/2.4.34 (Unix) PHP/5.6.30
Last-Modified: Fri, 21 Aug 2015 09:42:37 GMT
ETag: "d14c-51dcf1687fd40"
Accept-Ranges: bytes
Content-Length: 53580
Content-Type: image/png
[root@localhost_002 111.com]# curl -x127.0.0.1:80 www.111.com/123.php -I #同时也请求下123.php:
HTTP/1.1 200 OK
Date: Tue, 28 Aug 2018 15:25:09 GMT
Server: Apache/2.4.34 (Unix) PHP/5.6.30
X-Powered-By: PHP/5.6.30
Content-Type: text/html; charset=UTF-8
[root@localhost_002 111.com]# tail /usr/local/apapche2.4/logs/111.com-access_log
192.168.149.135 - - [28/Aug/2018:22:32:46 +0800] "GET /123.php HTTP/1.1" 200 7 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"
127.0.0.1 - - [28/Aug/2018:23:25:09 +0800] "HEAD HTTP://www.111.com/123.php HTTP/1.1" 200 - "-" "curl/7.29.0"
#由日记可知,发现只有访问www.111.com/123.php被记录,而访问图片并没有被记录到日记里:
注释:#由日记可知,发现只有访问www.111.com/123.php被记录,而访问图片www.111.com/7.png并没有被记录到日记里:
2、Apache的日记切割:日记每天都不停的写入,会把磁盘写满,所以有必然每天让它自动切割,并删除老的日记文件:
1:修改apache的配置文件: /usr/local/apapche2.4/conf/extra/httpd-vhosts.conf
[root@localhost_002 ~]# vim /usr/local/apapche2.4/conf/extra/httpd-vhosts.conf
<VirtualHost *:80>
ServerAdmin yuanhh@foreb.com
DocumentRoot "/data/wwwroot/111.com"
ServerName www.111.com
ServerAlias www.example.com www.2111.com
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.111.com$
RewriteRule ^/(.*)$ http://www.111.com/$1 [R=301,L]
</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/apapche2.4/bin/rotatelogs -l logs/123.com-access_%Y%m%d.log 86400" combined env=!img
</VirtualHost>
#需要指定apapche自带的rotatelogs工具, -l以当前系统时间为基准(CST),每天切割一次,并且日记格式名字为"111.com-access_年月日.log" 86400s是秒,表示一天:
注释:首先在CustomLog那一栏里,指定apapche自带的rotatelogs工具,-l(小写L),以当前世界为基准,每天切割一次,并且日记格式为"111.com-access_年年月", 86400的单位是秒,表示一天:
测试:在用curl或者是网页访问后,再次查看日记文件,会多出来一个日记文件:123.com-20180829.log的文件:
[root@localhost_002 logs]# curl -x127.0.0.1:80 www.111.com/
111.com
[root@localhost_002 logs]# ls
111.com-access_log 123.com-access_20180829.log abc.com-error_log error_log
111.com-error_log abc.com-access_log access_log httpd.pid
[root@localhost_002 logs]# tail -f 123.com-access_20180829.log
192.168.149.135 - - [29/Aug/2018:23:00:52 +0800] "GET / HTTP/1.1" 200 7 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"
192.168.149.135 - - [29/Aug/2018:23:00:52 +0800] "GET /favicon.ico HTTP/1.1" 404 209 "http://www.111.com/" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"
127.0.0.1 - - [29/Aug/2018:23:01:17 +0800] "GET HTTP://www.111.com/ HTTP/1.1" 200 7 "-" "curl/7.29.0"
注释:也为了防止磁盘写满的,可以写一个cron计划,存放日记中:
3、配置静态元素过期时间:浏览器在访问网站图片时会把静态的文件缓存到本地电脑中,这样下次在再访问时就不用去远程服务器去下载,所以需要配置静态元素过期时间,设定清除该部分缓存时间,可用于优化网站:
也是需要修改虚拟配置文件:/usr/local/apapche2.4/conf/extra/httpd-vhosts.conf
1:首先我们需要expires module模块的支持才可以:查看并打开该模块:
[root@localhost_002 logs]# /usr/local/apapche2.4/bin/apachectl -M|grep expire #检测此模块:
[root@localhost_002 logs]# vim /usr/local/apapche2.4/conf/httpd.conf
LoadModule expires_module modules/mod_expires.so
#打开配置文件httpd.conf,并把LoadModule前面的“#”号去掉:
[root@localhost_002 logs]# /usr/local/apapche2.4/bin/apachectl -M|grep expire #再次查看,已加载:
expires_module (shared)
2:修改配置文件: /usr/local/apapche2.4/conf/extra/httpd-vhosts.conf
[root@localhost_002 logs]# cat /usr/local/apapche2.4/conf/extra/httpd-vhosts.conf
<VirtualHost *:80>
ServerAdmin yuanhh@foreb.com
DocumentRoot "/data/wwwroot/111.com"
ServerName www.111.com
ServerAlias www.example.com www.2111.com
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.111.com$
RewriteRule ^/(.*)$ http://www.111.com/$1 [R=301,L]
</IfModule>
<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/apapche2.4/bin/rotatelogs -l logs/123.com-access_%Y%m%d.log 86400" combined env=!img
</VirtualHost>
3: 检测并重新加载服务: -t graceful
[root@localhost_002 111.com]# /usr/local/apapche2.4/bin/apachectl -t
Syntax OK
[root@localhost_002 111.com]# /usr/local/apapche2.4/bin/apachectl graceful
4:测试: curl -x127.0.0.1:80 www.111.com/baidu.png
111.com[root@localhost_002 111.com]# curl -x127.0.0.1:80 www.111.com/baidu.png -I
HTTP/1.1 200 OK
Date: Wed, 29 Aug 2018 15:58:20 GMT
Server: Apache/2.4.34 (Unix) PHP/5.6.30
Last-Modified: Fri, 21 Aug 2015 09:42:37 GMT
ETag: "d14c-51dcf1687fd40"
Accept-Ranges: bytes
Content-Length: 53580
Cache-Control: max-age=86400 #等同于我们设置的86400s:
Expires: Thu, 30 Aug 2018 15:58:20 GMT
Content-Type: image/png
注释:如上图例中:即png图片时间缓存为1天:
Date: Wed, 29 Aug 2018 15:58:20 GMT表示当前时间:
缓存时长:Cache-Control: max-age=86400 单位是s(秒):
失效时间:Expires: Thu, 30 Aug 2018 15:58:20 GMT:
也可以通过在浏览器里刷新,用F12查看其头部(Headers)查看其过期时间:
ctrl+F5:浏览器强制刷新: