apache 下配置gzip 和静态文件缓存

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">在很多情况下需要客户短开启缓存,文件打开gzip压缩来加快页面加载速度。</span>

一.首先看下开启页面缓存(也就是经常说的页面过期时间)

1.apache中需要增加

 LoadModule expires_module modules/mod_expires.so

  如果是前面加了警号需要把警号去掉

2.增加如下代码

   <IfModule expires_module>
    #打开缓存
    ExpiresActive on 
    #文件缓存864000/3600/24=10天
    ExpiresByType text/css A864000
    ExpiresByType application/x-javascript A864000
    ExpiresByType application/javascript A864000
    ExpiresByType text/html A864000
    ExpiresByType image/jpeg A864000
    ExpiresByType image/gif A864000
    ExpiresByType image/png A864000
    ExpiresByType image/x-icon A864000
</IfModule>

ExpiresByType  是文件的MIME类型 可以直接指定过期的类型


重启apache 服务器 service httpd restart

重新刷新几下网页

在chrome 下可以看到 

  1. Cache-Control:max-age=864000
    Connection:close
    Date:Wed, 28 May 2014 06:57:55 GMT
    ETag:"d9233e-452-4fa5f3d0de300"
    Expires:Sat, 07 Jun 2014 06:57:55 GMT
    Server:Apache/2.2.15 (CentOS)
    

  2. 可以看到有效期Cache-control;过期时间是10天


二. 开启gzip压缩
     apache有两种压缩方式 mod_deflate  和  mod_gzip  
    以 mod_deflate为例子 先看看是否开启了对应的模块  
   LoadModule deflate_module modules/mod_deflate.so
如果已经开启了
直接在 httpd.conf 或者在 .htaccess 文件中 增加
<ifmodule mod_deflate.c>
DeflateCompressionLevel 6 
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/atom_xml
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/x-httpd-php
AddOutputFilterByType DEFLATE image/svg+xml
</ifmodule>

  1. 重启apache就可以了 刷新chrome可以看到 size 的大小

  2. 压缩比例还是不错的!
关于 mod_deflate  和  mod_gzip  可以参照 http://www.xmydlinux.org/201108/604.html  
MIME 类型可以参照 http://www.iana.org/assignments/media-types/media-types.xhtml






  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值