How do I enable mod_deflate for PHP files?

末鹿安然 提交于 2019-12-02 00:02:27

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.

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

PHP files usually serves text/html content (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/html or images). You're looking to gzip this content.

You can add zlib.output_compression = On to your php.ini configuration file. This will compress the output regardless of mod_deflate.

That whole config is way obsolete. It can and should be deleted now: Netscape 4 hasn't been around for a long time, and setting Vary: User-Agent unnecessarily hinders caching.

The only mod_deflate config you need is one line to turn deflate on for compressible media types, either using AddOutputFilter to choose compressible files by extension, or AddOutputFilterByType to choose them by returned Content-Type.

Samer Ata

You need to move your mod_deflate directives outside Directory section. Define your rules globally for mod_deflate in your httpd.conf

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 not being compressed according to the headers.

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.

So, the server was correctly gzipping everything, and my antivirus was unzipping it before it got to me.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!