网页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服务器的性能;