網站大多元素爲靜態文件,如圖片、css、js等,這些元素能夠不用記錄javascript
vhost原始配置php
<VirtualHost *:80> ServerAdmin test@163.com 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>
[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 test@163.com 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"
日誌一直記錄總有一天會把整個磁盤佔滿,因此有必要讓它自動切割,並刪除老的日誌文件。css
配置,使用Apache自帶的切割工具rotatelogs進行日誌文件切割html
[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 test@163.com 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
瀏覽器訪問網站的圖片時會把靜態的文件緩存在本地電腦裏,這樣下次再訪問時就不用去遠程下載了java
須要使用到expires_moduleweb
配置測試apache
[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