3

I have a Liquid Web VPS account, I've made sure that mod_deflate is installed and running/active.

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?

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

.htaccess file:

<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>
DM.
  • 111
  • 1
  • 1
  • 8

6 Answers6

5

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

DM.
  • 111
  • 1
  • 1
  • 8
2

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.

Savageman
  • 9,257
  • 6
  • 40
  • 50
  • 2
    Right. But if mod_deflate is setup to compress everything, that would include text/html too, no? Static HTML files (served as text/html) are being compressed, just nothing output by PHP, whether it be text/html or anything else. – DM. May 14 '10 at 16:38
  • @DM: Are you actually seeing this through testing, or are you just assuming it's not compressing? – Powerlord May 14 '10 at 16:59
  • @RBemrose Have a look at http://DMred.com - css and js files get compressed, PHP files do not. – DM. May 14 '10 at 17:03
0

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

Artefacto
  • 96,375
  • 17
  • 202
  • 225
  • Yeah, I'm aware of that. I was hoping there was a way to have mod_deflate take care of it though...?... (Does zlib check if the browser can accept gzip before compressing?) – DM. May 14 '10 at 16:40
  • It does. And I believe so does PHP. – Artefacto May 14 '10 at 16:41
  • In my testing, doing the compression in PHP with the zlib.output_compression setting is slower/less efficient than doing it in the server with mod_deflate (or nginx with the gzip module). You also have the drawback that you'll still need to do it in the server for static files (eg Javascript/CSS) anyway. – thomasrutter Jan 24 '14 at 06:50
0

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.

bobince
  • 528,062
  • 107
  • 651
  • 834
  • 4
    Adding "AddOutputFilterByType DEFLATE text/html text/plain text/xml" to the top of the .htaccess doesn't help. PHP output (being sent as text/html) still is uncompressed... – DM. May 14 '10 at 17:10
  • AIUI it should compress. Certainly works for me with CGI or WSGI output generating HTML. Are you sure there's not some other config elsewhere interfering with it? – bobince May 14 '10 at 17:18
  • 2
    that's what I'm wondering - if there's some other config somewhere that;s interfering. I was hoping it was a simple oversight on my part, something small, but I'm starting to think maybe not... – DM. May 14 '10 at 17:23
  • @bobince,could you please give us config example. – kittygirl Nov 05 '18 at 07:50
0

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.

Karen Zilles
  • 7,633
  • 3
  • 34
  • 33
0

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

Udo Held
  • 12,314
  • 11
  • 67
  • 93
Samer Ata
  • 1,027
  • 1
  • 12
  • 11