配置静态元素的过期时间

配置静态元素过期时间

浏览器去访问网站时会把静态文件(图片,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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值