Rails:适当的设置与DNS,机架重写等为Heroku [英] Rails: Proper setup with DNS, Rack-rewrite etc for Heroku

查看:157
本文介绍了Rails:适当的设置与DNS,机架重写等为Heroku的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经浏览了整个互联网,但无法得到一个最终的解决方案,我希望是100%正确的DNS等设置,以获得以下结果在Heroku.com:





我可以从任何地方得到一点帮助,但没有什么结论把整个问题从DNS处理到机架重写,这就是为什么我在这里问问(我的注册商和Heroku的支持只能给我他们的一部分)。



需要做的是,据我所知:


  1. 添加mydomain.com和www.mydomain。 com到Heroku的自定义域名


  2. 将CNAME更改为 http://myapp.herokuapp.com 在我的注册服务商DNS。 问题:将使用CNAME重定向到myapp.herokuapp.com的www和*?


  3. 添加@DNS记录以处理裸域请求(http://mydomain.com)。由于CNAME不会执行此操作,因此我使用的IP号码位于 https://devcenter.heroku .com / articles / custom-domains ,尽管根据文章有严重的正常运行时间问题。


- 1-3给我留下一个网站,将同时为 http://mydomain.com http://www.mydomain.com ,内容为 http://myapp.herokuapp.com



c。现在,我想将所有内容从 http://mydomain.com 重定向到 http://www.mydomain.com ,根据Heroku的支持,应该使用机架重写(https://github.com/jtrupiano/rack -改写)。使用示例,代码应该是:

  r301%r {。*},'http:// mydomain .com $&',:if => Proc.new {| rack_env | 
rack_env ['SERVER_NAME']!='www.mydomain.com'
}

然而,这为我创造了一个永恒的循环。



所以,我的问题现在是: Heroku和DNS设置为了实现这个设置?



很难解决这个问题,因为尝试尝试使用不同的DNS设置是非常困难的(很难知道他们是否经历过)。

解决方案

这是重写规则我认为这是说做一个301重定向到' a href =http://mydomain.com =nofollow noreferrer> http://mydomain.com $&'(其中$&表示它将保留.com后的所有内容),但只有当请求中的主机不是www.mydomain.com。



所以你会进入重定向循环,因为对你的服务器的任何请求那里没有www,例如裸体mydomain.com将重定向到mydomain.com。



它可能需要重写为:

  r301%r {。*},'http://www.mydomain.com$&',if => Proc.new {| rack_env | 
rack_env ['SERVER_NAME'] =='mydomain.com'}

所以你是重定向到您的域的www版本。



其实有一个 dupe here



编辑:
这是我会做的:


  1. 删除您的重定向规则

  2. 设置您的dns,以便www.domain.com和domain.com返回myapp.herokuapp .com

  3. 验证dns是否正常工作,而不用担心重定向

  4. 确保在DNS级别不要尝试一些有用的重定向

在这个阶段,与DNS无关。他们唯一的办法是返回IP地址(A记录)或其他域(CNAME)。然后他们的工作就完成了。



下一页:


  1. 应用机架重写规则

  2. 服务器可能需要重新启动 - 以防止

  3. 使用类似 Sam Spade - 标题将包含301重定向和新位置,如果它有正常工作


I have looked all over the internet but could not get one final solution to what I would expect to be the 100% proper setup of DNS etc to get the following result on Heroku.com:

I can get a bit of help from everywhere but nothing conclusive that will take care of the whole problem from DNS to rack-rewrite so that is why I am asking here (the support at my registrar and Heroku can only give me "their part").

What needs to be done is, as far as I understand:

  1. Add mydomain.com and www.mydomain.com to Custom Domains at Heroku

  2. Change CNAME to http://myapp.herokuapp.com in my registrars DNS. Question: Would it be both "www" and "*" that would use the CNAME redirection to myapp.herokuapp.com?

  3. Add a "@" DNS record in order to handle naked domain requests (http://mydomain.com). Since this will not be done with CNAME, I am using the IP numbers at https://devcenter.heroku.com/articles/custom-domains, although they have severe uptime problems according to the article.

-- 1-3 leaves me with a website that will serve both http://mydomain.com and http://www.mydomain.com with the content at http://myapp.herokuapp.com

c. Now, I want to redirect everything from http://mydomain.com to http://www.mydomain.com and, according to Heroku support, this should be done with rack-rewrite (https://github.com/jtrupiano/rack-rewrite). Using the example in there, the code should then be:

r301 %r{.*}, 'http://mydomain.com$&', :if => Proc.new {|rack_env|
  rack_env['SERVER_NAME'] != 'www.mydomain.com'
}

This, however, creates an eternal loop for me.

So, my question is now: What would be the proper workflow for Heroku and DNS-settings in order to achieve this setup?

It is very difficult to troubleshoot this, since trying out different DNS-settings by trial and error is very difficult (it's hard to know if they have gone through).

解决方案

It's the rewrite rule I think that it is saying "do a 301 redirect to 'http://mydomain.com$&' (where the $& indicates that it will keep everything after the .com) but only if the host in the request is not 'www.mydomain.com'".

So you'll go into a redirect loop because any request to your server that doesn't have the www in it, e.g. the naked mydomain.com, will redirect back to mydomain.com.

It probably needs rewriting to something like:

r301 %r{.*}, 'http://www.mydomain.com$&', :if => Proc.new {|rack_env|
rack_env['SERVER_NAME'] == 'mydomain.com'}

So you are redirecting to the www version of your domain.

Actually there is a dupe here

Edit: This is what I would do:

  1. remove your redirect rules
  2. set up your dns so that both www.domain.com and domain.com returns myapp.herokuapp.com
  3. verify that the dns works without worrying about redirection
  4. make sure that at the DNS level it isn't trying to do some helpful redirecting

At this stage there isn't anything else to do with the DNS. The only thing they do is return an IP address (A record) or another domain (CNAME). Then their job is done.

Next:

  1. Apply the rack-rewrite rule
  2. The server might need a restart - do it just in case
  3. Test using something like Sam Spade - the headers will contain a 301 redirect and the new location if it has worked properly

这篇关于Rails:适当的设置与DNS,机架重写等为Heroku的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆