Skip to content

Instantly share code, notes, and snippets.

@dannvix
Created February 19, 2013 01:28
Show Gist options
  • Save dannvix/4982325 to your computer and use it in GitHub Desktop.
Save dannvix/4982325 to your computer and use it in GitHub Desktop.
nginx_sendfile() for streaming mp4 to Safari from Nginx + Passenger + Rails
# customized sendfile() implementation utilizing Nginx's X-Accel-* header options
# needed ecause Rails' built-in sendfile() generates chunked response without Content-Length,
# and Safari rufuses to player videos coming in chunks......
def nginx_sendfile (file_path, options={})
response.headers["Cache-Control"] = options[:cache_control] || "max-age=86400, public"
response.headers["Content-Disposition"] = options[:disposition] || "inline"
response.headers["X-Accel-Limit-Rate"] = options[:limit_rate] || "307200" # byte
response.headers["X-Accel-Buffering"] = options[:buffering] || "yes"
response.headers["X-Accel-Redirect"] = file_path
render :nothing => true
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment