Apache启用性能优化——启用Gzip,JS压缩

在做个项目中,发现人家的JQuery1.4.2-min.js在Firebug中只有不到24KB大小。如下图:

而实际jQuery的官方文件的大小要有70KB的样子。

由于直接通过Firebug查看JS代码,发现对应的min.js代码没有发生任何改动和压缩扰码。(JQuery官方已经进了删除回车,多余空格等优化操作,因此,从压缩代码的角度,再提升这么的压缩比实在不现实),因此,考虑到是浏览器和WebService端进行的压缩处理。

最成熟的Apache的压缩解决方案就是采用mod_deflate 模块的GZip压缩。作为,曾经写过Apache模块的我来说。对于这个Module已经是耳熟能详了。其配置方法也十分简单。

首先要确保自己的Apache已经加载了deflate模块。

[root@localhost ~]# apachectl -M | grep deflate
Loaded Modules:
 core_module (static)
 mpm_prefork_module (static)
 http_module (static)
 .......
 expires_module (shared)
 deflate_module (shared)
 headers_module (shared)
 ......
Syntax OK
[root@localhost ~]#

如果没有加载,则需要自行编译加载,这个不在本文的论述范围之内,有需要的朋友,请自行询问谷歌大神。

加载mod_deflate.so模块,并进行配置

[root@localhost ~]# vi /etc/httpd/conf/httpd.conf
#Add deflate module for enable GZIP function
LoadModule deflate_module     modules/mod_deflate.so

#Add GZIP config filetype
AddOutputFilterByType DEFLATE text/html text/php text/png text/jpg text/plain text/css text/xml text/javascript

#Set compression level
DeflateCompressionLevel 9

#Hook on output filter
SetOutputFilter DEFLATE
[root@localhost ~]#

重启Apache服务,加载模块

[root@localhost ~]# /etc/init.d/httpd restart

启用压缩后,效果: