Apache: Browser Caching and GZIP Compression

My coworker and I had been putting off looking into setting the cache expiry headers and enabling GZIP compression on our site. It had been on my to do list for a while after getting recommendations from the Google Page Speed and Yahoo YSlow Firefox add-ons for Firebug. Great add-ons for developing. Pick them up if you don’t have them already. Now, back to caching and compression.

First take a look at Google and Yahoo to give a brief explanation about what Caching and Compression is and how it helps your website.

Google Caching Explanation
Google Compression Explanation

Yahoo Caching Explanation
Yahoo Compression Explanation

The first thing to do is make sure Apache has the proper modules enabled. We will need to SSH into the web server and open the Apache config file. I am assuming you know your way around SSH command line. If not, leave a comment and I will give more details.

The config file is normally contained somewhere around /etc/httpd/conf/httpd.conf. This is based on Apache 2.x. Open the config file and look for the following:

LoadModule expires_module modules/mod_expires.so
LoadModule deflate_module modules/mod_deflate.so

These commands load the modules we need for setting the cache and gzip compression. If you don’t see these contact your host provider about getting them enabled.

Now we can either add the following in our httpd.conf file or if you have multiple sites on your web server and only want this for certain sites, we can put it an .htaccess file in the root dir of the site. If you put this in the conf file instead of .htaccess you will need to restart Apache. For restarting Apache, see the manual.

[pre lang=”apache”]
# ————— Browser Caching —————————–

#Turn on caching expirations
ExpiresActive On

# Set Cache Expiry to 3 days from access
ExpiresDefault “access plus 3 days”

# ————— GZIP Compression —————————

# Turn on compression

# Insert filter
SetOutputFilter DEFLATE

# Don’t compress images
SetEnvIfNoCase Request_URI \
\.(?:gif|jpe?g|png)$ no-gzip dont-vary

# Make sure proxies don’t deliver the wrong content
Header append Vary User-Agent env=!dont-vary
[/pre]

All of this is modified directly from the examples in the Apache manual.

For caching we first turn on the mod_expires module. Now set all our resources to cache for 3 days from the time it is accessed. If you content is more static you may want to set it for 1 month or 1 year. It can also be set up to set different caching time periods for different types of files. See the manual for more details.

For compression we also turn on the module first (mod_deflate). It will now send compressed content to visitors who have browsers that can support compressed content (all current browsers should support this). Next we add a rule to not compress images as these are already compressed and compressing them further would reduce quality as well as waste resources on the web server. The last rule is for proxy servers which I’m not totally sure what exactly it does. For additional details, see the manual.

Now either analyze your site again in YSlow or Page Speed in Firefox to see what it says about compression and caching. You can also use this site to test compression. Make sure you clear your browser cache first.

Additional Resources:

Apache mod_deflate module (GZIP compression)
Apache mod_expires module (Caching)
Better Explained – GZIP Compression Article

[carousel keywords=”apache” category=”Books” tag=”damnsemi01-20″ showBorder=”True”]

Continue Reading

  • No Related Posts
  • Fantastic web site. Plenty of useful information here. I’m sending it to some friends ans also sharing in delicious. And naturally, thanks for your effort!