RSS

Recent Comments

Powered by Disqus
Mar
12th
Fri
permalink

X-sendfile + Rails + Passenger/Apache2

I installed this a while back but never really gave it much thought, until today. I wanted to be sure that it was working. I don’t think I’ll say much about why you should be using it, but I’ll say this unless you don’t have to serve any relatively large static files, you should really be using it, relatively easy to install and good returns.

Basically what it does it frees your rails processes which are memory hogs and delegates the static file serving to apache (or nginx) which you can have many more processes of because of the smaller memory footprint. This is very useful especially on small slice VPSes where you only have so much memory.

To convince myself that it was working I finally tested it by requesting a big file and and see how many requests I can get started until it locks up. With x-sendfile off, I could get 3 requests, then the server locked up, and it could not serve any more requests to the rails app. Imagine that, 3 people download a file (~20mins to complete) and your app is basically offline for that whole duration. With x-sendfile on, I got up to 8 simultaneous downloads and the rails app could still take requests. I could probably increase more but I felt convinced that it was working well enough.

Where is the X-sendfile header?

Why I investigated in the first place was because I didn’t really know how it worked so I was looking for the the “X-sendfile: path/to/file” header in the response. After much reading and testing, I still could not find any clear article/documentation on this. So what I believe is happening is that when you request a file from the rails app, it makes a request with the”X-sendfile: path/to/file” header to apache2 which then discards the response contents and basically streams the path/to/file to the requester.

So the requester will not see any “X-sendfile” header which is good cause it actually contains the path of the file on the server. All they get is the file.

For < Rails v2.3  you’ll need a plugin. Else its built right into send_file.

blog comments powered by Disqus