Search code examples
ruby-on-rails-3.1proxyrackreverse-proxyroutes

How do I use Rack::Proxy within Rails to proxy requests to a specific path to another app


I found this great blog post on how to use Rack::Proxy as a separate proxy app. The article explains how he uses Rack::Proxy to proxy requests to http://localhost:3000 to an app on port 3001 and requests to http://localhost:3000/api to an app on port 3002. I want to do the same thing, but I do not want to create a separate proxy app. Instead, I want my main Rails app to proxy requests to /blog to a different app.

Blog Post: http://livsey.org/blog/2012/02/23/using-rack-proxy-to-serve-multiple-rails-apps-from-the-same-domain-and-port/


Solution

  • Figured it out.

    lib/proxy.rb

    require 'rack-proxy'
    class Proxy < Rack::Proxy
        def initialize(app)
            @app = app
        end
    
        def rewrite_env(env)
            # do magic in here
        end
    end
    

    config/application.rb

    config.middleware.use "Proxy"