經過虛擬主機配置vhost文件查看該虛擬機日誌文件目錄 javascript
訪問日誌記錄用戶的每個請求,在httpd配置文件定義日誌格式,是虛擬主機配置文件使用該格式名稱去定義日誌格式css
vim /usr/local/apache2.4/conf/httpd.conf //搜索LogFormat LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined LogFormat "%h %l %u %t \"%r\" %>s %b" common h 來源IP u 用戶 t 時間 r 動做 Refere 是由某連接進入該網站的連接 User-Agent 用戶訪問代理,好比瀏覽器、curl等 把該虛擬主機配置文件改爲以下: <VirtualHost *:80> DocumentRoot "/data/wwwroot/111.com" ServerName 111.com ServerAlias www.110.com CustomLog "logs/123.com-access_log" combined </VirtualHost> 這是虛擬主機日誌文件後加common [root@chy002 extra]# cat /usr/local/apache2.4/logs/111.com-access_log 192.168.212.130 - - [04/Mar/2018:01:11:00 +0800] "GET / HTTP/1.1" 200 7 192.168.212.1 - - [04/Mar/2018:04:20:04 +0800] "GET /favicon.ico HTTP/1.1" 404 209 192.168.212.1 - - [04/Mar/2018:04:20:05 +0800] "GET /favicon.ico HTTP/1.1" 404 209 192.168.212.1 - - [04/Mar/2018:04:20:07 +0800] "GET / HTTP/1.1" 200 7 ... ... 192.168.212.130 - - [04/Mar/2018:04:25:08 +0800] "GET / HTTP/1.1" 401 381 192.168.212.130 - chyuanliu [04/Mar/2018:04:26:16 +0800] "GET / HTTP/1.1" 200 7 127.0.0.1 - chyuanliu [04/Mar/2018:04:26:39 +0800] "GET HTTP://111.com HTTP/1.1" 200 7 192.168.212.1 - chyuanliu [04/Mar/2018:04:27:38 +0800] "GET / HTTP/1.1" 200 7 192.168.212.1 - chyuanliu [04/Mar/2018:04:27:41 +0800] "GET /favicon.ico HTTP/1.1" 404 209 加上combined後 192.168.212.1 - - [04/Mar/2018:17:59:39 +0800] "GET /favicon.ico HTTP/1.1" 301 238 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.4549.400 QQBrowser/9.7.12900.400"
默認狀況下log日誌格式爲: LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined 其中%h 是記錄訪問者的IP,若是在web的前端有一層代理,那麼這個%h其實就是代理機器的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
個人需求是,把相似請求 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
網站大多元素爲靜態文件,如圖片、css、js等,這些元素能夠不用記錄,能夠減小訪問日誌的記錄前端
修改虛擬主機配置文件,在日誌輸出上面輸入,定義環境變量。不須要模塊。java
<VirtualHost *:80> DocumentRoot "/data/wwwroot/www.111.com" ServerName 111.com ServerAlias www.110.com 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 #只要符合 img 的,日誌都不會記錄 </VirtualHost>
實驗:web
在/data/wwwroot/111.com目錄下建立一個.jpg結尾的文件,經過curl去訪問還文件,發現日誌沒有記錄,說明配置成功。apache
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 vim
日誌一直記錄總有一天會把整個磁盤佔滿,因此有必要讓它自動切割,並刪除老的日誌文件,不須要模塊。
把 vhost 虛擬主機配置文件改爲以下: 時間86400是天天切割,天天凌晨自動生成新的日誌文件。
CustomLog "|/usr/local/apache2.4/bin/rotatelogs -l logs/111.com-access_%Y%m%d.log 86400" combined env=!img
從新加載配置文件 -t, graceful
ls /usr/local/apache2.4/logs瀏覽器
瀏覽器訪問網站的圖片時會把靜態的文件緩存在本地電腦裏,這樣下次再訪問時就不用去遠程下載,增長配置緩存
<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模塊支持。bash
win平臺,谷歌瀏覽器Ctrl+F5 強制刷新緩存。
curl測試,看cache-control: max-age
[root@localhost www.dilei.com]# curl www.dilei.com/zxl.jpg -I HTTP/1.1 200 OK Date: Mon, 09 Jul 2018 14:53:00 GMT Server: Apache/2.4.33 (Unix) PHP/5.6.30 Last-Modified: Mon, 09 Jul 2018 05:54:25 GMT ETag: "12a864-5708aa5528640" Accept-Ranges: bytes Content-Length: 1222756 Cache-Control: max-age=86400 Expires: Tue, 10 Jul 2018 14:53:00 GMT Content-Type: image/jpeg