如何为 PHP 文件启用 mod_deflate?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2835818/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I enable mod_deflate for PHP files?
提问by DM.
I have a Liquid Web VPS account, I've made sure that mod_deflate is installed and running/active.
我有一个 Liquid Web VPS 帐户,我确保 mod_deflate 已安装并正在运行/处于活动状态。
I used to gzip my css and js files via PHP, as well as my PHP files themselves... However, I'm now trying to do this via mod_deflate, and it seems to work fine for all files except for PHP files. (Txt files work fine, css, js, static HTML files, just nothing that is generated via a PHP file.) How do I fix this?
我曾经通过 PHP 压缩我的 css 和 js 文件,以及我的 PHP 文件本身......但是,我现在尝试通过 mod_deflate 执行此操作,它似乎适用于除 PHP 文件之外的所有文件。(Txt 文件工作正常,css、js、静态 HTML 文件,只是没有通过 PHP 文件生成的内容。)我该如何解决这个问题?
(I used the "Compress all content" option under "Optimize Website" in cPanel, which creates an .htaccess file in the home directory (not public_html, one level higher than that) with exactly the same text as the "compress everything except images" example on http://httpd.apache.org/docs/2.0/mod/mod_deflate.html)
(我使用了 cPanel 中“优化网站”下的“压缩所有内容”选项,它在主目录(不是 public_html,比它高一级)中创建了一个 .htaccess 文件,其文本与“压缩除图像之外的所有内容”完全相同" http://httpd.apache.org/docs/2.0/mod/mod_deflate.html 上的示例)
.htaccess file:
.htaccess 文件:
<IfModule mod_deflate.c>
SetOutputFilter DEFLATE
<IfModule mod_setenvif.c>
# 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 \bMSIE !no-gzip !gzip-only-text/html
# NOTE: Due to a bug in mod_setenvif up to Apache 2.0.48
# the above regex won't work. You can use the following
# workaround to get the desired effect:
BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
# Don't compress images
SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary
</IfModule>
<IfModule mod_headers.c>
# Make sure proxies don't deliver the wrong content
Header append Vary User-Agent env=!dont-vary
</IfModule>
</IfModule>
回答by DM.
It's been a while since I posted this question - I ended up enabling zlib compression via PHP.ini so zlib compresses PHP output while mod_deflate compresses everything else.
我发布这个问题已经有一段时间了 - 我最终通过 PHP.ini 启用了 zlib 压缩,因此 zlib 压缩 PHP 输出,而 mod_deflate 压缩其他所有内容。
I'm thinking the reason it wasn't working (mod_deflate wasn't compressing PHP output) has to do with PHP running as CGI and not an Apache DSO...
我在想它不工作的原因(mod_deflate 没有压缩 PHP 输出)与 PHP 作为 CGI 而不是 Apache DSO 运行有关...
回答by Savageman
PHP files usually serves text/htmlcontent (although you can also generate images and pretty much everything). PHP files are never sent to the client: it gets processed to generate content (text/htmlor images).
You're looking to gzip this content.
PHP 文件通常提供text/html内容(尽管您也可以生成图像和几乎所有内容)。PHP 文件永远不会发送到客户端:它会被处理以生成内容(text/html或图像)。您正在寻找 gzip 此内容。
回答by Artefacto
You can add zlib.output_compression = Onto your php.ini configuration file. This will compress the output regardless of mod_deflate.
您可以添加zlib.output_compression = On到 php.ini 配置文件中。无论 mod_deflate 如何,这都会压缩输出。
回答by bobince
That whole config is wayobsolete. It can and should be deleted now: Netscape 4 hasn't been around for a long time, and setting Vary: User-Agentunnecessarily hinders caching.
这整个配置的方式已经过时了。现在可以而且应该删除它:Netscape 4 已经很长时间没有出现了,并且Vary: User-Agent不必要的设置会阻碍缓存。
The only mod_deflate config you need is one line to turn deflate on for compressible media types, either using AddOutputFilterto choose compressible files by extension, or AddOutputFilterByTypeto choose them by returned Content-Type.
您需要的唯一 mod_deflate 配置是一行打开可压缩媒体类型的 deflate,要么使用AddOutputFilter按扩展名选择可压缩文件,要么使用AddOutputFilterByType返回的Content-Type.
回答by Samer Ata
You need to move your mod_deflate directives outside Directory section. Define your rules globally for mod_deflatein your httpd.conf
您需要将 mod_deflate 指令移到 Directory 部分之外。mod_deflate在您的全局范围内定义您的规则httpd.conf
回答by Karen Zilles
Just in case anyone with the same issue ends up here.
以防万一有同样问题的人最终来到这里。
I turned on mod_deflate on my server. I could see from the response headers in my browser that text, css, and javascript files were being compressed with gzip. Html files were notbeing compressed according to the headers.
我在我的服务器上打开了 mod_deflate。我可以从浏览器的响应头中看到,文本、css 和 javascript 文件正在使用 gzip 进行压缩。未根据标头压缩Html 文件。
It turns out it was my virus protection. It was intercepting the incoming HTTP request, and if it saw <html>tags in the response it would decompress it and insert its own javascript into the page as part of its "Web Antivirus". It would strip the gzip header from the response and deliver it uncompressed.
原来这是我的病毒防护。它正在拦截传入的 HTTP 请求,如果它<html>在响应中看到标签,它将解压缩它并将其自己的 javascript 作为其“Web Antivirus”的一部分插入到页面中。它会从响应中去除 gzip 标头并在未压缩的情况下传送它。
So, the server was correctly gzipping everything, and my antivirus was unzipping it before it got to me.
因此,服务器正确地压缩了所有内容,并且我的防病毒软件在它到达我之前将其解压缩。

