1. 程式人生 > >Apache日誌不記錄訪問靜態檔案,訪問日誌切割,靜態元素過期時間設定

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
[[email protected] 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>
[
[email protected]
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"
  • 進行限制配置
[[email protected] 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>
  • 重新載入配置,訪問測試
[[email protected] extra]# /usr/local/apache2.4/bin/apachectl graceful
[[email protected] 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"

[[email protected] 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>
[[email protected] 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"
[[email protected] 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>
[[email protected] 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進行日誌檔案切割

[[email protected] 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

[[email protected] 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>

[[email protected] extra]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[[email protected] extra]# curl -x127.0.0.1:80 -l www.test.com/index.php
hello, test-webroot from www.test.com[[email protected] extra]#
[[email protected] extra]#
[[email protected] 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

  • 配置測試

[[email protected] 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>


[[email protected] test-webroot]#
[[email protected] test-webroot]# /usr/local/apache2.4/bin/apachectl -M | grep expires # 檢視expires_module是否可用
[[email protected] test-webroot]# vim /usr/local/apache2.4/conf/httpd.conf # 取消該模組的註釋
[[email protected] test-webroot]# /usr/local/apache2.4/bin/apachectl -M | grep expires
 expires_module (shared)
[[email protected] test-webroot]# /usr/local/apache2.4/bin/apachectl graceful
[[email protected] test-webroot]#
[[email protected] 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