apache 下配置gzip 和静态文件缓存
2014-05-28 17:03
393 查看
<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 的大小
压缩比例还是不错的!
关于 mod_deflate
和 mod_gzip 可以参照 http://www.xmydlinux.org/201108/604.html MIME
类型可以参照 http://www.iana.org/assignments/media-types/media-types.xhtml
相关文章推荐
- apache 配置用户认证 域名跳转 日志 静态缓存文件 防盗链接
- Apache配置用户认证、域名跳转、日志轮询、静态文件缓存、防盗链
- apache配置静态文件缓存
- LAMP第二部分apache配置2(访问日志切割 and 配置静态文件缓存 and配置防盗链)
- apache配置静态文件缓存
- Apache配置静态缓存
- django+apache配置网站的robots.txt和sitemaps等理静态文件
- apache2.0x 开启gzip压缩和http缓存的配置方法
- apache2.0x 开启gzip压缩和http缓存的配置方法
- 2.1-apache配置静态缓存
- apache 服务器设置伪静态 配置文件php
- apache mod_expires缓存静态文件
- apache mod_expires缓存静态文件
- 修改Apache配置文件开启gzip压缩传输
- Apache配置静态缓存
- 修改Apache配置文件开启gzip压缩传输
- 修改Apache配置文件开启gzip压缩传输
- apache中使用.htaccess文件缓存图片的配置方法
- apache下用expires_module让浏览器缓存静态文件
- Apache开启gzip压缩和http缓存的配置