Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added nginx accel redirect support for cutting down on IO.

  • Loading branch information...
commit b2ee0a3520653a64eaf24c40bcbbb258c0678b96 1 parent 2b5e637
@tobi authored
View
11 config.ru
@@ -9,7 +9,16 @@ require 'image_server'
# Allows us to serve cache hits directly from file system
# by nginx (big speed boost). read:
# http://github.com/rack/rack-contrib/blob/5ea5e585a43669842314aa07f1e603be70d6e288/lib/rack/contrib/sendfile.rb
-use Rack::Sendfile
+
+
+if ENV['NGINX_ACCEL_REDIRECTS']
+ STDERR.puts 'Using accel redirect (Shopify config).'
+ require 'lib/middleware/accel_redirect'
+ use AccelRedirect
+else
+ use Rack::Sendfile
+end
+
use Rack::ShowExceptions
# 1. Forget about stupid favicons
View
3  config/env.rb
@@ -17,8 +17,9 @@
require 'memcached'
+ENV['CACHE_LOCATION'] = '/mnt/data/cache/rack/body'
ENV['META_STORE'] = 'memcache://127.0.0.1:11211/meta'
-ENV['ENTITY_STORE'] = 'file:/mnt/data/cache/rack/body'
+ENV['ENTITY_STORE'] = "file:#{ENV['CACHE_LOCATION']}"
# Logging
View
40 lib/middleware/accel_redirect.rb
@@ -0,0 +1,40 @@
+require 'rack/file'
+
+class File #:nodoc:
+ alias :to_path :path
+end
+
+# To make this work you have to add:
+#
+# location /cache/ {
+ # internal;
+ # alias /mnt/data/cache/rack/body;
+# }
+#
+# to nginx config
+
+
+class AccelRedirect
+ F = ::File
+
+ def initialize(app, variation=nil)
+ @app = app
+ end
+
+ def call(env)
+ status, headers, body = @app.call(env)
+ if body.respond_to?(:to_path)
+
+ path = body.to_path
+ url = path.sub(/^#{ENV['CACHE_LOCATION']}/i, '/cache')
+
+ Logger.current.info " => sending #{url} through nginx"
+
+ headers['Content-Length'] = '0'
+ headers['X-Accel-Redirect'] = url
+ body = []
+ end
+ [status, headers, body]
+ end
+
+end
View
2  lib/send_file.rb
@@ -14,7 +14,7 @@ module SendFile
}
def send_file(file)
- headers = {'Content-Length' => file.content.length}
+ headers = {'Content-Length' => file.content.length.to_s}
if file.respond_to?(:headers)
CopyHeaders.each do |key|
Please sign in to comment.
Something went wrong with that request. Please try again.