Save Bandwidth with gzip Compression

Browsers are equipped to serve compressed pages. Most hosts too have mod_gzip or zlib extensions installed to ensure you can avail of these features.

Compression ensures that your browser receives a smaller packet and so you effectively save bandwidth.

WordPress by default also comes with this feature that enables you to compress the webpages and send them to the browser.

You can find it under Options » Reading. Scroll to the bottom where you will find WordPress should compress articles (gzip) if browsers ask for them.

I’ve noticed upto 1 second difference between the loading times of my pages with this checked and unchecked.

After checking this, it is best to analyze your page to see if gzip is infact working or not. For this I use Web Page Analyzer.

In case you do have the above option checked and this is reporting that the files are not compressed, you will need to talk to your host.

Many of us have WP-Cache installed on our WordPress installation. When a page is requested for the first time, WP-Cache creates a cache file for the page. Subsequent requests are served from the cache. This ensures that the page doesn’t need to be recompiled, thus reducing the load on the server.

The problem with WP-Cache is that it doesn’t support GZip encoding. This means, that you need to first uncheck the above option that I talked about. This also means that you’re increasing the load time of your pages as well as consuming more bandwidth.

However, Mark Jaquith and Chris Stormer have found that a minor change in the the code of the plugin can turn on GZip encoding.

All you need to do is find the line

foreach ($meta->headers as $header) {

in wp-cache-phase1.php. This is line 35 in my file.

Just before this line you need to add:

if ( extension_loaded('zlib') ) ob_start('ob_gzhandler');

Now, your file will look like:

if ( extension_loaded('zlib') ) ob_start('ob_gzhandler');
foreach ($meta->headers as $header) {

Save the file and upload it and your set.

You can analyze the page again using the analyzer to check if the files are compressed.

Note that the above option in WordPress has to be disabled, or your pages will be royally messed up!

For those interested, the line of code we added checks if the zlib extension is installed. If it is, then it calls ob_gzhandler which gzips the output buffer and sends it to the browser.

Share this post with your friends!

24 Comments

  1. Do let me know how it works for you. Btw, I recommend not using WP-Cache unless you really need it. You get a whole new set of issues when you use it.

  2. Ah… Compressing pages using GZip sounds like a fantastic idea, but won’t it increase the CPU usage?

    I tried it on mine. My daily average has increased by nearly 2%

  3. Yes it will increase the CPU Usage, but it would also mean much faster loading of your pages for your visitors. In the end it is a tradeoff.

  4. Thanks for the helpful hint , I will try it on my website right away

    in the wp-config.php the variable WP_CACHE needs to be set to true also.

    define(?WP_CACHE?, true);

    correct me if I am wrong..

    thanks

  5. fields, you’re right about that. If you install wp-cache it will take care of adding the above line in your wp-config.php if it has write permissions. The guide above takes into account what needs to be done after wp-cache is installed.

  6. Hey Ajay,

    Great post, thanks. I was actually just contemplating turning on Gzip compression. This would be especially helpful for some of our post pages where we have LOTS of long and complex comments for lots of readers (example: ).

    Since you’ve been using a couple of days now, have you seen any issues at all?

    Thilak, point taken, but enhancing the user experience is always a wonderful thing – and gets us closer to that holy grail of LOTS MORE page views per visitor!!! 🙂

    Have an awesome day!
    Dan

  7. Ok, cool, thanks for the feedback. We’ll enable it then and see where it goes.

    Gzip should really make a difference for us since a lot of our articles/posts deal with topics that people have very strong emotional perspectives on… so we get a bunch of very long articulate comments. This is the example I was going to include in my comment above but forgot (feel free to edit out the URL if you’re not fuzzy with it). 🙂
    http://www.askdanandjennifer.com/relationship-advice/honesty-about-previous-sex-partners-how-much-do-you-really-need-to-share/

    Saving a second or two on load time sounds great. I’ll let you know how it ends up working out.

    BTW, just came across your other tech blog, very cool.

    Have an awesome day!
    Dan

  8. I’ve currently disabled it, but I ran it for nearly 15 days with absolutely no issues. Infact things ran a lot faster with that.

    Using GZip is great because your pages load faster for visitors and you also save on the bandwidth your site uses.

  9. hey there ajay I am testing out your modification and thanks again for publicizing it ..

    it looks like your code sample is reversed

    yours says
    1.foreach ($meta->headers as $header) {
    2.if ( extension_loaded(‘zlib’) ) ob_start(‘ob_gzhandler’);

    I think it should be
    1)if ( extension_loaded(?zlib?) )
    2) ob_start(?ob_gzhandler?);
    foreach ($meta->headers as $header) {

    am I wrong ? your text indicates otherwise but the code sample contradicts it ..

  10. Hi, you are right… this was an oversight on my part and have changed it. Now I hope people correct their installation!

  11. After doing that the second line indicating that the page is served from cache is not added to to end of pages (I mean <!– Cached page served by WP-Cache –> line).
    Does this mean that pages are not loaded from cache and always are generated dynamically or it is just because cached page is compressed and plugin cannot add that line to its end?
    Thanks and Excuse me if this is a support question!

  12. When a page is not loaded from cache (because it has been expired, or is a new page or …) a line is added to the source of page (after html close tag [/html]) like this:

    <!– Dynamic Page Served (once) in 0.493 seconds –>

    But when you reload that page in your browser these two lines are added (indicating that page is loaded from cache):

    <!– Dynamic Page Served (once) in 0.493 seconds –>
    <!– Cached page served by WP-Cache –>

    This occurs when WP-Cache is not modified and does not generate compressed pages.
    You can check this by viewing html source of any wordpress blog using WP-Cache without compression like WP-Cache creator’s blog at http://mnm.uib.es/gallir/ .

    I applied your modifications to my WP-Cache installation and did not see the second state whenever I try to reload the page.
    This is what I want to know. Why this does not happen?

  13. You should check in WP-Admin > WP-Cache if the cache pages are being generated or not.
    Easiest way is the clean the cache in WP-Admin and then start browsing your site and see if it says that pages are cached.

  14. Nice post but there is a better solution to serve gzip pages without sacrificing CPU power by caching the compressed page once it is generated.
    I have explained the required modifications here

    The only drawback is that it doesn’t support embedded dynamic PHP functions.

Comments are closed.

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for subscribing.

Something went wrong.