Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails3 application behind a Proxy

I'm trying to setup a Rails 3 application behind an Apache2 webserver which is acting as a proxy. The Apache webserver is running on port 8080, and if I make a call to the http://ip:8080, I see the request in the mongrel window, so the proxy properly relays the incomming requests to the mongrel servers.

However, my index page performs a redirect to the login if no username exists. So i make the following call : http://:8080/app , but the redirect goes to http:///session/new instead of http:///app/sessio/new i'm not really sure that the apache is badly configured, i'm more doubting about rails 3.

Below is the config of my apache for this proxy stuff, my routes.rb file and some code I found for a potential rverse proxy fix, but it doesn't seem to work.

REVERSE_PROXY_FIX

BASE_URL = ''
module ActionController
  ActionController::Base.asset_host= BASE_URL

  class UrlRewriter
#    alias old_rewrite_url rewrite_url

    # Prepends the BASE_URL to all of the URL requests created by the
    # URL rewriter in Rails.
    def rewrite_url(path, options)
      url = old_rewrite_url(path, options)
      url = url.gsub(@request.protocol + @request.host_with_port, '')
      url = BASE_URL + url
      url
    end
  end
end 

Apache2 config

# This file contains the proxy settings for apache to map all the incomming requests for a specific application to the relevant mongrel
# web server that is able to handle the incoming requests.
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
ProxyRequests Off

<Proxy *>
    Order deny,allow
    Allow from all
</Proxy>


# The section below tells Apache where to find all the static files for our applications.
# Because these settings are application specific, we need to create entries for each application
# that we wish to run under the Apache & Mongrel configuration
Alias /Esco "c:/web/esco/public"
<Directory "C:/web/esco/public">
    Options Indexes FollowSymLinks
    AllowOverride none
    Order allow,deny
    Allow from all
</Directory>

ProxyPass /Esco/images !
ProxyPass /Esco/stylesheets !
ProxyPass /Esco/javascripts !
ProxyPass /Esco/ http://127.0.0.1:4000/
ProxyPass /Esco http://127.0.0.1:4000/
ProxyPassReverse /Esco/ http://127.0.0.1:4000/

routes.rb

ESCO::Application.routes.draw do
  root :to => 'static#index'
  resources :cvs
  match 'cvs/match' => 'cvs#match', :as => :match_resume, :via => :post

    # Session Routes
  # These routes are for logging in and out from the application.
  match 'session/new' => 'session#new', :as => :new_session, :via => :get
  match 'session/create' => 'session#create', :as => :create_session, :via => :put
  match 'session/destroy' => 'session#destroy', :as => :destroy_session, :via => :delete

  # Admin panel Routers
  # These routes are for managing all the entities currently residing in the application
  match 'admin' => 'admin/static#index', :as => :admin_index, :via => :get
  match 'admin/cvs/save' => 'admin/cvs#save', :as => :admin_save_cv, :via => :post
  namespace "admin" do
    resources :users, :cvs, :settings, :languages, :vacancies, :countries, :languages
  end
end

I also like to mnetion that the apache is running on a Windows Server 2008R2 x64 system, and that the rails applications are running inside a Mongrel server on the same system ranging from port 4000 -> 4010. I hope someone can help me sort this reverse proxy thing.

EDIT: I've updated the config.ru file to run the application from the same subfolder domain as the proxy would do, and that allows me to see the views etc, but still missing the stylesheets and images.

Mongrel receives the following:

Started GET "/Esco/" for 81.82.197.2 at 2011-05-09 13:25:44 +0200
  Processing by StaticController#index as HTML
Rendered static/index.html.haml within layouts/application (15.6ms)
Completed 200 OK in 31ms (Views: 31.2ms | ActiveRecord: 0.0ms)

And if I browse to the stylsheets directly, I can see them.

like image 248
codingbunny Avatar asked May 09 '11 10:05

codingbunny


1 Answers

Here is how I set up a RoR 3 app behind Apache 2 proxy using a sub URI.

First, I set up the app to run in a sub URI using webrick resulting in the following URL:

http://localhost:3000/myapp  

In config/environment.rb I added the following line:

ENV['RAILS_RELATIVE_URL_ROOT'] = "/myapp"

Next in config.ru I changed the following line:

run Myapp::Application

to:

map '/myapp' do
  run Myapp::Application
end

Started webrick and pointed my browser to the following URL to make sure it works:

http://localhost:3000/myapp

Configured Apache next. Enabled the proxy and proxy_http modules. This is what my proxy.conf looks like:

ProxyRequests On
<Proxy *>
    AddDefaultCharset off        Order deny,allow
    Allow from all
    #Allow from .example.com
</Proxy>

ProxyPass /myapp http://localhost:3000/myapp
ProxyPassReverse /myapp http://localhost:3000/myapp

Restarted Apache and my app was available at:

http://www.example.com/myapp

All links and redirects work.

like image 126
Nick Nikolov Avatar answered Oct 17 '22 09:10

Nick Nikolov