# Gzip response to AJAX requests

**URL:** <https://www.ruby-forum.com/t/gzip-response-to-ajax-requests/112222>\
**Category:** Rails\
**Created:** [August 16, 2007, 3:55pm UTC](https://www.ruby-forum.com/t/gzip-response-to-ajax-requests/112222 "2007-08-16T15:55:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![stephen\_OSD](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/df788c/32.png) [@stephen\_OSD](https://www.ruby-forum.com/u/stephen_OSD)\
**Post date:** [August 16, 2007, 3:55pm UTC](https://www.ruby-forum.com/t/gzip-response-to-ajax-requests/112222/1 "2007-08-16T15:55:12Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![stephen\_OSD](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/df788c/32.png) [@stephen\_OSD](https://www.ruby-forum.com/u/stephen_OSD)\
**Post date:** [August 16, 2007, 5:10pm UTC](https://www.ruby-forum.com/t/gzip-response-to-ajax-requests/112222/2 "2007-08-16T17:10:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![stephen\_OSD](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/df788c/32.png) [@stephen\_OSD](https://www.ruby-forum.com/u/stephen_OSD)\
**Post date:** [August 16, 2007, 6:23pm UTC](https://www.ruby-forum.com/t/gzip-response-to-ajax-requests/112222/3 "2007-08-16T18:23:38Z")

</div>

On Aug 16, 4:09 pm, Peter De Berdt [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.
