Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does server side GZipping work?

You might know that HTML related file formats are compressed using GZip compression, server side, (by mod_gzip on Apache servers), and are decompressed by compatible browsers. ("content encoding")

Does this only work for HTML/XML files? Lets say my PHP/Perl file generates some simple comma delimited data, and sends that to the browser, will it be encoded by default?

What about platforms like Silverlight or Flash, when they download such data will it be compressed/decompressed by the browser/runtime automatically? Is there any way to test this?

like image 879
Robin Rodricks Avatar asked Aug 09 '09 18:08

Robin Rodricks


1 Answers

Does this only work for HTML/XML files?

No : it is quite often used for CSS and JS files, for instance -- as those are amongst the biggest thing that websites are made of (except images), because of JS frameworks and full-JS applications, it represents a huge gain!

Actually, any text-based format can be compressed quite well (on the opposite, images can not, for instance, as they are generally already compressed) ; sometimes, JSON data returned from Ajax-requests are compressed too -- it's text data, afterall ;-)

Lets say my PHP/Perl file generates some simple comma delimited data, and sends that to the browser, will it be encoded by default?

It's a matter of configuration : if you configured your server to compress that kind of content, it'll probably be compressed :-)
(If the browser says it accepts gzip-encoded data)


Here's a sample of configuration for Apache 2 (using mod_deflate) that I use on my blog :

<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript application/x-javascript application/xml
</IfModule>

Here, I want html/xml/css/JS te be compressed.

And here is the same thing, plus/minus a few configuration options I used once, under Apache 1 (mod_gzip) :

<IfModule mod_gzip.c>
    mod_gzip_on                   Yes
    mod_gzip_can_negotiate        Yes

    mod_gzip_minimum_file_size    256
    mod_gzip_maximum_file_size    500000

    mod_gzip_dechunk              Yes

    mod_gzip_item_include         file       \.css$
    mod_gzip_item_include         file       \.html$
    mod_gzip_item_include         file       \.txt$
    mod_gzip_item_include         file       \.js$
    mod_gzip_item_include         mime       text/html

    mod_gzip_item_exclude         mime       ^image/
</IfModule>

Things that can be noticed here are that I don't want too small (the gain wouldn't be quite important) or too big (would eat too much CPU to compress) files to be compressed ; and I want css/html/txt/js files to be compressed, but not images.


If you want you comma-separated data to be compressed the same way, you'll have to add either it's content-type or it's extension to the configuration of your webserver, to activate gzip-compression for it.

Is there any way to test this?

For any content returned directly to the browser, Firefox's extensions Firebug or LiveHTTPHeaders are a must-have.

For content that doesn't go through the standard communication way of the browser, it might be harder ; in the end, you may have to end up using something like Wireshark to "sniff" what is really going through the pipes... Good luck with that!

What about platforms like Silverlight or Flash, when they download such data will it be compressed/decompressed by the browser/runtime automatically?

To answer your question about Silverlight and Flash, if they send an Accept header indicating they support compressed content, Apache will use mod_deflate or mod_gzip. If they don’t support compression they won’t send the header. It will “just work.” – Nate

like image 50
Pascal MARTIN Avatar answered Sep 20 '22 13:09

Pascal MARTIN