如何在apache2下启用HTTP压缩?


Answers:


8

这需要deflate模块,因此启用它。在Ubuntu(这是我正在使用的)下,该命令为a2enmod deflate

然后,SetOutputFilter在您的ProxyDirectory指令上使用:

<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>

1
对于其他发行版,可以使用“ LoadModule deflate_module /path/to/module/mod_deflate.so”加载模块-一个好的答案只需几分钱。

4

作为您链接到注释的Wikipedia条目,您可以使用mod_deflatemod_gzip。在这里,请参见mod_gzip示例


1
<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>

1

根据您的部署,将其作为mod_deflate.conf放置在conf.d或extras文件夹中:

# 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将出现在所有与上述正则表达式不匹配的扩展上,这减少了总体CPU开销,因为它阻止了mod_deflate尝试压缩几乎看不到压缩的格式(因为它已经被压缩)。

要查看压缩的确切内容和压缩率,请将其放置在

# 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
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.