美文网首页我爱编程
访问日志不记录静态文件、访问日志切割、静态元素过期时间

访问日志不记录静态文件、访问日志切割、静态元素过期时间

作者: 强出头 | 来源:发表于2018-04-16 23:09 被阅读0次
访问日志不记录静态文件
[root@wsl-001 ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf 
(更改以下,注意位置)

    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
[root@wsl-001 ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@wsl-001 ~]# /usr/local/apache2.4/bin/apachectl graceful
[root@wsl-001 ~]# curl -x 127.0.0.1:80 111.com/dasfa.php
<!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 /dasfa.php was not found on this server.</p>
</body></html>
[root@wsl-001 ~]# tail /usr/local/apache2.4/logs/111.com-access_log 
172.16.79.1 - - [13/Apr/2018:12:15:41 +0800] "GET /admin.php HTTP/1.1" 401 381
172.16.79.1 - abc [13/Apr/2018:12:15:46 +0800] "GET /admin.php HTTP/1.1" 200 78494
127.0.0.1 - abc [13/Apr/2018:12:16:21 +0800] "HEAD HTTP://111.com/admin.php HTTP/1.1" 200 -
172.16.79.1 - abc [14/Apr/2018:16:25:21 +0800] "GET /admin.php HTTP/1.1" 200 78494
172.16.79.1 - - [14/Apr/2018:17:05:36 +0800] "GET / HTTP/1.1" 301 223
172.16.79.1 - abc [14/Apr/2018:17:05:36 +0800] "GET / HTTP/1.1" 200 7
127.0.0.1 - - [14/Apr/2018:17:05:54 +0800] "GET HTTP://www.example.com/ HTTP/1.1" 301 223
172.16.79.1 - abc [14/Apr/2018:17:49:40 +0800] "GET / HTTP/1.1" 200 7 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36 QQBrowser/4.3.4986.400"
127.0.0.1 - - [16/Apr/2018:21:44:01 +0800] "GET HTTP://www.example.com/ HTTP/1.1" 301 223 "-" "curl/7.29.0"
127.0.0.1 - - [16/Apr/2018:21:44:37 +0800] "GET HTTP://111.com/dasfa.php HTTP/1.1" 404 207 "-" "curl/7.29.0"
[root@wsl-001 ~]# curl -x 127.0.0.1:80 111.com/dasfa.jpg
<!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 /dasfa.jpg was not found on this server.</p>
</body></html>
[root@wsl-001 ~]# tail /usr/local/apache2.4/logs/111.com-access_log 
172.16.79.1 - - [13/Apr/2018:12:15:41 +0800] "GET /admin.php HTTP/1.1" 401 381
172.16.79.1 - abc [13/Apr/2018:12:15:46 +0800] "GET /admin.php HTTP/1.1" 200 78494
127.0.0.1 - abc [13/Apr/2018:12:16:21 +0800] "HEAD HTTP://111.com/admin.php HTTP/1.1" 200 -
172.16.79.1 - abc [14/Apr/2018:16:25:21 +0800] "GET /admin.php HTTP/1.1" 200 78494
172.16.79.1 - - [14/Apr/2018:17:05:36 +0800] "GET / HTTP/1.1" 301 223
172.16.79.1 - abc [14/Apr/2018:17:05:36 +0800] "GET / HTTP/1.1" 200 7
127.0.0.1 - - [14/Apr/2018:17:05:54 +0800] "GET HTTP://www.example.com/ HTTP/1.1" 301 223
172.16.79.1 - abc [14/Apr/2018:17:49:40 +0800] "GET / HTTP/1.1" 200 7 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36 QQBrowser/4.3.4986.400"
127.0.0.1 - - [16/Apr/2018:21:44:01 +0800] "GET HTTP://www.example.com/ HTTP/1.1" 301 223 "-" "curl/7.29.0"
127.0.0.1 - - [16/Apr/2018:21:44:37 +0800] "GET HTTP://111.com/dasfa.php HTTP/1.1" 404 207 "-" "curl/7.29.0"

访问日志切割

访问日志切割

日志需要写脚本按需求时间删除

[root@wsl-001 ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
(更改CustomLog)

CustomLog "|/usr/local/apache2.4/bin/rotatelogs -l logs/111.com-access_%Y%m%d.log 86400" combined env=!img

[root@wsl-001 ~]# /usr/local/apache2.4/bin/apachectl graceful
[root@wsl-001 ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
(再访问后可看到)
[root@wsl-001 ~]# ls /usr/local/apache2.4/logs/
111.com-access_20180416.log  111.com-error_log   abc.com-error_log  error_log
111.com-access_log           abc.com-access_log  access_log         httpd.pid
[root@wsl-001 ~]# cat /usr/local/apache2.4/logs/111.com-access_20180416.log 
172.16.79.1 - - [16/Apr/2018:22:02:17 +0800] "GET / HTTP/1.1" 200 7 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36 QQBrowser/4.3.4986.400"
172.16.79.1 - - [16/Apr/2018:22:02:21 +0800] "GET /123.php HTTP/1.1" 404 205 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36 QQBrowser/4.3.4986.400"

静态元素过期时间

[root@wsl-001 111.com]# !vim
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>

[root@wsl-001 111.com]# /usr/local/apache2.4/bin/apachectl -M |grep expires
[root@wsl-001 111.com]# vim /usr/local/apache2.4/conf/httpd.conf
(打开下面这个模块)
LoadModule expires_module modules/mod_expires.so
[root@wsl-001 111.com]# /usr/local/apache2.4/bin/apachectl graceful
[root@wsl-001 111.com]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@wsl-001 111.com]# /usr/local/apache2.4/bin/apachectl -M |grep expires
 expires_module (shared)
测试

扩展
apache日志记录代理IP以及真实客户端IP http://ask.apelearn.com/question/960
apache只记录指定URI的日志 http://ask.apelearn.com/question/981
apache日志记录客户端请求的域名 http://ask.apelearn.com/question/1037
apache 日志切割问题 http://ask.apelearn.com/question/566

相关文章

网友评论

    本文标题:访问日志不记录静态文件、访问日志切割、静态元素过期时间

    本文链接:https://www.haomeiwen.com/subject/rhkykftx.html