配置靜態元素的過期時間

配置靜態元素過期時間

瀏覽器去訪問網站時會把靜態文件(圖片,css , js)默認緩存在電腦裏。這樣下次訪問時就不用再去遠程下載了。緩存多長時間?瀏覽器會有自己的機制,清空緩存。或者在遠程服務器端可以設置。

服務器通過expires模塊可以定義失效時間。

服務器定義靜態元素過期時間配置:

<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模塊,開關(ExpiresActive on ),爲某些類型的靜態元素配置過期時間,gif 的時間爲1天( ExpiresByType image/gif “access plus 1 days”),png的一天(ExpiresByType image/png “access plus 24 hours”),css的兩小時( ExpiresByType text/css “now plus 2 hour”)

打開網站(111.com/logo.png),這是會顯示304 
這裏寫圖片描述

在服務器段配置元素過期時間:

編輯虛擬主機配置文件:

[root@shuai-01 111.com]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf

<VirtualHost *:80>
    DocumentRoot "/data/wwwroot/111.com"
    ServerName 111.com
    ServerAlias www.111.com www.example.com
  #  <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>
        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
    ErrorLog "logs/111.com-error_log"
    CustomLog "|/usr/local/apache2.4/bin/rotatelogs -l logs/111.com-access_%Y%m%d.log 86400" combined env=!img
</VirtualHost>

這是一個模塊,先看這個模塊文件有沒有打開

[root@shuai-01 111.com]# /usr/local/apache2.4/bin/apachectl -M |grep expires

在主配置文件中加載模塊

[root@shuai-01 111.com]# vim /usr/local/apache2.4/conf/httpd.conf

LoadModule env_module modules/mod_env.so
LoadModule expires_module modules/mod_expires.so
LoadModule headers_module modules/mod_headers.so

檢查配置文件語法是否錯誤並重新加載

[root@shuai-01 111.com]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@shuai-01 111.com]# /usr/local/apache2.4/bin/apachectl graceful

這是在訪問網站(111.com/logo.png)

沒做過期和做了過期前後對比: 
沒做過期 
這裏寫圖片描述

做過期 
這裏寫圖片描述

在curl裏也能反映出來

[root@shuai-01 111.com]# curl -x127.0.0.1:80 111.com/logo.png -I
HTTP/1.1 200 OK
Date: Thu, 21 Dec 2017 14:46:37 GMT
Server: Apache/2.4.29 (Unix) PHP/5.6.30
Last-Modified: Thu, 21 Dec 2017 14:12:26 GMT
ETag: "1914-560da4a1b6680"
Accept-Ranges: bytes
Content-Length: 6420
Cache-Control: max-age=86400
Expires: Fri, 22 Dec 2017 14:46:37 GMT
Content-Type: image/png
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章