Rails: Proper setup with DNS, Rack-rewrite etc for Heroku

折月煮酒 提交于 2019-12-22 00:11:05

问题


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:

  • Using a mydomain.com to show the content at myapp.herokuapp.com
  • Redirecting all traffic from http://mydomain.com to http://www.mydomain.com (including subfolders)

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).


回答1:


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


来源:https://stackoverflow.com/questions/10395184/rails-proper-setup-with-dns-rack-rewrite-etc-for-heroku

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!