網站大多元素爲靜態文件,如圖片、css、js等,這些元素能夠不用記錄javascript
編輯虛擬主機配置文件「httpd-vhosts.conf」:php
[root@cham002 ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf 。。。。。。 <VirtualHost *:80> DocumentRoot "/data/wwwroot/111.com" ServerName 111.com ServerAlias www.example.com 2111.com.cn # <Directory /data/wwwroot/111.com> # <FilesMatch 123.php> # AllowOverride AuthConfig # AuthName "111.com user auth" # AuthType Basic #AuthUserFile /data/.htpasswd # require valid-user #</FilesMatch> # </Directory> <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> [root@cham002 ~]# /usr/local/apache2.4/bin/apachectl graceful 從新加載 [root@cham002 ~]# /usr/local/apache2.4/bin/apachectl -t [root@cham002 ~]# curl -x127.0.0.1:80 111.com/123214143523eee5353434 -I HTTP/1.1 404 Not Found Date: Thu, 21 Dec 2017 05:48:51 GMT Server: Apache/2.4.29 (Unix) PHP/7.1.6 Content-Type: text/html; charset=iso-8859-1 [root@cham002 ~]# tail /usr/local/apache2.4/logs/111.com-access_log 192.168.230.1 - cham [20/Dec/2017:22:31:12 +0800] "GET / HTTP/1.1" 200 18 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36" 192.168.230.135 - - [21/Dec/2017:10:09:44 +0800] "GET HTTP://111.com/ HTTP/1.1" 200 18 "-" "curl/7.29.0" 127.0.0.1 - - [21/Dec/2017:10:19:31 +0800] "GET HTTP://111.com/ HTTP/1.1" 200 18 "-" "curl/7.29.0" 127.0.0.1 - - [21/Dec/2017:10:19:37 +0800] "HEAD HTTP://111.com/ HTTP/1.1" 200 - "-" "curl/7.29.0" 127.0.0.1 - - [21/Dec/2017:10:19:53 +0800] "GET HTTP://111.com/ HTTP/1.1" 200 18 "-" "curl/7.29.0" 127.0.0.1 - - [21/Dec/2017:10:20:23 +0800] "GET HTTP://111.com/ HTTP/1.1" 200 18 "-" "curl/7.29.0" 127.0.0.1 - - [21/Dec/2017:10:22:28 +0800] "HEAD HTTP://2111.com.cn/ HTTP/1.1" 301 - "-" "curl/7.29.0" 127.0.0.1 - - [21/Dec/2017:13:45:52 +0800] "HEAD HTTP://111.com/123214143523535 HTTP/1.1" 404 - "-" "curl/7.29.0" 127.0.0.1 - - [21/Dec/2017:13:47:05 +0800] "HEAD HTTP://111.com/123214143523eee535 HTTP/1.1" 404 - "-" "curl/7.29.0" 127.0.0.1 - - [21/Dec/2017:13:48:51 +0800] "HEAD HTTP://111.com/123214143523eee5353434 HTTP/1.1" 404 - "-" "curl/7.29.0" [root@cham002 ~]# curl -x127.0.0.1:80 111.com/123214143523eee53.jpg -I 這個是不記錄的 HTTP/1.1 404 Not Found Date: Thu, 21 Dec 2017 05:49:35 GMT Server: Apache/2.4.29 (Unix) PHP/7.1.6 Content-Type: text/html; charset=iso-8859-1 [root@cham002 ~]# tail /usr/local/apache2.4/logs/111.com-access_log 192.168.230.1 - cham [20/Dec/2017:22:31:12 +0800] "GET / HTTP/1.1" 200 18 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36" 192.168.230.135 - - [21/Dec/2017:10:09:44 +0800] "GET HTTP://111.com/ HTTP/1.1" 200 18 "-" "curl/7.29.0" 127.0.0.1 - - [21/Dec/2017:10:19:31 +0800] "GET HTTP://111.com/ HTTP/1.1" 200 18 "-" "curl/7.29.0" 127.0.0.1 - - [21/Dec/2017:10:19:37 +0800] "HEAD HTTP://111.com/ HTTP/1.1" 200 - "-" "curl/7.29.0" 127.0.0.1 - - [21/Dec/2017:10:19:53 +0800] "GET HTTP://111.com/ HTTP/1.1" 200 18 "-" "curl/7.29.0" 127.0.0.1 - - [21/Dec/2017:10:20:23 +0800] "GET HTTP://111.com/ HTTP/1.1" 200 18 "-" "curl/7.29.0" 127.0.0.1 - - [21/Dec/2017:10:22:28 +0800] "HEAD HTTP://2111.com.cn/ HTTP/1.1" 301 - "-" "curl/7.29.0" 127.0.0.1 - - [21/Dec/2017:13:45:52 +0800] "HEAD HTTP://111.com/123214143523535 HTTP/1.1" 404 - "-" "curl/7.29.0" 127.0.0.1 - - [21/Dec/2017:13:47:05 +0800] "HEAD HTTP://111.com/123214143523eee535 HTTP/1.1" 404 - "-" "curl/7.29.0" 127.0.0.1 - - [21/Dec/2017:13:48:51 +0800] "HEAD HTTP://111.com/123214143523eee5353434 HTTP/1.1" 404 - "-" "curl/7.29.0"
說明: 將全部訪問圖片的請求定義爲變量img,在訪問記錄(日誌)中將其排除便可。從新加載後使用curl訪問img變量中所指定格式的文件內容時將不會產生訪問記錄。 取消,去掉 env=!img便可css
日誌一直記錄總有一天會把整個磁盤佔滿,因此有必要讓它自動切割,並刪除老的日誌文件html
編輯虛擬主機配置文件:前端
[root@cham002 111.com]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf <VirtualHost *:80> DocumentRoot "/data/wwwroot/111.com" ServerName 111.com ServerAlias www.example.com 2111.com.cn # <Directory /data/wwwroot/111.com> # <FilesMatch 123.php> # AllowOverride AuthConfig # AuthName "111.com user auth" # AuthType Basic #AuthUserFile /data/.htpasswd # require valid-user #</FilesMatch> # </Directory> <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 "|/usr/local/apache2.4/bin/rotatelogs -l logs/111.com-access_%Y%m%d.log 86400" combined env=!img </VirtualHost> #使用rotatelogs工具,以系統時間爲基準,天天切割一第二天志,而且日誌名字格式爲「111.com-access_%Y%m%d.log」。 #每小時切割一第二天志,語法: #CustomLog "|/usr/local/apache2.4/bin/rotatelogs logs/access_%Y%m%d%H.log 3600" combined
說明: rotatelogs是Apache切割日誌的工具;-l表示使用系統(CST=Chinese time)時間,若是加-l選項,將以默認的(UTC)時間爲準;日誌文件名「%Y%m%d」表示年月日;時間間隔設定爲一天(1day=24h=1440min=86400s)。 java
檢查重載配置文件: web
[root@cham002 111.com]# /usr/local/apache2.4/bin/apachectl -t Syntax OK [root@cham002 111.com]# /usr/local/apache2.4/bin/apachectl graceful [root@cham002 111.com]# ls /usr/local/apache2.4/logs/ 111.com-access_log 111.com-error_log abc.com-access_log abc.com-error_log access_log error_log httpd.pid [root@cham002 111.com]# curl -x127.0.0.1:80 111.com/123.php hello 123.php[root@cham/usr/local/apache2.4/bin/apachectl graceful [root@cham002 111.com]# ls /usr/local/apache2.4/logs/ 111.com-access_20171221.log 111.com-error_log abc.com-error_log error_log 111.com-access_log abc.com-access_log access_log httpd.pid [root@cham002 111.com]# cat /usr/local/apache2.4/logs/111.com-access_20171221.log 127.0.0.1 - - [21/Dec/2017:14:13:14 +0800] "GET HTTP://111.com/123.php HTTP/1.1" 200 14 "-" "curl/7.29.0"
當瀏覽器訪問網站時會把靜態文件(如圖片文件、css、js文件等)緩存到本地電腦,這樣下次訪問的時候就不用去遠程下載了,能夠自定義清除該部分緩存的時間,即設定靜態元素過時時間。該設置可用於優化網站,特別是公司內網。 apache
[root@cham002 111.com]# ls 123.php index.php photo.jpg 把photo.jpg 改爲photo1.jpg [root@cham002 111.com]# mv photo.jpg photo1.jpg [root@cham002 111.com]# ls 123.php index.php photo1.jpg
第一次訪問是200vim
第2次訪問變成304 Not Modified!瀏覽器
編輯虛擬主機配置文件:
[root@cham002 111.com]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf <VirtualHost *:80> DocumentRoot "/data/wwwroot/111.com" ServerName 111.com ServerAlias www.example.com 2111.com.cn # <Directory /data/wwwroot/111.com> # <FilesMatch 123.php> # AllowOverride AuthConfig # AuthName "111.com user auth" # AuthType Basic #AuthUserFile /data/.htpasswd # require valid-user #</FilesMatch> # </Directory> <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{HTTP_HOST} !^111.com$ RewriteRule ^/(.*)$ http://111.com/$1 [R=301,L] </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> RewriteRule ^/(.*)$ http://111.com/$1 [R=301,L] ErrorLog "logs/111.com-error_log" SetEnvIf Request_URI ".*\.gif$" img SetEnvIf Request_URI ".*\.jpg$" img SetEnvIf Request_URI ".*\.png$" 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>
說明: 在配置文件添加mod_expires.c模塊內容,其他不變。
[root@cham002 111.com]# /usr/local/apache2.4/bin/apachectl -t Syntax OK [root@cham002 111.com]# /usr/local/apache2.4/bin/apachectl -M |grep expire 檢測Apache配置文件是否開啓expire模塊:沒有打開 [root@cham002 111.com]# vim /usr/local/apache2.4/conf/httpd.conf [root@cham002 111.com]# /usr/local/apache2.4/bin/apachectl graceful [root@cham002 111.com]# /usr/local/apache2.4/bin/apachectl -M |grep expi expires_module (shared)
ctrl+f5強制刷新,清空瀏覽器緩存。由於配置了expires.c模塊右邊小框裏面就出現了它的過時時間等信息
用curl也是同樣的,出現了它的過時時間等信息
[root@cham002 111.com]# curl -x127.0.0.1:80 111.com/photo1.jpg -I HTTP/1.1 200 OK Date: Thu, 21 Dec 2017 06:46:42 GMT Server: Apache/2.4.29 (Unix) PHP/7.1.6 Last-Modified: Thu, 19 Oct 2017 09:44:16 GMT ETag: "976c5-55be33310dc00" Accept-Ranges: bytes Content-Length: 620229 Cache-Control: max-age=86400 Expires: Fri, 22 Dec 2017 06:46:42 GMT Content-Type: image/jpeg
說明: 當前時間「Date: Tue, 01 Aug 2017 10:17:36 GMT」,緩存時長「Cache-Control: max-age=86400s」,失效時間「Expires: Wed, 02 Aug 2017 10:17:36 GMT」,即png格式圖片緩存時長爲1天。
正常狀況下,根本就沒有必要記錄這一項,畢竟我們大都根據虛擬主機來設置相應的訪問日誌,但也有個別的狀況,好比 ServerName *.abc.com這樣泛解析的形式,因此有必要記錄一下用戶請求的域名究竟是哪一個。 而apache的LogFormat 中正好有一項值知足了這個需求。即 %V 這裏是大寫的V ,小寫的v 記錄的是我們在虛擬主機中設置的ServerName ,這個的確是沒有必要記錄的。
需求: 把相似請求 www.aaa.com/aaa/... 這樣的請求才記錄日誌。
方法: 在httpd.conf 或者 相關的虛擬主機配置文件中添加:
SetEnvIf Request_URI "^/aaa/.*" aaa-request CustomLog "|/usr/local/apache/bin/rotatelogs -l /usr/local/apache/logs/aaa-access_%Y%m%d.log 86400" combined env=aaa-request
原理和不記錄圖片等靜態訪問的日誌( http://www.lishiming.net/thread-561-1-1.html )是同樣的。
默認狀況下log日誌格式爲:
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
,其中%h 是記錄訪問者的IP,若是在web的前端有一層代理,那麼這個%h其實就是代理機器的IP,這不是咱們想要的。咱們須要的是記錄客戶端的真實IP,「%{X-FORWARDED-FOR}i 」該字段會記錄客戶端真實IP,因此log日誌的格式應改成:
LogFormat "%h %{X-FORWARDED-FOR}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
語法: rotatelogs [ -l ] logfile [ rotationtime [ offset ]] rotatelogs [ filesizeM ] logfile
選項: -l:使用本地時間代替GMT時間做爲時間基準。注意:在一個改變GMT偏移量(好比夏令時)的環境中使用-l會致使不可預料的結果。因此必定要加上-l 不然出現的日誌時間和實際時間是相差8小時的。 rotationtime:日誌文件滾動的以秒爲單位的間隔時間。 offset:相對於UTC的時差的分鐘數。若是省略,則假定爲」0″並使用UTC時間。好比,要指定UTC時差爲」-5小時」的地區的當地時間,則此參數應爲」-300″。 filesizeM:指定以filesizeM文件大小滾動,而不是按照時間或時差滾動。
使用cronolog命令:
一:每一天創建一個新日誌 CustomLog "|bin/cronolog logs/access_%Y%m%d.log" combined 二:每小時創建一個新日誌 CustomLog "|bin/cronolog logs/access_%Y%m%d%h.log" combined