0

I have a Sinatra app in a Rails app that serves a static asset from a directory. The app is mounted in the Rails routes like this:

mount BeanstalkdView::Server, :at => "/beanstalk"

When I run this locally it works fine, using Thin, but when I run it on my test server (Nginx/Passenger) the static assets behave strange. A request to a static file return 200 OK, but there is no content.

I tell Sinatra where my static files are via set :public_folder, "#{root}/resources" and in the template I load the static files, e.g. a CSS file with #{request.env['SCRIPT_NAME']}/css/file.css. I verified that both paths are correct.

4

2 回答 2

3

That happens due to ::Rack::Sendfile middleware which is enabled by default in Rails 3.0.x in production (disabled by default in any environment since 3.1.x).

This middleware is really simple but yet powerful. When you pass ::Rack::File or ::Sinatra::StaticFile (or any other object) which respond to :path, this middlware adds X-SendFile (for Apache) or X-SendFile-Redirect (for NGinx) and does not sends actual body. So that Apache or NGinx will handle real file delivery. It's a good and most efficient way to serve static assets in production, however you may disable this middleware (if you don't want to mess with your Nginx/Apache config). Find and comment following config option in your config/environments/production.rb file:

config.action_dispatch.x_sendfile_header

this config option is used to instruct Sendfile middleware which header to set (do not do anything if not specified).

于 2013-04-30T15:04:12.003 回答
2

Aleksey V's answer helped me a lot. In the end I fixed this using the proper setting for Nginx in production.rb:

config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect'

Make sure you restart your Rails app, Nginx and do a hard refresh in your browser to get the files.

For more info check out: http://jimneath.org/2011/04/05/rails-3-nginx-and-send_file.html.

于 2013-05-01T11:59:29.663 回答