日志不记录静态文件,日志切割,静态元素过期时间
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了日志不记录静态文件,日志切割,静态元素过期时间相关的知识,希望对你有一定的参考价值。
访问日志不记录静态文件
1.过滤静态文件:修改虚拟主机配置文件
[[email protected] ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} !^111.com$
RewriteRule ^/(.*)$ http://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 "logs/111.com-access_log" combined env=!img
</VirtualHost>
2.进行验证,重新加载:
[[email protected] ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[[email protected] ~]# /usr/local/apache2.4/bin/apachectl graceful
3.测试:jpg1,jpg
[[email protected] ~]# curl -x127.0.0.1:80 111.com/alsdkjfakjg.jpg1 -I
HTTP/1.1 404 Not Found
Date: Mon, 05 Mar 2018 13:20:01 GMT
Server: Apache/2.4.29 (Unix) php/7.1.6
Content-Type: text/html; charset=iso-8859-1
[[email protected] ~]# tail /usr/local/apache2.4/logs/111.com-access_log
192.168.188.1 - - [03/Mar/2018:10:01:35 +0800] "GET / HTTP/1.1" 200 7 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; LCJB; rv:11.0) like Gecko"
192.168.188.1 - - [03/Mar/2018:10:01:36 +0800] "GET /favicon.ico HTTP/1.1" 404 209 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; Trident/7.0; LCJB; rv:11.0) like Gecko"
192.168.188.1 - - [03/Mar/2018:10:01:42 +0800] "GET / HTTP/1.1" 200 7 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; LCJB; rv:11.0) like Gecko"
192.168.188.1 - - [03/Mar/2018:10:01:51 +0800] "GET / HTTP/1.1" 301 223 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; LCJB; rv:11.0) like Gecko"
192.168.188.1 - - [03/Mar/2018:10:01:51 +0800] "GET / HTTP/1.1" 200 7 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; LCJB; rv:11.0) like Gecko"
192.168.188.1 - - [03/Mar/2018:10:02:36 +0800] "GET / HTTP/1.1" 200 7 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; LCJB; rv:11.0) like Gecko"
192.168.188.1 - - [03/Mar/2018:10:02:43 +0800] "GET / HTTP/1.1" 200 7 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; LCJB; rv:11.0) like Gecko"
192.168.188.1 - - [03/Mar/2018:10:02:49 +0800] "GET / HTTP/1.1" 200 7 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; LCJB; rv:11.0) like Gecko"
127.0.0.1 - - [05/Mar/2018:21:16:51 +0800] "HEAD HTTP://111.com/alsdkjfakjg.jpg HTTP/1.1" 404 - "-" "curl/7.29.0"
127.0.0.1 - - [05/Mar/2018:21:20:01 +0800] "HEAD HTTP://111.com/alsdkjfakjg.jpg1 HTTP/1.1" 404 - "-" "curl/7.29.0"
[[email protected] ~]# curl -x127.0.0.1:80 111.com/alsdkjfak1231jg.jpg -I
HTTP/1.1 404 Not Found
Date: Mon, 05 Mar 2018 13:20:24 GMT
Server: Apache/2.4.29 (Unix) PHP/7.1.6
Content-Type: text/html; charset=iso-8859-1
[[email protected] ~]# tail /usr/local/apache2.4/logs/111.com-access_log
192.168.188.1 - - [03/Mar/2018:10:01:35 +0800] "GET / HTTP/1.1" 200 7 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; LCJB; rv:11.0) like Gecko"
192.168.188.1 - - [03/Mar/2018:10:01:36 +0800] "GET /favicon.ico HTTP/1.1" 404 209 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; Trident/7.0; LCJB; rv:11.0) like Gecko"
192.168.188.1 - - [03/Mar/2018:10:01:42 +0800] "GET / HTTP/1.1" 200 7 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; LCJB; rv:11.0) like Gecko"
192.168.188.1 - - [03/Mar/2018:10:01:51 +0800] "GET / HTTP/1.1" 301 223 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; LCJB; rv:11.0) like Gecko"
192.168.188.1 - - [03/Mar/2018:10:01:51 +0800] "GET / HTTP/1.1" 200 7 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; LCJB; rv:11.0) like Gecko"
192.168.188.1 - - [03/Mar/2018:10:02:36 +0800] "GET / HTTP/1.1" 200 7 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; LCJB; rv:11.0) like Gecko"
192.168.188.1 - - [03/Mar/2018:10:02:43 +0800] "GET / HTTP/1.1" 200 7 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; LCJB; rv:11.0) like Gecko"
192.168.188.1 - - [03/Mar/2018:10:02:49 +0800] "GET / HTTP/1.1" 200 7 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; LCJB; rv:11.0) like Gecko"
127.0.0.1 - - [05/Mar/2018:21:16:51 +0800] "HEAD HTTP://111.com/alsdkjfakjg.jpg HTTP/1.1" 404 - "-" "curl/7.29.0"
127.0.0.1 - - [05/Mar/2018:21:20:01 +0800] "HEAD HTTP://111.com/alsdkjfakjg.jpg1 HTTP/1.1" 404 - "-" "curl/7.29.0"
[[email protected] ~]# curl -x127.0.0.1:80 111.com/alsdkjfak1231jg.jpg12 -I
HTTP/1.1 404 Not Found
Date: Mon, 05 Mar 2018 13:20:48 GMT
Server: Apache/2.4.29 (Unix) PHP/7.1.6
Content-Type: text/html; charset=iso-8859-1
[[email protected] ~]# tail /usr/local/apache2.4/logs/111.com-access_log
192.168.188.1 - - [03/Mar/2018:10:01:36 +0800] "GET /favicon.ico HTTP/1.1" 404 209 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; Trident/7.0; LCJB; rv:11.0) like Gecko"
192.168.188.1 - - [03/Mar/2018:10:01:42 +0800] "GET / HTTP/1.1" 200 7 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; LCJB; rv:11.0) like Gecko"
192.168.188.1 - - [03/Mar/2018:10:01:51 +0800] "GET / HTTP/1.1" 301 223 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; LCJB; rv:11.0) like Gecko"
192.168.188.1 - - [03/Mar/2018:10:01:51 +0800] "GET / HTTP/1.1" 200 7 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; LCJB; rv:11.0) like Gecko"
192.168.188.1 - - [03/Mar/2018:10:02:36 +0800] "GET / HTTP/1.1" 200 7 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; LCJB; rv:11.0) like Gecko"
192.168.188.1 - - [03/Mar/2018:10:02:43 +0800] "GET / HTTP/1.1" 200 7 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; LCJB; rv:11.0) like Gecko"
192.168.188.1 - - [03/Mar/2018:10:02:49 +0800] "GET / HTTP/1.1" 200 7 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; LCJB; rv:11.0) like Gecko"
127.0.0.1 - - [05/Mar/2018:21:16:51 +0800] "HEAD HTTP://111.com/alsdkjfakjg.jpg HTTP/1.1" 404 - "-" "curl/7.29.0"
127.0.0.1 - - [05/Mar/2018:21:20:01 +0800] "HEAD HTTP://111.com/alsdkjfakjg.jpg1 HTTP/1.1" 404 - "-" "curl/7.29.0"
127.0.0.1 - - [05/Mar/2018:21:20:48 +0800] "HEAD HTTP://111.com/alsdkjfak1231jg.jpg12 HTTP/1.1" 404 - "-" "curl/7.29.0"
访问日志切割
1.打开虚拟主机配置文件:
[[email protected] 111.com]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
CustomLog "|/usr/local/apache2.4/bin/rotatelogs -l logs/111.com-access_%Y%m%d.log 86400" combined env=!img
2.重新加载:
[[email protected] 111.com]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[[email protected] 111.com]# /usr/local/apache2.4/bin/apachectl graceful
3.验证:
[[email protected] 111.com]# ls /usr/local/apache2.4/logs
111.com-access_log abc.com-access_log access_log httpd.pid
111.com-error_log abc.com-error_log error_log
[[email protected] 111.com]# curl -x127.0.0.1:80 111.com/123.php
123.php[[email protected] 111^C
[[email protected] 111.com]# ls /usr/local/apache2.4/logs
111.com-access_20180305.log 111.com-error_log abc.com-error_log error_log
111.com-access_log abc.com-access_log access_log httpd.pid
[[email protected] 111.com]# cat /usr/local/apache2.4/logs/111.com-access_20180305.log
127.0.0.1 - - [05/Mar/2018:21:51:50 +0800] "GET HTTP://111.com/123.php HTTP/1.1" 200 7 "-" "curl/7.29.0"
静态元素过期时间
304表示来自缓存
1.打开虚拟主机配置文件
[[email protected] 111.com]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
</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>
2.重新加载
[[email protected] 111.com]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[[email protected] 111.com]# /usr/local/apache2.4/bin/apachectl graceful
3.检测模块是否开启,没有打开,修改主配置文件:
[[email protected] 111.com]# vim /usr/local/apache2.4/conf/httpd.conf
LoadModule env_module modules/mod_env.so
LoadModule expires_module modules/mod_expires.so
LoadModule headers_module modules/mod_headers.so
#LoadModule unique_id_module modules/mod_unique_id.so
[[email protected] 111.com]# /usr/local/apache2.4/bin/apachectl -M |grep expire
expires_module (shared)
4.验证:有cache control一行
[[email protected] 111.com]# curl -x127.0.0.1:80 111.com/bj.jpg -I
HTTP/1.1 200 OK
Date: Mon, 05 Mar 2018 14:14:26 GMT
Server: Apache/2.4.29 (Unix) PHP/7.1.6
Last-Modified: Tue, 02 May 2017 15:30:36 GMT
ETag: "7e25-54e8c38a0bf00"
Accept-Ranges: bytes
Content-Length: 32293
Cache-Control: max-age=86400
Expires: Tue, 06 Mar 2018 14:14:26 GMT
Content-Type: image/jpeg
以上是关于日志不记录静态文件,日志切割,静态元素过期时间的主要内容,如果未能解决你的问题,请参考以下文章