Note that there are some explanatory texts on larger screens.

plurals
  1. POCorrect DNS setup for custom domain with subdomains and CNAMES pointing at them
    text
    copied!<p>I have a multi-tenant Rails app, hosted with Heroku at <a href="http://myapp.herokuapp.com" rel="nofollow">http://myapp.herokuapp.com</a>. The tenants/accounts are separated via subdomains (and Postgresql schemas). I have then added my own domain as custom domain to the Heroku app. Since Heroku really advice against using A records because of "uptime implications" (<a href="https://devcenter.heroku.com/articles/avoiding-naked-domains-dns-arecords" rel="nofollow">https://devcenter.heroku.com/articles/avoiding-naked-domains-dns-arecords</a>) I first tried to put a CNAME record from my custom domain to myapp.herokuapp.com. That worked fine also with subdomains like <a href="http://clientaccount.mydomain.com" rel="nofollow">http://clientaccount.mydomain.com</a>.</p> <p>The problem began when my client wanted her own domain pointing to their account so that <a href="http://www.clientdomain.com" rel="nofollow">http://www.clientdomain.com</a> showed <a href="http://clientaccount.mydomain.com" rel="nofollow">http://clientaccount.mydomain.com</a>, the most obvious solution seemed to be to create a CNAME record in my clients DNS to point to <a href="http://clientaccount.mydomain.com" rel="nofollow">http://clientaccount.mydomain.com</a>. That did not work, when visiting the address an error message stated that "There is no app configured at that hostname", also <code>host www.clientdomain.com</code>gave:</p> <pre><code>www.clientdomain.com is an alias for clientaccount.mydomain.com clientaccount.mydomain.com is an alias for myapp.herokuapp.com myapp.herokuapp.com is an alias for ar.herokuapp.com </code></pre> <p>After some VERY confused support from Heroku, they advised my to use A records instead pointed to their three apex IPs. So changed it but it still didn't work. They then told me to add my clients domain as a custom domain in my Heroku settings which I have done without good result.</p> <p>So my current configuration is as follows:</p> <h2>Myapp at Heroku has following custom domains:</h2> <p>*.mydomain.com www.clientdomain.com</p> <h2>mydomain.com DNS</h2> <p>*.mydomain.com has three A records pointing at Heroku apex IPs</p> <h2>clientdomain.com DNS</h2> <p>In the DNS for clientdomain.com, clientdomain.com (without www) is redirected to www.clientdomain.com (Not sure how they do it but it seems to work.)</p> <p>For www.clientdomain.com there's a CNAME record pointing at clientaccount.mydomain.com</p> <h2>Current state</h2> <p>www.mydomain.com resolves correctly.</p> <p>clientaccount.mydomain.com resolves correctly.</p> <p>www.clientdomain.com goes to www.mydomain.com (without subdomain)</p> <p>So the problem is obvously either in the DNS somehow, does the subdomain fall away somewhere since my application obviously doesn't receive it? Or do I have to change some code in Rails in order to handle this?</p> <p>The subdomain is handled as route constraint, Subdomain class:</p> <pre><code>class Subdomain def self.matches?(request) request.subdomain.present? &amp;&amp; Account.find_by_subdomain(request.subdomain) end end </code></pre> <p>Any input is appreciated!</p> <h2>EDIT</h2> <p>I have done everything that is suggested, do I have to change my application controller as well?</p> <p><strong>application_controller.rb</strong></p> <pre><code>def handle_subdomain if @account = Account.find_by_subdomain(request.subdomain) PgTools.set_search_path @account.id @current_account = @account else @current_account = nil PgTools.restore_default_search_path end end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload