# Serving images with send\_file performance issue

**URL:** <https://www.ruby-forum.com/t/serving-images-with-send-file-performance-issue/88656>\
**Category:** Rails\
**Created:** [March 13, 2007, 11:39am UTC](https://www.ruby-forum.com/t/serving-images-with-send-file-performance-issue/88656 "2007-03-13T11:39:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![robbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/2acd7d/32.png) [@robbie](https://www.ruby-forum.com/u/robbie)\
**Post date:** [March 13, 2007, 11:39am UTC](https://www.ruby-forum.com/t/serving-images-with-send-file-performance-issue/88656/1 "2007-03-13T11:39:28Z")

</div>

I’ve got an app serving images on the filesystem from outside the  
/public directory so I can authenticate users first.

I’m serving the images and thumbnails up using send\_file, (1 large 60k  
image and 9 6k thumbnails) in one view, and it seems a bit sluggish. Is  
this just because of my crappy old windows box running in development  
mode - or is each request to serve up an image from an action in the  
controller slowing the app down?

---

<div class="post-metadata">

**Author:** ![robbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/2acd7d/32.png) [@robbie](https://www.ruby-forum.com/u/robbie)\
**Post date:** [March 14, 2007, 5:50am UTC](https://www.ruby-forum.com/t/serving-images-with-send-file-performance-issue/88656/2 "2007-03-14T05:50:35Z")

</div>

Action caching will help improve matters, but if you need to  
authenticate for every image, you are already in a bad place for  
performance.

Lighttpd and I’m sure other servers have a way of serving ‘secure’  
downloads. This works by putting a token into the URL for each image  
when you generate the HTML. This token will expire shortly, so only  
that user will be able to read those images. The images are then  
served directly by the web server (but only if the token is valid).  
I’ve seen this technique used for downloading purchased MP3 files, and  
it worked as advertised.

More info (including a Rails example) at the mod\_secdownload page:  
[http://trac.lighttpd.net/trac/wiki/Docs%3AModSecDownload](http://trac.lighttpd.net/trac/wiki/Docs%3AModSecDownload)

On Mar 13, 3:39 am, Robbie S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![robbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/2acd7d/32.png) [@robbie](https://www.ruby-forum.com/u/robbie)\
**Post date:** [March 14, 2007, 6:18am UTC](https://www.ruby-forum.com/t/serving-images-with-send-file-performance-issue/88656/3 "2007-03-14T06:18:21Z")

</div>

thanks Tom - after some more research it appears that Mongrel (which I’m  
using) seems to be playing a large part in the problem. I read about  
using lighttpd and the X-send-file header, which might work better.

---

<div class="post-metadata">

**Author:** ![robbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/2acd7d/32.png) [@robbie](https://www.ruby-forum.com/u/robbie)\
**Post date:** [March 14, 2007, 6:28am UTC](https://www.ruby-forum.com/t/serving-images-with-send-file-performance-issue/88656/4 "2007-03-14T06:28:37Z")

</div>

I have a plugin that extends Rails Action Caching to use the X-  
Sendfile header on lighttpd or the equivalent on nginx to send the  
cached file, so your Rails code only needs to care about action  
caching, and all the magic will be enabled by the server  
configuration.

More info here: [http://agilewebdevelopment.com/plugins/action\_cache](http://agilewebdevelopment.com/plugins/action_cache)

On Mar 13, 10:18 pm, Robbie S. \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![robbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/2acd7d/32.png) [@robbie](https://www.ruby-forum.com/u/robbie)\
**Post date:** [March 14, 2007, 8:25am UTC](https://www.ruby-forum.com/t/serving-images-with-send-file-performance-issue/88656/5 "2007-03-14T08:25:45Z")

</div>

There’s a gem that is supposed to take care of this on Mongrel,  
although I haven’t tried it. We’re about to tackle the same issue so  
if you use this and it works for you would you mind dropping us a  
line?

[http://rubyforge.org/projects/msend-file/](http://rubyforge.org/projects/msend-file/)

Dale

On Mar 13, 9:18 pm, Robbie S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
