1. 程式人生 > >LAMP-靜態元素過期時間

LAMP-靜態元素過期時間

apache

當用戶訪問網站的時候一些圖片會自動緩存在電腦瀏覽器緩存中,當下一次訪問的時候就不必要再次加載了。當超時或更新時,會重新請求加載。

1、配置虛擬主機

[[email protected] ~]# vi /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
<VirtualHost *:80>
    DocumentRoot "/data/www/abc.com"
    ServerName abc.com
    ServerAlias www.abc.com
    ErrorLog "logs/abc.com-error_log"
<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>
    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/abc.com-access_%Y%m%d.log 86400" combined env=!img
</VirtualHost>

2、檢查重新加載

[[email protected] ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[[email protected] ~]# /usr/local/apache2.4/bin/apachectl -M |grep -i expire
[[email protected] ~]# vi /usr/local/apache2.4/conf/httpd.conf
LoadModule expires_module modules/mod_expires.so  ##取消註釋啟用
[[email protected]
/* */ ~]# /usr/local/apache2.4/bin/apachectl -M |grep -i expire expires_module (shared) [[email protected] ~]# /usr/local/apache2.4/bin/apachectl graceful

3、驗證效果

[[email protected] abc.com]# curl -x127.0.0.1:80 abc.com/123.gif -I
HTTP/1.1 200 OK
Date: Fri, 21 Jul 2017 11:19:21 GMT
Server: Apache/2.4.27 (Unix) PHP/7.1.6
Last-Modified: Fri, 21 Jul 2017 11:19:21 GMT
ETag: W/"8c5-555b2e6023fc0"
Accept-Ranges: bytes
Content-Length: 2245
Cache-Control: max-age=86400            ##最大老化時間86400秒,也就是1天
Expires: Sat, 22 Jul 2017 11:19:21 GMT  ##過期時間
Content-Type: image/gif


本文出自 “Gorilla Grodd” 博客,請務必保留此出處http://juispan.blog.51cto.com/943137/1952835

LAMP-靜態元素過期時間