React apache 静态文件缓存实现(亲测可用)

在. .haccess 中配置直接添加  

设置了   Cache-Control 会覆盖 Expires 的过期时间

效果就是,关掉浏览器重新打开访问会读取  Cache-Control(from disk cache) 缓存 从硬盘加载,  直接刷新访问(不是强刷)的情况下会读取 from memory cache

<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|woff|js|css|swf)$">
	Header set Cache-Control "max-age=31536000"
    #31536000 秒等于1年
	FileETag MTime Size
</FilesMatch>

<IfModule expires_module>
    #打开缓存
    ExpiresActive on 
    ExpiresByType text/html "access plus 12 months"
    ExpiresByType image/gif  "access plus 12 months"
    ExpiresByType image/svg  "access plus 12 months"
    ExpiresByType image/jpeg "access plus 12 months"
    ExpiresByType image/png "access plus 12 months"
    ExpiresByType text/css "now plus 12 months"
    ExpiresByType application/x-javascript "now plus 12 months"
    ExpiresByType application/x-shockwave-flash "now plus 12 months”
    #其他数据不缓存
    ExpiresDefault "now plus 0 min"
  </IfModule>






<IfModule expires_module>
# 启用有效期控制
ExpiresActive On
# 图片
ExpiresByType image/jpg "access plus 1 month" 
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 month"
# css/js
ExpiresByType text/css "access plus 4 weeks"
ExpiresByType text/javascript "access plus 4 weeks"
# html
ExpiresByType text/html "access plus 2 days"
  </IfModule>
  #开启缓存
    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/x-shockwave-flash "now plus 2 hours”
    #其他数据不缓存
    ExpiresDefault "now plus 0 min"
缓存起始点
access   从当前访问时间开始
now (equivalent to 'access')  相当于access
modification   从修改时间算起
缓存时间单位
years
months
weeks
days
hours
minutes
seconds

前提要打开 这几个插件  在  httpd.conf 中:

 

LoadModule expires_module modules/mod_expires.so

LoadModule cache_module modules/mod_cache.so
LoadModule cache_disk_module modules/mod_cache_disk.so

这个 开启:

在apache  中开启:

这样就 完成了.如果实在本地呢  也可以直接在  httpd.conf 中加 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值