Heroku SSL on root domain

后端 未结 6 1375
孤独总比滥情好
孤独总比滥情好 2020-12-02 05:26

I am trying to setup SSL for my heroku app. I am using the hostname based SSL add-on. The heroku documentation states the following:

Hostname based SSL will          


        
相关标签:
6条回答
  • 2020-12-02 05:48

    DNS redirects wouldn't care whether the inbound request is http or https so would maintain the original protocol - so would redirect http://foo.com to http://www.foo.com and the same for https.

    You'll need to do it within the application via the gem you found or some other rack redirect gem or if www. is a problem use the IP based SSL addon.

    0 讨论(0)
  • 2020-12-02 05:57

    On the Rails part, to make the redirection, it'd be more sane to make it occur on the router layer, like this (works on Rails 3+):

    Rails.application.routes.draw do
    
      match '/*splat' => redirect { |_, request| request.url.sub('//www.', '//') }, :constraints => { :subdomain => 'www' }
    
      # ...
    
    end
    
    0 讨论(0)
  • 2020-12-02 06:01

    DNSimple offers an ALIAS record type to address this need. You can create an alias from your root domain (a.k.a zone apex) pointing to a CNAME. Read more about it here:

    http://blog.dnsimple.com/introducing-the-alias-record/

    0 讨论(0)
  • 2020-12-02 06:03

    One thing you will like to keep in mind is that google might index both versions of your site if both versions are accessible (Root vs WWW). You would need to setup conicals to handle that which might be a pain to upkeep.

    In my DNS settings I set up a URL / Forward record (DNS Simple)

    URL foo.com     3600        http://www.foo.com
    

    The CNAME setup only needs to be setup for WWW

    CNAME   www.foo.com 3600        providedsslendpoint.herokussl.com
    

    I also had to setup and Alias for my root

    ALIAS   foo.com 3600        providedsslendpoint.herokussl.com
    

    Then I decided to simply replace foo.com with an env variable ENV['SITE_HOST'] (Where SITE_HOST= www.foo.com or whatever I might define). I can control this via my heroku configuration or my .env file (See https://github.com/bkeepers/dotenv). That way, I can control what happens in different environments.

    For example, my test app uses test.foo.com as the url it also has its own SSL endpoint so that works fine for me. This also scales to create staging or qa specific environments as well.

      before_filter :check_domain
    
      def check_domain
        if Rails.env.production? || Rails.env.testing? and request.host.downcase != ENV['SITE_HOST']
          redirect_to request.protocol + ENV['SITE_HOST'] + request.fullpath, :status => 301
        end
      end
    

    From now on, end users will always access www with forced SSL. Old links will suffer a small hang but nothing noticeable.

    0 讨论(0)
  • 2020-12-02 06:05

    Wow...this took me forever, and a bunch of info on the web was wrong. Even Heroku's docs didn't seem to indicate this was possible.

    But Jesper J's answer provides a hint in the right direction: it works with DNSimple's ALIAS record which I guess is some new sort of DNS record they created. I had to switch my DNS service over to them just to get this record type (was previously with EasyDNS).

    To clarify when I say "works" I mean:

    • entire site on SSL using your root domain
    • no browser warnings
    • using Heroku's Endpoint SSL offering ($20/month)

    It works for all of the following urls (redirects them to https://foo.com with no warnings)

    • http://foo.com
    • http://www.foo.com
    • https://www.foo.com
    • https://foo.com

    To summarize the important bits.

    1. move your DNS over to DNSimple (if anyone knows other providers offering an ALIAS record please post them in the comments, they were the only one I could find)
    2. setup Heroku endpoint ssl as normal https://devcenter.heroku.com/articles/ssl-endpoint
    3. Back in DNSimple add an ALIAS record pointing foo.com to your heroku ssl endpoint, something like waterfall-9359.herokussl.com
    4. Also add a CNAME record pointing www.foo.com to your heroku ssl endpoint, waterfall-9359.herokussl.com
    5. finally in your rails (or whatever) app make the following settings:

    in production.rb set

    config.force_ssl = true
    

    in application_controller.rb add

    before_filter :check_domain
    
    def check_domain
      if Rails.env.production? and request.host.downcase != 'foo.com'
        redirect_to request.protocol + 'foo.com' + request.fullpath, :status => 301
      end
    end
    

    This finally seems to work! The key piece seems to be the ALIAS dns record. I'd be curious to learn more about how it works if anyone knows, and how reliable/mature it is. Seems to do the trick though.

    0 讨论(0)
  • 2020-12-02 06:09

    For those heroku users using godaddy previously, I just finish porting the DNS over from godaddy to cloudflare. And the https is working fine now.

    Godaddy DNS is incompatible with heroku. And this is due to:

    Some DNS providers will only offer A records for root domains. Unfortunately, A records will not suffice for pointing your root domains to Heroku because they require a static IP. These records have serious availability implications when used in environments such as on-premise data-centers, cloud infrastructure services, and platforms like Heroku. Since Heroku uses dynamic IP addresses, it’s necessary to use a CNAME-like record (often referred to as ALIAS or ANAME records) so that you can point your root domain to another domain.

    Setting up is fairly simple.

    First, add the nameservers of the cloudflare into godaddy dns manager. These are some examples:

    roxy.ns.cloudflare.com sam.ns.cloudflare.com

    Next, you only need two more steps.

    1. Add a CNAME NAME.com and link it to NAME.com.herokudns.com
    2. That's it. This is assuming that you already have a CNAME www.NAME.com linked to www.NAME.com.herokudns.com

    If you are using Rails, be sure to set config.force_ssl = true at config/environment/production.rb

    0 讨论(0)
提交回复
热议问题