Apache日志不记录访问静态文件,访问日志切割,静态元素过期时间设置

Apache配置不记录访问静态文件的日志

  • 网站大多元素为静态文件,如图片、css、js等,这些元素可以不用记录

  • vhost原始配置

<VirtualHost *:80>
    ServerAdmin  [email protected]
    DocumentRoot "/usr/local/apache2.4/test-webroot"
    ServerName test.com
    ServerAlias www.test.com
    ErrorLog "logs/test.com-error_log"
    CustomLog "logs/test.com-access_log" combined
</VirtualHost>
  • 访问www.test.com/1.jpg
[root@test-a extra]# curl -x127.0.0.1:80 -l www.test.com/1.png
<!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 /1.png was not found on this server.</p>
</body></html>
[root@test-a extra]# tail -n 1 /usr/local/apache2.4/logs/test.com-access_log
127.0.0.1 - - [19/Nov/2018:07:00:54 +0800] "GET HTTP://www.test.com/1.png HTTP/1.1" 404 203 "-" "curl/7.29.0"
  • 进行限制配置
[root@test-a extra]# vim httpd-vhosts.conf # 进行如下配置
<VirtualHost *:80>
    ServerAdmin  [email protected]
    DocumentRoot "/usr/local/apache2.4/test-webroot"
    ServerName test.com
    ServerAlias www.test.com
    SetEnvIf Request_URI ".*\.gif$" static_file
    SetEnvIf Request_URI ".*\.jpg$" static_file
    SetEnvIf Request_URI ".*\.png$" static_file
    SetEnvIf Request_URI ".*\.bmp$" static_file
    SetEnvIf Request_URI ".*\.swf$" static_file
    ErrorLog "logs/test.com-error_log"
    CustomLog "logs/test.com-access_log" combined env=!static_file
</VirtualHost>
  • 重新加载配置,访问测试
[root@test-a extra]# /usr/local/apache2.4/bin/apachectl graceful
[root@test-a extra]# tail -n 1 /usr/local/apache2.4/logs/test.com-access_log
127.0.0.1 - - [19/Nov/2018:07:04:14 +0800] "GET HTTP://www.test.com/12.png HTTP/1.1" 404 204 "-" "curl/7.29.0"

[root@test-a extra]# curl -x127.0.0.1:80 -l www.test.com/123.png
<!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 /123.png was not found on this server.</p>
</body></html>
[root@test-a extra]# tail -n 1 /usr/local/apache2.4/logs/test.com-access_log
127.0.0.1 - - [19/Nov/2018:07:04:14 +0800] "GET HTTP://www.test.com/12.png HTTP/1.1" 404 204 "-" "curl/7.29.0"
[root@test-a extra]# curl -x127.0.0.1:80 -l www.test.com/123.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 /123.jpg was not found on this server.</p>
</body></html>
[root@test-a extra]# tail -n 1 /usr/local/apache2.4/logs/test.com-access_log
127.0.0.1 - - [19/Nov/2018:07:04:14 +0800] "GET HTTP://www.test.com/12.png HTTP/1.1" 404 204 "-" "curl/7.29.0"

访问日志切割

  • 日志一直记录总有一天会把整个磁盘占满,所以有必要让它自动切割,并删除老的日志文件。

  • 配置,使用Apache自带的切割工具rotatelogs进行日志文件切割

[root@test-a extra]# ls /usr/local/apache2.4/logs/
access_log                          dummy-host.example.com-access_log  httpd.pid
dummy-host2.example.com-access_log  dummy-host.example.com-error_log   test.com-access_log
dummy-host2.example.com-error_log   error_log                          test.com-error_log

[root@test-a extra]# vim httpd-vhosts.conf # 进行如下的配置
<VirtualHost *:80>
    ServerAdmin  [email protected]
    DocumentRoot "/usr/local/apache2.4/test-webroot"
    ServerName test.com
    ServerAlias www.test.com
    SetEnvIf Request_URI ".*\.gif$" static_file
    SetEnvIf Request_URI ".*\.jpg$" static_file
    SetEnvIf Request_URI ".*\.png$" static_file
    SetEnvIf Request_URI ".*\.bmp$" static_file
    SetEnvIf Request_URI ".*\.swf$" static_file
    ErrorLog "logs/test.com-error_log"
    CustomLog "|/usr/local/apache2.4/bin/rotatelogs -l  logs/test.com-access_log-%Y-%m-%d 86400" combined env=!static_file
</VirtualHost>

[root@test-a extra]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@test-a extra]# curl -x127.0.0.1:80 -l www.test.com/index.php
hello, test-webroot from www.test.com[root@test-a extra]#
[root@test-a extra]#
[root@test-a extra]# ls /usr/local/apache2.4/logs/
access_log                          dummy-host.example.com-access_log  httpd.pid                       test.com-error_log
dummy-host2.example.com-access_log  dummy-host.example.com-error_log   test.com-access_log
dummy-host2.example.com-error_log   error_log                          test.com-access_log-2018-11-18

配置静态文件过期时间

  • 浏览器访问网站的图片时会把静态的文件缓存在本地电脑里,这样下次再访问时就不用去远程下载了

  • 需要使用到expires_module

  • 配置测试

[root@test-a extra]# vim httpd-vhosts.conf  # 添加如下配置
<IfModule mod_expires.c>
    ExpiresActive on
    ExpiresByType image/gif  "access plus 1 day"
    ExpiresByType image/jpeg "access plus 24 hours"
    ExpiresByType image/png "access plus 24 hours"
    ExpiresByType text/css "now plus 2 hours"
    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@test-a test-webroot]#
[root@test-a test-webroot]# /usr/local/apache2.4/bin/apachectl -M | grep expires # 查看expires_module是否可用
[root@test-a test-webroot]# vim /usr/local/apache2.4/conf/httpd.conf # 取消该模块的注释
[root@test-a test-webroot]# /usr/local/apache2.4/bin/apachectl -M | grep expires
 expires_module (shared)
[root@test-a test-webroot]# /usr/local/apache2.4/bin/apachectl graceful
[root@test-a test-webroot]#
[root@test-a test-webroot]# curl -x127.0.0.1:80  www.test.com/1.jpg -I
HTTP/1.1 200 OK
Date: Mon, 19 Nov 2018 00:32:00 GMT
Server: Apache/2.4.37 (Unix) PHP/5.6.32
Last-Modified: Mon, 19 Nov 2018 00:30:17 GMT
ETag: "0-57af99f141942"
Accept-Ranges: bytes
Cache-Control: max-age=86400 # 一天过期
Expires: Tue, 20 Nov 2018 00:32:00 GMT
Content-Type: image/jpeg

猜你喜欢

转载自my.oschina.net/u/996931/blog/2877920