<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>等待喝彩_OnEcho &#187; 优化</title>
	<atom:link href="http://www.onecho.com/tag/%e4%bc%98%e5%8c%96/feed" rel="self" type="application/rss+xml" />
	<link>http://www.onecho.com</link>
	<description>http://www.onecho.com  回声的启示</description>
	<lastBuildDate>Fri, 16 Jul 2010 14:18:16 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>网站优化：压缩篇</title>
		<link>http://www.onecho.com/2010-03-09/648.html</link>
		<comments>http://www.onecho.com/2010-03-09/648.html#comments</comments>
		<pubDate>Tue, 09 Mar 2010 04:45:32 +0000</pubDate>
		<dc:creator>Kenami</dc:creator>
				<category><![CDATA[网络编程]]></category>
		<category><![CDATA[gzip]]></category>
		<category><![CDATA[mod_deflate]]></category>
		<category><![CDATA[优化]]></category>
		<category><![CDATA[压缩]]></category>

		<guid isPermaLink="false">http://www.onecho.com/2010-03-09/648.html</guid>
		<description><![CDATA[今天在站长站看到PHP如何使用gzip压缩页面的文章，自己研究了一下，基本有两种压缩页面的方法：]]></description>
			<content:encoded><![CDATA[<p>今天在站长站看到PHP如何使用gzip压缩页面的文章，自己研究了一下，基本有两种压缩页面的方法：</p>
<p>一、使用PHP，开启gzip功能进行压缩</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">Extension_Loaded</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'zlib'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #990000;">Ob_Start</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ob_gzhandler'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">Header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-type: text/html&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;head&gt;
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=gb2312&quot; /&gt;
&lt;title&gt;无标题文档&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><span style="color: #000088;">$i</span><span style="color: #339933;">&lt;</span><span style="color: #cc66cc;">10000</span><span style="color: #339933;">;</span><span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Hello World!'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;/body&gt;
&lt;/html&gt;
<span style="color: #000000; font-weight: bold;">&lt;?PHP</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">Extension_Loaded</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'zlib'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #990000;">Ob_End_Flush</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>这种方法只能压缩html页面，对css、js等文件没有压缩<span id="more-648"></span></p>
<p>二、利用apache的mod_deflate模块</p>
<p>mod_deflate的安装：</p>
<p>找到mod_deflate.c文件，执行：</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">/usr/local/apache2/bin/apxs -i -c -a mod_deflate.c</pre></div></div>

<p>此时在apache的配置文件httpd.conf 里面会加了这样一行：</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">LoadModule deflate_module     modules/mod_deflate.so</pre></div></div>

<p>说明mod_deflate模块加载成功，此时要重启apache</p>
<p>我们在.htaccess 文件里面设置，开启对mod_deflate 的支持</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">&lt;/IfModule&gt;
&lt;ifmodule mod_deflate.c&gt;
AddOutputFilter DEFLATE html xml php js css
&lt;/ifmodule&gt;</pre></div></div>

<p> 或者</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">&lt;/IfModule&gt;
&lt;ifmodule mod_deflate.c&gt;
AddOutputFilterByType DEFLATE text/html text/xml application/x-httpd-php application/x-javascript text/css
&lt;/ifmodule&gt;</pre></div></div>

<p>另外如果方便可以在httpd.conf里面设置，当然自己要有主机的root权限：</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">&lt;IfModule mod_deflate.c&gt;
DeflateCompressionLevel 7
AddOutputFilterByType DEFLATE text/html text/plain text/xml application/x-httpd-php
AddOutputFilter DEFLATE css js
#DeflateFilterNote Input instream
#DeflateFilterNote Output outstream
#DeflateFilterNote Ratio ratio
#LogFormat '&quot;%r&quot; %{outstream}n/%{instream}n (%{ratio}n%%)' deflate
#CustomLog logs/deflate_log.log deflate
&lt;/IfModule&gt;</pre></div></div>

<p> </p>
<p>或者：</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">&lt;IfModule mod_deflate.c&gt;
DeflateCompressionLevel 9
SetOutputFilter DEFLATE
#DeflateFilterNote Input instream
#DeflateFilterNote Output outstream
#DeflateFilterNote Ratio ratio
#LogFormat '&quot;%r&quot; %{outstream}n/%{instream}n (%{ratio}n%%)' deflate
#CustomLog logs/deflate_log.log deflate
&lt;/IfModule&gt;</pre></div></div>

<p>如果将#号去掉，可以在logs/deflate_log.log日志文件中看到文件压缩前后的字节数、压缩比<br />
例如：&#8221;GET /index.html HTTP/1.1&#8243; 49373/276249 (17%)<br />
压缩前的字节数为276249，压缩后的字节数为49373，压缩比为17%</p>
]]></content:encoded>
			<wfw:commentRss>http://www.onecho.com/2010-03-09/648.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
