Rumah > Artikel > pembangunan bahagian belakang > Apache开启GZIP 压缩网页_PHP教程
二、Web服务器处理
通过查看HTTP头,我们可以快速判断使用的客户端浏览器是否支持接受gzip压缩。
若发送的HTTP头中出现以下信息,则表明你的浏览器支持接受相应的gzip压缩:
# mod_gzip
<span <</span><span ifModule </span><span mod_gzip.c</span><span ></span><span mod_gzip_on Yes mod_gzip_dechunk Yes mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$ mod_gzip_item_include handler ^cgi-script$ mod_gzip_item_include mime ^text/.* mod_gzip_item_include mime ^application/x-javascript.* mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.* </span><span <</span><span ifModule</span><span ></span>
(1)严格匹配文件类型
# mod_deflate:
<span <</span><span ifmodule </span><span mod_deflate.c</span><span ></span><span DeflateCompressionLevel 6 #压缩率, 6是建议值. AddOutputFilterByType DEFLATE text/plain AddOutputFilterByType DEFLATE text/html AddOutputFilterByType DEFLATE text/php 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 AddOutputFilterByType DEFLATE image/gif image/png image/jpe image/swf image/jpeg image/bmp # Don’t compress images and other #排除不需要压缩的文件 BrowserMatch ^Mozilla/4 gzip-only-text/html BrowserMatch ^Mozilla/4\.0[678] no-gzip BrowserMatch \bMSIE !no-gzip !gzip-only-text/html SetEnvIfNoCase Request_URI .(?:html|htm)$ no-gzip dont-varySetEnvIfNoCase #SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary SetEnvIfNoCase Request_URI .(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary SetEnvIfNoCase Request_URI .(?:pdf|doc)$ no-gzip dont-vary </span><span </</span><span ifmodule</span><span ></span>
(2)过滤文件类型
# mod_deflate:
<span <</span><span ifmodule </span><span mod_deflate.c</span><span ></span><span DeflateCompressionLevel 6 SetOutputFilter DEFLATE #压缩所有文件 #Don’t compress images and other #过滤不需要压缩文件 #SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary SetEnvIfNoCase Request_URI .(?:html|htm)$ no-gzip dont-vary SetEnvIfNoCase Request_URI .(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary SetEnvIfNoCase Request_URI .(?:pdf|doc)$ no-gzip dont-vary </span><span </</span><span IfModule</span><span ></span>
五、效果
没有启动Gzip压缩:
启动Gzip压缩: