发送重定向到特定端口

发布于 2024-09-05 14:26:16 字数 271 浏览 9 评论 0原文

我有一个 Rails 应用程序服务器正在侦听端口 9000,并通过 haproxy 进行调用。我来自该服务器的所有重定向都通过端口 9000 重定向回,而它们应该在端口 80 上发送回。

我使用 haproxy + nginx +乘客的组合。有没有办法确保所有重定向都通过端口 80 发送,无论实际服务器正在侦听哪个端口?

我不在乎它是否是 haproxy、nginx、Passenger 或 Rails 的更改。我只需要确保大多数请求(除非另有说明)都发送回端口 80。

谢谢!

I have an Rails application server that is listening on port 9000, and is being called through haproxy. All my redirects from that server are being redirected back through port 9000, when they should be sent back on port 80.

I am using a combination of haproxy + nginx + passenger. Is there a way to make sure all redirects are being sent through port 80, regardless of what port the actual server is listening on?

I don't care if its a haproxy, nginx, Passenger, or Rails change. I just need to make sure most requests unless specified otherwise, are sent back to port 80.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

甜味拾荒者 2024-09-12 14:26:16

正如 elektronaut 所指出的,这可能是应该在代理配置中处理的事情。也就是说,ActiveSupport::UrlFor#url_for 有一些可能有用的信息。查看 http://github。 com/rails/rails/blob/master/actionpack/lib/action_dispatch/routing/url_for.rb

我认为它归结为将两个参数传递到 url_for 和/或 link_to 调用中。首先是 :port =>; 123 参数,第二个是 :only_path => false 以便生成包含域、端口等的完整链接。

因此,在生成链接时,您可能会这样做:

link_to 'test', root_url(:port => 80, :only_path => false)

在创建自定义 url 时,您可能会这样做:

url_for :controller => 'test', :action => 'index', :port => 80, :only_path => false

对于重定向:

redirect_to root_url(:port => 80, :only_path => false)

我希望这会有所帮助,并且如果不是,您能否更具体地说明如何生成 URL、rails 为您生成什么以及您希望它生成什么。

更新:
我没有意识到这一点,但似乎您可以为使用 url_for 生成的 URL 设置默认值,生成链接和/或 URL 的其他所有内容都使用它。这里有一篇很好的文章: http://lucastej.blogspot.com/2008/01/ruby-on-rails-how-to-set-urlfor.html

或者为您总结一下:

将其添加到您的 application_controler.rb

def default_url_options(options)
   { :only_path => false, :port => 80 }
end

和这个:

helper_method :url_for

第一个块在控制器中设置默认值,第二个块使 url_for 帮助程序使用控制器中找到的默认值,因此默认值也适用于该控制器。

Like elektronaut indicated, this is probably something that should be handled in your proxy's configuration. That said, ActiveSupport::UrlFor#url_for has some information that might be useful. Take a look at http://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/routing/url_for.rb

What I think it boils down to is passing two arguments into your url_for and/or link_to calls. First is the :port => 123 argument, the second is :only_path => false so that it generates the full link including domain, port, etc.

So when generating a link, you might do:

link_to 'test', root_url(:port => 80, :only_path => false)

and when creating a custom url you might do:

url_for :controller => 'test', :action => 'index', :port => 80, :only_path => false

For a redirect:

redirect_to root_url(:port => 80, :only_path => false)

I hope this helps, and if it doesn't, can you be more specific about how you generating your URLs, what rails is generating for you, and what you would like it to generate.

Update:
I wasn't aware of this, but it seems you can set defaults for the URL's rails generates with url_for, which is used by everything else that generates links and/or URLs. There is a good write up about it here: http://lucastej.blogspot.com/2008/01/ruby-on-rails-how-to-set-urlfor.html

Or to sum it up for you:

Add this to your application_controler.rb

def default_url_options(options)
   { :only_path => false, :port => 80 }
end

and this:

helper_method :url_for

The first block sets defaults in the controllers, the second causes the url_for helper to use the one found in the controllers, so the defaults apply to that as well.

缱倦旧时光 2024-09-12 14:26:16

重写重定向可能应该是 Web 服务器的责任,但您可以修改请求对象以始终在 before_filter 中返回端口 80:

class ApplicationController < ActionController::Base
    before_filter :use_port_80 if RAILS_ENV == production
    def use_port_80
        class << request
            def port; 80; end
        end
    end
end

Rewriting the redirect should probably be the web server's responsibility, but you can hack the request object to always return port 80 in a before_filter:

class ApplicationController < ActionController::Base
    before_filter :use_port_80 if RAILS_ENV == production
    def use_port_80
        class << request
            def port; 80; end
        end
    end
end
等风也等你 2024-09-12 14:26:16

我建议通过将此代码添加到配置中来修复 haproxy 中的问题。

rsprep (.*):9000(.*) \1\2

I propose it should be fixed in haproxy by adding this code to the config.

rsprep (.*):9000(.*) \1\2
递刀给你 2024-09-12 14:26:16

如果这是链接到与其所在网站相同的服务器的链接。您可以使用相对链接而不是绝对链接。如果您使用辅助方法创建链接,则可以使用 _path 后缀而不是 _url

如果您的routes.rb看起来像这样:

ActionController::Routing::Routes.draw do |map|
  map.resources :users
end

或者在rails 3中:

YourAppName::Application.routes.draw do
  resources :users
end

您可以使用以下辅助方法来创建相对链接:

users_path     #=> /users
user_path      #=> /users/:id
edit_user_path #=> /users/:id/edit
new_user_path  #=> /users/new

# instead of

users_url      #=> http(s)://example.com:9000/users
user_url       #=> http(s)://example.com:9000/users/:id
edit_user_url  #=> http(s)://example.com:9000/users/:id/edit
new_user_url   #=> http(s)://example.com:9000/users/new

如您所见,这些链接独立于您的端口或主机重新运行。

If this are links that are linked to the same server as site the site they are on. You can use relative links instead of absolute ones. If you're using helper methods to create the links you can use the _path suffix instead of _url.

If your routes.rb looks something like this:

ActionController::Routing::Routes.draw do |map|
  map.resources :users
end

or in rails 3:

YourAppName::Application.routes.draw do
  resources :users
end

you can use the following helper methods to create relative links:

users_path     #=> /users
user_path      #=> /users/:id
edit_user_path #=> /users/:id/edit
new_user_path  #=> /users/new

# instead of

users_url      #=> http(s)://example.com:9000/users
user_url       #=> http(s)://example.com:9000/users/:id
edit_user_url  #=> http(s)://example.com:9000/users/:id/edit
new_user_url   #=> http(s)://example.com:9000/users/new

As you can see, these links are independent of the port or host you're running on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文