Hi,
I an application I am working on, each user has a bunch of text files
that they can retrieve using a AJAX request. These files don't change
after creation, but they are served from a Rails process as they can
only be viewed by a logged in user.
Some of the files are a reasonable size, so what I would like to do it
gzip them on disk, and then send them gzipped to the browser, avoiding
the need to gzip them on the fly.
Is it possible to do this with Rails?
I have done some googling and apache mod_deflate seems to be the
answer to compressing static pages on the fly, but with my case I
reckon I should be able to compress once and serve many times instead
(plus to Apache my 'static pages' look dynamic).
Also can mod_deflate compress the output from a Rails (Mongrel)
process or does it not touch dynamic content?
Thanks,
Stephen.
Apache’s mod_deflate can compress dynamic content from a mongrel without a problem (all our apps do it), you just add the following lines to the end of your virtual host configuration:
Deflate
AddOutputFilterByType DEFLATE text/html text/plain text/css text/javascript
… text/xml application/xml application/xhtml+xml text/javascript or whatever text-based mime type you want to send gzipped
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
The lines above make sure compressed pages are only sent to browsers that support it
The overhead of the deflating is negligible from what we’ve seen, so don’t bother too much trying to cache compressed versions (and how would you handle requests from browsers that don’t support compressed content?). Implementing caching in your Rails application on the other hand might be a very good idea, because it’s the processing of the request in mongrel that takes up most of the cycles when generating a page. If you’ve implemented caching in Rails, make sure you add the following line to your virtual host config before redirecting the request to your mongrel cluster (Apache will then serve the page without mongrel and it will deflate it before sending it to the user):
Rewrite to check for Rails cached page
RewriteRule ^([^.]+)$ $1.html [QSA]
Best regards
Peter De Berdt
Good point on the browsers that don't support gzip!
The files I am sending are cached (ie they are not generated from the
database), but I need to go through the rails process to ensure the
user is allowed to access the file they have asked for, so I cannot
use Rails caching as it stands. The AJAX request pretty much checks
the ownership of the file and pulls it off disk as it is so it should
be pretty efficient (I hope!).
Must have a go at setting up Apache, mod_deflate and mongrel tonight.
Thanks,
Stephen.