windows 代理背后的 Rails3 应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5935497/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails3 application behind a Proxy
提问by codingbunny
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.
我正在尝试在充当代理的 Apache2 网络服务器后面设置一个 Rails 3 应用程序。Apache 网络服务器在端口 8080 上运行,如果我调用http://ip:8080,我会在 mongrel 窗口中看到请求,因此代理正确地将传入的请求中继到 mongrel 服务器。
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.
但是,如果不存在用户名,我的索引页面会重定向到登录名。所以我进行了以下调用:http://:8080/app ,但重定向转到 http:///session/new 而不是 http:///app/sessio/new 我不太确定 apache配置不当,我更怀疑 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.
下面是我的 apache 为这个代理配置的配置、我的 routes.rb 文件和我为潜在的反向代理修复找到的一些代码,但它似乎不起作用。
REVERSE_PROXY_FIX
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
Apache2 配置
# 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.
我还想说 apache 在 Windows Server 2008R2 x64 系统上运行,而 rails 应用程序在同一系统上的 Mongrel 服务器内运行,范围从端口 4000 -> 4010。我希望有人能帮我解决这个问题代理的东西。
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.
编辑:我已经更新了 config.ru 文件以从与代理相同的子文件夹域运行应用程序,这使我可以查看视图等,但仍然缺少样式表和图像。
Mongrel receives the following:
Mongrel 收到以下信息:
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.
如果我直接浏览样式表,我可以看到它们。
采纳答案by codingbunny
I finally solved the problem by switching to an Nginx frontend instead of apache. Alot easier to configure and works like a charm.
我终于通过切换到 Nginx 前端而不是 apache 解决了这个问题。更容易配置和工作就像一个魅力。
I've done some research on apache proxy on windows and found several pages with the similar problem beeing described and bugs mentioned about a certain .so file. I finally gave up on this and used nginx
我已经对 Windows 上的 apache 代理进行了一些研究,发现有几个页面描述了类似的问题,并提到了关于某个 .so 文件的错误。我终于放弃了这个并使用了nginx
回答by Nick Nikolov
Here is how I set up a RoR 3 app behind Apache 2 proxy using a sub URI.
下面是我如何使用子 URI 在 Apache 2 代理后面设置 RoR 3 应用程序。
First, I set up the app to run in a sub URI using webrick resulting in the following URL:
首先,我使用 webrick 将应用程序设置为在子 URI 中运行,从而得到以下 URL:
http://localhost:3000/myapp
In config/environment.rb I added the following line:
在 config/environment.rb 我添加了以下行:
ENV['RAILS_RELATIVE_URL_ROOT'] = "/myapp"
Next in config.ru I changed the following line:
接下来在 config.ru 中,我更改了以下行:
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:
启动 webrick 并将我的浏览器指向以下 URL 以确保它正常工作:
http://localhost:3000/myapp
Configured Apache next. Enabled the proxy and proxy_http modules. This is what my proxy.conf looks like:
接下来配置Apache。启用了 proxy 和 proxy_http 模块。这是我的 proxy.conf 的样子:
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:
重新启动 Apache,我的应用程序可在以下位置使用:
http://www.example.com/myapp
All links and redirects work.
所有链接和重定向都有效。
回答by David Barlow
I think Your problem is that rails and rack by default assumes that the root url is / not /app
我认为你的问题是 rails 和 rack 默认情况下假设根 url 是 / 不是 /app
You can fix this by tweaking your Rails Rackconfig.
您可以通过调整 Rails Rack配置来解决此问题。
See this answer on how to do this: Changing the base URL for Rails 3 development
请参阅有关如何执行此操作的答案:更改 Rails 3 开发的基本 URL
also add this to the top of your environment.rb file
还将它添加到您的 environment.rb 文件的顶部
ENV['RAILS_RELATIVE_URL_ROOT'] = "/app"
Hope this helps.
希望这可以帮助。