如何在apache2下启用HTTP压缩?

我如何configurationApache2来支持HTTP压缩

这需要deflate模块,所以启用。 在Ubuntu下(这是我使用的),那个命令是a2enmod deflate

然后,在ProxyDirectory指令中使用SetOutputFilter

 <ProxyMatch "^http://localhost:8080/($|app/)"> Order allow,deny Allow from all SetOutputFilter Deflate </ProxyMatch> 

要么:

 <Directory /var/www> Order allow,deny Allow from all SetOutputFilter Deflate </Directory> 

作为链接到笔记的维基百科条目,可以使用mod_deflatemod_gzip 。 在这里看到一个mod_gzip的例子 ,这是我使用的。

 <VirtualHost *:80> ... DeflateBufferSize 16384 DeflateCompressionLevel 5 DeflateMemLevel 9 DeflateWindowSize 15 <Location / > AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/x-javascript </Location> ... </VirtualHost> 

取决于你的部署,把它放在你的conf.d或extras文件夹中作为mod_deflate.conf:

 # Enable mod_deflate (gzip) SetOutputFilter DEFLATE # Do not attempt to compress the following extensions SetEnvIfNoCase Request_URI \ \.(?:gif|jpe?g|png|swf|flv|zip|gz|tar|mp3|mp4|m4v)$ no-gzip dont-vary 

重新启动Apache和mod_deflate将发生在所有不符合上述正则expression式的扩展,这将减less整体CPU开销,因为它阻止mod_deflate试图压缩格式,你将看到很less或没有压缩(因为它已经被压缩)。

要查看究竟是什么被压缩,以及压缩比率,把它放在你的

 # Deflate accurate logging DeflateFilterNote Input instream DeflateFilterNote Output outstream DeflateFilterNote Ratio ratio LogFormat '"%r" %{outstream}n/%{instream}n (%{ratio}n%%)' deflate CustomLog logs/yourdomain_deflate.log