Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a server serving up a JS file a few million times a day using apache2. Some of my users would like the JS to be gzipped. Does anyone know how apache2 mod_deflate handles compression of static files? Will it compress the js for each request(in which case I'd be worried about cpu load)? If it does, is there a way to pre-compress the JS files so apache2 wouldn't have to do this for each file?

share|improve this question
See this post on Stack Overflow:… – Phil Ross Apr 11 '10 at 22:20
sweet1! thanks! – rizen Apr 11 '10 at 22:32
mod_deflate will keep compressed files in memory the best it can. Obviously memory constraints play into which files will be cached and for how long. If you want to ensure certain files are cached all the time see the Multiviews instructions below. – Chris S Jan 23 '12 at 21:44

You can do it with

Options +Multiviews

then precompress the file as whatever.js.gz using

gzip -c -9 whatever.js > whatever.js.gz

However, if there are older browsers that may break with this.

In the apache config

AddEncoding x-gzip .gz


AddType application/x-gzip .gz
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.