# Sendfile and mongrel

**URL:** <https://www.ruby-forum.com/t/sendfile-and-mongrel/122863>\
**Category:** Mongrel\
**Created:** [December 12, 2007, 6:59pm UTC](https://www.ruby-forum.com/t/sendfile-and-mongrel/122863 "2007-12-12T18:59:30Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![ddancer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ecb155/32.png) [@ddancer](https://www.ruby-forum.com/u/ddancer)\
**Post date:** [December 12, 2007, 6:59pm UTC](https://www.ruby-forum.com/t/sendfile-and-mongrel/122863/1 "2007-12-12T18:59:30Z")

</div>

forgive me if this has been resolved already…

I have an app that is using sendfile to return large files to customers.  
We can’t use regular Apache to handle this static content since the  
files are what the customers are paying for - so it wouldn’t be cool for  
anyone else to get access to them.

The issue is that the mongrel mem footprint gets bloated when the files  
are transferred and this mem does not get returned to the system.

I’ve looked into x-sendfile, but it doesn’t appear to be compatible with  
Apache 2.2.

Now the question, does the current mongrel 1.1.1 have this same issue  
with sendfile and memory bloating? (I’m currently running 1.0.1)

Are there any other recommendations for handling this situation?

thanks

---

<div class="post-metadata">

**Author:** ![ddancer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ecb155/32.png) [@ddancer](https://www.ruby-forum.com/u/ddancer)\
**Post date:** [December 12, 2007, 7:49pm UTC](https://www.ruby-forum.com/t/sendfile-and-mongrel/122863/2 "2007-12-12T19:49:41Z")

</div>

I use X\_SendFile with Apache 2.2 just fine. FYI.

And wouldn’t it be possible to intercept all download requests and force  
use of a cookie/session authorization requirement? I do it on one of my  
websites. The X\_SendFile feature doesn’t actually send the server  
location of the file and if you used a url such as …

[http://website/download/[filename]](http://website/download/%5Bfilename%5D)

/download/ is an action in one of my controllers and it requires a valid  
login session. I do a query on the filename and if you’re not  
authorized for the file you don’t get to download. All downloads are  
stored outside the website directory so no direct linking is available.  
Then I use the x\_send\_file feature to have apache serve up the file  
without going through mongrel.

There’s also a nice x\_send\_file plugin that replaces “send\_file” with an  
“x\_send\_file” call, so code updates should be at a minimum.

matte

---

<div class="post-metadata">

**Author:** ![ddancer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ecb155/32.png) [@ddancer](https://www.ruby-forum.com/u/ddancer)\
**Post date:** [December 12, 2007, 8:04pm UTC](https://www.ruby-forum.com/t/sendfile-and-mongrel/122863/3 "2007-12-12T20:04:28Z")

</div>

Matte E. wrote:

> I use X\_SendFile with Apache 2.2 just fine. FYI.

I was hoping I was wrong about that.

Where do you get your x-sendfile source from?

I got it from “[tn123.ath.cx is offline](http://tn123.ath.cx/mod_xsendfile/)”

but I get all kinds of errors when trying to compile it.

---

<div class="post-metadata">

**Author:** ![ddancer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ecb155/32.png) [@ddancer](https://www.ruby-forum.com/u/ddancer)\
**Post date:** [December 12, 2007, 8:20pm UTC](https://www.ruby-forum.com/t/sendfile-and-mongrel/122863/4 "2007-12-12T20:20:59Z")

</div>

That’s also where I got mine. I’m running Apache 2.2.6 on FreeBSD 6.x  
and compiled it as instructed on that page.

apxs -cia mod\_xsendfile.c

My linux-fu is moderate at best so I was lucky I didn’t experience any  
errors compiling. Otherwise I’d be spending a day in Google finding  
answers. Good luck.

matte

---

<div class="post-metadata">

**Author:** ![ddancer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ecb155/32.png) [@ddancer](https://www.ruby-forum.com/u/ddancer)\
**Post date:** [December 12, 2007, 8:22pm UTC](https://www.ruby-forum.com/t/sendfile-and-mongrel/122863/5 "2007-12-12T20:22:45Z")

</div>

Before I discovered x-sendfile, I used to handle file uploads with  
PHP – talking to the same database as my main Rails app. PHP doesn’t  
bloat when dumping files across the wire like Ruby does.

Remember: you don’t need to use Rails / Mongrel for everything –  
sometimes switching to another platform to solve a particular problem  
can be the easiest, most robust solution.

But in this case: x-sendfile FTW. Deny from all in your files  
directory, and apache will send via x-sendfile but won’t allow  
clients to download them directly.

-n

---

<div class="post-metadata">

**Author:** ![ddancer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ecb155/32.png) [@ddancer](https://www.ruby-forum.com/u/ddancer)\
**Post date:** [December 12, 2007, 8:25pm UTC](https://www.ruby-forum.com/t/sendfile-and-mongrel/122863/6 "2007-12-12T20:25:06Z")

</div>

On Dec 12, 2007 8:27 AM, Nathan V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Before I discovered x-sendfile, I used to handle file uploads with  
> PHP – talking to the same database as my main Rails app. PHP doesn’t  
> bloat when dumping files across the wire like Ruby does.

_Ruby_ doesn’t.

Observe:  
swiftiply 1510 0.7 0.2 53300 21136 ? Ss Dec09 27:43  
ruby swiftiply -c /etc/swiftiply.cnf

Next I retrieve a 470mb file, twice. Then look at ps again

swiftiply 1510 0.7 0.2 53300 21136 ? Ss Dec09 27:56  
ruby swiftiply -c /etc/swiftiply.cnf

No change in the memory footprint, despite running almost a gigabyte  
of file data through it.

One should see essentially the same thing with Mongrel’s send\_file()  
method, because it reads the file in Const::CHUNK\_SIZE pieces (16k).  
However…if you are using send\_file() from within Rails, then Rails  
has to read the entire file into the response before it ever goes back  
to Mongrel, and THAT is what is causing the memory bloat. Not Ruby.  
Not Mongrel. Rails.

I agree with the other pieces of advice that have been given, though.  
Leverage Apache’s x-sendfile since you are on Apache. Alternately,  
write a dedicated mongrel handler that will deal with authentication &  
file delivery outside of Rails (which also means outside of the Rails  
mutex lock, which is a BIG win with regard to scalability of  
downloads, at least compared to doing it with send\_file from inside of  
Rails).

Kirk H.

---

<div class="post-metadata">

**Author:** ![ddancer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ecb155/32.png) [@ddancer](https://www.ruby-forum.com/u/ddancer)\
**Post date:** [December 12, 2007, 9:46pm UTC](https://www.ruby-forum.com/t/sendfile-and-mongrel/122863/7 "2007-12-12T21:46:49Z")

</div>

> One should see essentially the same thing with Mongrel’s send\_file()  
> method, because it reads the file in Const::CHUNK\_SIZE pieces (16k).  
> However…if you are using send\_file() from within Rails, then Rails  
> has to read the entire file into the response before it ever goes back  
> to Mongrel, and THAT is what is causing the memory bloat. Not Ruby.  
> Not Mongrel. Rails.

The rails mongrel handler actually. It buffers all the content from  
rails and sends it after the request is done. Rails supports  
streaming just fine with FCGI afaik. This is actually something I’d  
like to fix (and bring the rails handler into rails) actually, but I’m  
not totally clear why this was done. A thread for another time…

+1 for x-send-file or accel-redirect if you’re using nginx.

–  
Rick O.  
[http://lighthouseapp.com](http://lighthouseapp.com)  
[http://weblog.techno-weenie.net](http://weblog.techno-weenie.net)  
[http://mephistoblog.com](http://mephistoblog.com)

---

<div class="post-metadata">

**Author:** ![ddancer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ecb155/32.png) [@ddancer](https://www.ruby-forum.com/u/ddancer)\
**Post date:** [September 23, 2010, 3:24pm UTC](https://www.ruby-forum.com/t/sendfile-and-mongrel/122863/8 "2010-09-23T15:24:03Z")

</div>

Matte E. wrote:

> I use X\_SendFile with Apache 2.2 just fine. FYI.

Matt how do i force apche to handle downlaod request??

---

<div class="post-metadata">

**Author:** ![ddancer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ecb155/32.png) [@ddancer](https://www.ruby-forum.com/u/ddancer)\
**Post date:** [September 23, 2010, 4:49pm UTC](https://www.ruby-forum.com/t/sendfile-and-mongrel/122863/9 "2010-09-23T16:49:37Z")

</div>

On Thu, Sep 23, 2010 at 7:24 AM, Amit T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Matte E. wrote:
> 
> > I use X\_SendFile with Apache 2.2 just fine. FYI.
> 
> Matt how do i force apche to handle downlaod request??

Wow. This is an ancient thread that you just hijacked.

This is 100000% an Apache configuration issue. It doesn’t have  
anything to do with Mongrel, or with Ruby in the other thread on  
ruby-talk that you’ve been beating Luis up on.

Kirk H.

---

<div class="post-metadata">

**Author:** ![ddancer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ecb155/32.png) [@ddancer](https://www.ruby-forum.com/u/ddancer)\
**Post date:** [December 12, 2007, 9:21pm UTC](https://www.ruby-forum.com/t/sendfile-and-mongrel/122863/10 "2007-12-12T21:21:29Z")

</div>

ok, after reading the source of mod\_xsendfile.c the command should be

apxs2 -cia mod\_xsendfile.c

doing apxs tries to include files from Apache 1.3 - and then fails

also, this is on a Debian Etch

---

<div class="post-metadata">

**Author:** ![ddancer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ecb155/32.png) [@ddancer](https://www.ruby-forum.com/u/ddancer)\
**Post date:** [September 27, 2010, 10:14am UTC](https://www.ruby-forum.com/t/sendfile-and-mongrel/122863/11 "2010-09-27T10:14:34Z")

</div>

Kirk H. wrote:

> On Thu, Sep 23, 2010 at 7:24 AM, Amit T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > Matte E. wrote:
> > 
> > > I use X\_SendFile with Apache 2.2 just fine. ï¿½FYI.
> > 
> > Matt how do i force apche to handle downlaod request??
> 
> Wow. This is an ancient thread that you just hijacked.
> 
> This is 100000% an Apache configuration issue. It doesn’t have  
> anything to do with Mongrel, or with Ruby in the other thread on  
> ruby-talk that you’ve been beating Luis up on.
> 
> Kirk H.

Kirk do u have any idea ,how should resolved this problem  
how do configure apache to handle this download request???
