- <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 下可以看到
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)
可以看到有效期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>
复制代码 重启apache就可以了 刷新chrome可以看到 size 的大小
压缩比例还是不错的!
|