如何更大限度地提高网站响应速度,其中一个办法就是启用web 服务器的内容压缩功能。
下面的案例在未启用前347毫秒。
General Request URL:http://www.soosmart.com/topic/add.html Request Method:GET Status Code:200 OK Remote Address:118.123.166.150:80 Response Headers Cache-Control:private Connection:Keep-Alive Content-Length:7514 Content-Type:text/html; charset=utf-8 Date:Sat, 28 May 2016 00:46:09 GMT Expires:Thu, 19 Nov 1981 08:52:00 GMT Keep-Alive:timeout=5, max=99 Pragma:no-cache Server:Apache/2.4.7 (Unix) PHP/5.5.8 X-Powered-By:BOOKSMART Request Headers Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 Accept-Encoding:gzip, deflate, sdch Accept-Language:en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4
进入httpd.conf通过设置mod_deflate.
LoadModule deflate_module modules/mod_deflate.so
<ifmodule mod_deflate.c> DeflateCompressionLevel 6 #压缩率, 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>
如果之前没有安装mod_deflate的话,可以通过apxs来安装
[apache2 home]/bin/apxs -i -c -a [apacheSRC]/modules/filters/mod_deflate.c
重启apache后效果明显:
官方文档:http://httpd.apache.org/docs/current/mod/mod_deflate.html