網站大多元素爲靜態文件,如圖片、css、js等,這些元素能夠不用記錄
把虛擬主機配置文件改爲以下:javascript
<VirtualHost *:80> DocumentRoot "/data/wwwroot/111.com" ServerName www.111.com ServerAlias 111.com 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/123.com-access_log" combined env=!img </VirtualHost>
從新加載配置文件 -t, graceful
mkdir /data/wwwroot/www.123.com/images //建立目錄,並在這目錄下上傳一個圖片
curl -x127.0.0.1:80 -I 123.com/images/123.jpg
tail /usr/local/apache2.4/logs/123.com-access_log
先直接訪問圖片:php
[root@Dasoncheng ~]# ls /data/wwwroot/111.com/ admin.php index.php luds.jpg [root@Dasoncheng ~]# curl www.111.com/admin.php Welcome to the page of admin [root@Dasoncheng ~]# curl www.111.com/luds.jpg -I HTTP/1.1 200 OK Date: Fri, 23 Feb 2018 10:15:46 GMT Server: Apache/2.4.27 (Unix) PHP/5.6.30 Last-Modified: Fri, 11 Mar 2016 08:40:01 GMT ETag: "1ac51-52dc1e0711a40" Accept-Ranges: bytes Content-Length: 109649 Content-Type: image/jpeg
再看日誌:css
[root@Dasoncheng ~]# true > /usr/local/apache2.4/logs/111.com-access_log [root@Dasoncheng ~]# tail -f /usr/local/apache2.4/logs/111.com-access_log 192.168.60.11 - - [23/Feb/2018:18:14:31 +0800] "GET /admin.php HTTP/1.1" 200 29 "-" "curl/7.29.0" 192.168.60.11 - - [23/Feb/2018:18:14:42 +0800] "GET /luds.jpg HTTP/1.1" 200 109649 "-" "curl/7.29.0" ##這條訪問日誌裏面有luds.jpg記錄,咱們不想要這樣的靜態文件記錄怎麼辦?
[root@Dasoncheng ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf <VirtualHost *:80> # ServerAdmin webmaster@dummy-host2.example.com DocumentRoot "/data/wwwroot/111.com" ServerName www.111.com ServerAlias 111.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 "logs/111.com-access_log" combined env=!img </VirtualHost> [root@Dasoncheng ~]# /usr/local/apache2.4/bin/apachectl -t Syntax OK [root@Dasoncheng ~]# /usr/local/apache2.4/bin/apachectl graceful
再次訪問:html
[root@Dasoncheng ~]# curl www.111.com/admin.php Welcome to the page of admin [root@Dasoncheng ~]# curl www.111.com/luds.jpg -I HTTP/1.1 200 OK Date: Fri, 23 Feb 2018 10:15:46 GMT Server: Apache/2.4.27 (Unix) PHP/5.6.30 Last-Modified: Fri, 11 Mar 2016 08:40:01 GMT ETag: "1ac51-52dc1e0711a40" Accept-Ranges: bytes Content-Length: 109649 Content-Type: image/jpeg
再看日誌:java
[root@Dasoncheng ~]# tail -f /usr/local/apache2.4/logs/111.com-access_log 192.168.60.11 - - [23/Feb/2018:18:14:31 +0800] "GET /admin.php HTTP/1.1" 200 29 "-" "curl/7.29.0" 192.168.60.11 - - [23/Feb/2018:18:14:42 +0800] "GET /luds.jpg HTTP/1.1" 200 109649 "-" "curl/7.29.0" 192.168.60.11 - - [23/Feb/2018:18:15:39 +0800] "GET /admin.php HTTP/1.1" 200 29 "-" "curl/7.29.0" ##只多了一行admin.php的日誌,圖片的日誌沒有!ok
一些加載的訪問,都會記錄到日誌裏面去;
爲了不這種狀況(防止日誌過大佔用磁盤空間和io):定義一些條件、匹配一些文件!而後加入到規則裏面去web
日誌一直記錄總有一天會把整個磁盤佔滿,因此有必要讓它自動切割,並刪除老的日誌文件
把虛擬主機配置文件改爲以下:apache
<VirtualHost *:80> DocumentRoot "/data/wwwroot/www.123.com" ServerName www.123.com ServerAlias 123.com 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/apache2.4/bin/rotatelogs -l logs/123.com-access_%Y%m%d.log 86400" combined env=!img </VirtualHost>
從新加載配置文件 -t, graceful
ls /usr/local/apache2.4/logsvim
[root@Dasoncheng ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf <VirtualHost *:80> # ServerAdmin webmaster@dummy-host2.example.com DocumentRoot "/data/wwwroot/111.com" ServerName www.111.com ServerAlias 111.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/apache2.4/bin/rotatelogs -l logs/111.com-access_%Y%m%d.log 86400" combined env=!img </VirtualHost> [root@Dasoncheng ~]# /usr/local/apache2.4/bin/apachectl -t Syntax OK [root@Dasoncheng ~]# /usr/local/apache2.4/bin/apachectl graceful [root@Dasoncheng ~]# 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 [root@Dasoncheng ~]# curl www.111.com 111.com [root@Dasoncheng ~]# ls /usr/local/apache2.4/logs/ 111.com-access_20180223.log 111.com-error_log abc.com-error_log error_log 111.com-access_log abc.com-access_log access_log httpd.pid [root@Dasoncheng ~]# crontab -e 0 12 1 * * /usr/bin/find /usr/local/apache2.4/logs/ -mtime +30 -name "111.com-access_20*" -exec rm -f {} \; crontab: installing new crontab
目的:防止寫滿磁盤;方便管理日誌
利用Apache自帶的切割工具rotatelogs、-l表明使用本地時間!
24小時=86400s
再作一個任務計劃crontab,超過兩個月刪掉瀏覽器
瀏覽器訪問網站的圖片時會把靜態的文件緩存在本地電腦裏,這樣下次再訪問時就不用去遠程下載了
增長配置
<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>
須要expires_module
curl測試,看cache-control: max-age緩存
<VirtualHost *:80> # ServerAdmin webmaster@dummy-host2.example.com DocumentRoot "/data/wwwroot/111.com" ServerName www.111.com ServerAlias 111.com <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/apache2.4/bin/rotatelogs -l logs/111.com-access_%Y%m%d.log 86400" combined env=!img </VirtualHost>
開啓過時時間:
[root@Dasoncheng ~]# curl www.111.com/luds.jpg -I HTTP/1.1 200 OK Date: Fri, 23 Feb 2018 11:16:32 GMT Server: Apache/2.4.27 (Unix) PHP/5.6.30 Last-Modified: Fri, 11 Mar 2016 08:40:01 GMT ETag: "1ac51-52dc1e0711a40" Accept-Ranges: bytes Content-Length: 109649 Cache-Control: max-age=86400 Expires: Sat, 24 Feb 2018 11:16:32 GMT Content-Type: image/jpeg
關閉過時時間:
[root@Dasoncheng ~]# grep -i expire /usr/local/apache2.4/conf/httpd.conf #LoadModule expires_module modules/mod_expires.so [root@Dasoncheng ~]# /usr/local/apache2.4/bin/apachectl graceful
[root@Dasoncheng ~]# curl www.111.com/luds.jpg -I HTTP/1.1 200 OK Date: Fri, 23 Feb 2018 11:18:04 GMT Server: Apache/2.4.27 (Unix) PHP/5.6.30 Last-Modified: Fri, 11 Mar 2016 08:40:01 GMT ETag: "1ac51-52dc1e0711a40" Accept-Ranges: bytes Content-Length: 109649 Content-Type: image/jpeg
ctrl + f5 強制刷新(清理該頁面緩存)
apache日誌記錄代理IP以及真實客戶端IP http://www.lishiming.net/thread-960-1-1.html
apache只記錄指定URI的日誌 http://www.lishiming.net/thread-981-1-1.html
apache日誌記錄客戶端請求的域名 http://www.lishiming.net/thread-1037-1-1.html
apache 日誌切割問題 http://www.lishiming.net/thread-566-1-1.html