Rack::XStatic Middleware - 'X-Sendfile' for Rack
After I found the Rails X-Sendfile plugin, decided to code something similar for Rack (and Ramaze). The result: Rack::XStatic middleware. Example usage: xfile.ru rackup file. Start it with:
rackup -s thin -p 7000 xfile.ru
For using it with nginx 'X-Accel-Redirect':
# inside your .ru rackup file: use Rack::XStatic, :urls => ["/down", "/files"], :root => "public", :extra => { 'X-Sendfile-Type' => 'nginx', 'X-Accel-Limit-Rate' => '1024', 'X-Accel-Charset' => 'utf-8' } # inside your nginx.conf file: location ~ ^/(down|files)/ { internal; root /full/path/to/rack/public; }
'X-Sendfile-Type' can be also 'lighttpd', 'apache' or just 'yes' for adding only the 'X-Sendfile' header
Rack::XStatic also allow me to add custom headers to the response:
use Rack::XStatic, :urls => ["/css","/images"], :root => "public", :extra => { 'Cache-Control' => 'max-age=86400, public', 'Expires' => (Time.now + 86400).utc.rfc2822 }