1. Apache static element expiration time
- Most of the elements of the website are static files, such as pictures, css, js, etc. These elements do not need to be recorded
mkdir /data/wwwroot/123.com/images //创建目录,并在这目录下上传一个图片
curl -x127.0.0.1:80 -I 123.com/images/123.jpg
then check the log
tail /usr/local/apache2.4/logs/123.com-access_log
At this time, the log is recorded
vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
Change the virtual host configuration file to the following:
<VirtualHost *:80>
DocumentRoot "/data/wwwroot/www.123.com"
ServerName www.123.com
ServerAlias 123.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
</VirtualHost>
reload config file
/usr/local/apache2.4/bin/apachectl -t
/usr/local/apache2.4/bin/apachectl graceful
Test again, the log file no longer records the access record of the jpg file
2. Access log cutting
The log keeps recording and will fill up the entire disk one day, so it is necessary to let it automatically cut and delete the old log files
- Change the virtual host configuration file to the following:
<VirtualHost *:80> DocumentRoot "/data/wwwroot/www.123.com" ServerName www.123.com ServerAlias 123.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 "|/usr/local/apache2.4/bin/rotatelogs -l logs/123.com-access_%Y%m%d.log 86400" (-l是指定当前系统时间分隔,86400是指一天之后重新生成另一个,以每天0点开始) combined env=!img </VirtualHost>
- reload config file
/usr/local/apache2.4/bin/apachectl -t /usr/local/apache2.4/bin/apachectl graceful
- Visit the website casually, you can see the log file with date prefix generated in the llogs directory
3. Expiration time of static elements
When the browser accesses the pictures of the website, it will cache the static files in the local computer, so that the next time you visit, you don't have to download it remotely.
Here we set the expiration time of various element caches
- Add configuration
<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>
- Due to the need to use expires_module
/usr/local/apache2.4/bin/apachectl -M | grep expires
If it does not exist, open the apache configuration file to load the plugin
vim /usr/local/apache2.4/conf/httpd.conf
- reload config file
/usr/local/apache2.4/bin/apachectl -t /usr/local/apache2.4/bin/apachectl graceful
- The return results of the test data before and after the modification are different. After the modification, Cache-Control: max-age=0 is the effective time of the cache. Now it is necessary to reload
Expires every time: the time to reload
- The return results of the test data before and after the modification are different. After the modification, Cache-Control: max-age=0 is the effective time of the cache. Now it is necessary to reload