发送重定向到特定端口
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正如 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
以便生成包含域、端口等的完整链接。因此,在生成链接时,您可能会这样做:
在创建自定义 url 时,您可能会这样做:
对于重定向:
我希望这会有所帮助,并且如果不是,您能否更具体地说明如何生成 URL、rails 为您生成什么以及您希望它生成什么。
更新:
我没有意识到这一点,但似乎您可以为使用 url_for 生成的 URL 设置默认值,生成链接和/或 URL 的其他所有内容都使用它。这里有一篇很好的文章: http://lucastej.blogspot.com/2008/01/ruby-on-rails-how-to-set-urlfor.html
或者为您总结一下:
将其添加到您的
application_controler.rb
和这个:
第一个块在控制器中设置默认值,第二个块使 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:
and when creating a custom url you might do:
For a redirect:
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
and this:
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.
重写重定向可能应该是 Web 服务器的责任,但您可以修改请求对象以始终在 before_filter 中返回端口 80:
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:
我建议通过将此代码添加到配置中来修复 haproxy 中的问题。
I propose it should be fixed in haproxy by adding this code to the config.
如果这是链接到与其所在网站相同的服务器的链接。您可以使用相对链接而不是绝对链接。如果您使用辅助方法创建链接,则可以使用
_path
后缀而不是_url
。如果您的
routes.rb
看起来像这样:或者在rails 3中:
您可以使用以下辅助方法来创建相对链接:
如您所见,这些链接独立于您的端口或主机重新运行。
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:or in rails 3:
you can use the following helper methods to create relative links:
As you can see, these links are independent of the port or host you're running on.