apache 开启Gzip网页压缩

网页gzip压缩是个什么概念呢?如果你还没接触到,讲文字全是废话,先来看两张图看着实在;

网页压缩前:

白俊遥博客

网页压缩后:

白俊遥博客


从图中就可看出网页压缩后这加载时间是成倍的缩减,下面就是就是要讲解如何开启gzip压缩;

实例环境:

    服务器:centos6.5

    apche:2.2.15

gzip压缩需要 deflate_module和headers_module的支持;系统默认就是开启的;需要做的是在httpd.conf配置项添加规则;

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@iZ28qa8jt4uZ conf] # vim /etc/httpd/conf/httpd.conf  #修改配置项
#在最后边添加如下内容
<Location />
      AddOutputFilterByType DEFLATE text /plain
      AddOutputFilterByType DEFLATE text /css
      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 text /html
< /Location >
[root@iZ28qa8jt4uZ conf] # service httpd restart    #重启apache

ok就这么简单即可;然后通过火狐控制台的网络查看是否有Content-Encoding:gzip 这条

白俊遥博客

或者通过站长工具来查看:http://tool.chinaz.com/Gzips/

白俊遥博客


什么?你说你用的是虚拟空间;不能修改httpd.conf ?好吧,使用.htaccess总可以吧,好人做到底;在项目根目录下建一个.htaccess文件

里面添加如下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<IfModule mod_deflate.c>
# Insert filters
AddOutputFilterByType DEFLATE text /plain
AddOutputFilterByType DEFLATE text /html
AddOutputFilterByType DEFLATE text /xml
AddOutputFilterByType DEFLATE text /css
AddOutputFilterByType DEFLATE application /xml
AddOutputFilterByType DEFLATE application /xhtml +xml
AddOutputFilterByType DEFLATE application /rss +xml
AddOutputFilterByType DEFLATE application /javascript
AddOutputFilterByType DEFLATE application /x-javascript
AddOutputFilterByType DEFLATE application /x-httpd-php
AddOutputFilterByType DEFLATE application /x-httpd-fastphp
AddOutputFilterByType DEFLATE image /svg +xml
 
# Drop problematic browsers
BrowserMatch ^Mozilla /4  gzip -only-text /html
BrowserMatch ^Mozilla /4 .0[678] no- gzip
BrowserMatch bMSI[E] !no- gzip  ! gzip -only-text /html
 
# Make sure proxies don't deliver the wrong content
Header append Vary User-Agent  env =!dont-vary
< /IfModule >

如果是自己的服务器还想使用.htaccess;那需要在httpd.conf 里面开启.htaccess;

修改httpd.conf的302行左右 AllowOverride None 改为 AllowOverride All:

1
2
3
4
<Directory />
     Options FollowSymLinks
     AllowOverride All     
< /Directory >

还有大约340左右的AllowOverride None 改为 AllowOverride All:

1
AllowOverride All:

但是如果是自己的服务器,尽量修改http.conf,而不是使用.htaccess。因为开启.htaccess后apache每次都要检测.htaccess文件会稍微降低httpd服务器的性能;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值