2

I have a rails web app sitting on an nginx web server. I have some audio files on my server, and I want people to be able to listen to them, and to be able to seek to any part of the audio file to listen from that point.

Simple right?

I was using direct links in the src of my html5 audio element. It worked great. The file could be played and seeking worked.

Enter security and auditability.

My audio files are sensitive. I only want certain people to be able to listen to them. I also need to know each time that they listen to them. Suddenly the public directory isn't going to work.

Enter rails's send_file.

Send_file initially appeared to be exactly what I needed. It allows rails to serve my audio files, and I could keep my files in a protected directory, I could check the current user's permissions, and I could create the appropriate audit trail. Except...

With send_file I can't seek. That is a deal breaker.

A few stackoverflow questions address getting send_file to handle http-range/byte-range requests. The ones I reviewed are:

what is the proper way to serve mp4 files through rails...

rails media file stream accept byte range request through send file ...

After doing more research, I found the following blog post: https://blog.echoplex.us/2014/08/19/so-you-want-to-stream-videoaudio-with-rails/

tl;dr don't use rails send_file to serve media. don't try to make it like the stackoverflow questions say you can. Instead, use nginx and X-Accel-Redirect, and end up with a request pipeline that looks like you->nginx->rails->nginx->you

I am considering taking his approach, but didn't know if there was a better way to do this.

What are my options?

(also, you can assume that I'm using the current versions of rails and nginx)

Community
  • 1
  • 1
Jared Menard
  • 2,654
  • 1
  • 21
  • 22

1 Answers1

2

DON'T USE sendfile please. Use X-Accel-Redirect or the advice below.

Nginx secure_link module helps you to serve files straight from disk with private links. No backend required. The full example is here.

Anatoly
  • 15,298
  • 5
  • 53
  • 77