PHP禁用zlib.output_compression

时间:2015-03-12 16:39:41

标签: php apache

我的网站在ini文件中将zlib.output_compression设置为On,它会在发送到浏览器时压缩所有响应。

如何仅对二进制文件,PDF文件和Excel文件禁用压缩,以便在下载文件时正确输出到浏览器?

2 个答案:

答案 0 :(得分:2)

zlib.output_compression = on/off

和PHP

ini_set("zlib.output_compression", "On");
ini_set("zlib.output_compression", "Off");

查看此详细信息 - http://php.net/manual/en/zlib.configuration.php#ini.zlib.output-compression

答案 1 :(得分:2)

在服务器中使用.conf文件,将压缩限制为正确的MIME类型

例如/etc/httpd/conf.d/enable_mod_deflate.conf

# mod_deflate configuration
<IfModule mod_deflate.c>
  # Restrict compression to these MIME types
  AddOutputFilterByType DEFLATE text/plain
  AddOutputFilterByType DEFLATE text/html
  AddOutputFilterByType DEFLATE application/xhtml+xml
  AddOutputFilterByType DEFLATE text/xml
  AddOutputFilterByType DEFLATE application/xml
  AddOutputFilterByType DEFLATE application/xml+rss
  AddOutputFilterByType DEFLATE application/x-javascript
  AddOutputFilterByType DEFLATE text/javascript
  AddOutputFilterByType DEFLATE text/css
  AddOutputFilterByType DEFLATE image/png
  AddOutputFilterByType DEFLATE image/gif
  AddOutputFilterByType DEFLATE image/jpeg

  # Level of compression (Highest 9 - Lowest 1)
  DeflateCompressionLevel 9

  # Netscape 4.x has some problems.
  BrowserMatch ^Mozilla/4 gzip-only-text/html

  # Netscape 4.06-4.08 have some more problems
  BrowserMatch ^Mozilla/4\.0[678] no-gzip

  # MSIE masquerades as Netscape, but it is fine
  BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html

  <IfModule mod_headers.c>
    # Make sure proxies don't deliver the wrong content
    Header append Vary User-Agent env=!dont-vary
  </IfModule>
</IfModule>

不要忘记重启服务器

sudo service httpd restart