Forum: Ruby on Rails gzip response to AJAX requests

C8840bff779d9acaa15d1628f7869d12?d=identicon&s=25 stephen O'D (Guest)
on 2007-08-16 15:55
(Received via mailing list)
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.
00973881979aa0a660ffbbb2f7a907fb?d=identicon&s=25 Peter De Berdt (Guest)
on 2007-08-16 17:10
(Received via mailing list)
On 16 Aug 2007, at 15:52, stephen O'D wrote:

> 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?

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
C8840bff779d9acaa15d1628f7869d12?d=identicon&s=25 stephen O'D (Guest)
on 2007-08-16 18:23
(Received via mailing list)
On Aug 16, 4:09 pm, Peter De Berdt <peter.de.be...@pandora.be> wrote:
> Apache's mod_deflate can compress dynamic content from a mongrel
>    BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
> 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.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.