Apache mod_cache and mod_deflate

apache-2.2http-headersmod-cachemod-deflate

We have a Apache 2.2 with mod_cache and mod_deflate among other modules. The problem is that if we append Vary header as in Apache documentation…

# Make sure proxies don't deliver the wrong content
Header append Vary User-Agent env=!dont-vary

… we end up having copy of the served resource for each user agent variation in our cache. This wastes disk space and lowers the hit rate.

So, what is the preferred solution for this problem? Ditch the Vary header and compress only "safe" resources like plain html?

Best Answer

The reason for setting the header to vary by user agent is that Apache's recommended configuration for mod_deflate serves uncompressed versions of some content to Netscape 4 users. There are probably few enough Netscape 4 users now that you can just serve compressed content to all browsers that claim to support it, and not vary by user agent.

So instead of the recommended configuration:

<Location />
# Insert filter
SetOutputFilter DEFLATE

# 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
# 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
</Location>

You can just have:

<Location />
# Insert filter
SetOutputFilter DEFLATE

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