Search code examples
ruby-on-railsrubyssl

Rails SSL issue: (https://example.com) didn't match request.base_url (http://example.com)


I just installed an SSL certificate on my site. Unfortunately it has broken the login functionality. After submitting the login form on the site it just redirects to the home page. Checking the rails log shows this error:

(https://example.com) didn't match request.base_url (http://example.com)

Here is my virtualhosts file. I guess I need to force SSL somehow?

<VirtualHost *:80>
   ServerName example.com
   ServerAlias www.example.com
   Redirect permanent / https://example.com/
</VirtualHost>

<VirtualHost *:443>
   ServerAdmin hello@example.com
   ServerName example.com
   ServerAlias www.example.com
   SSLEngine on
   SSLCertificateFile /home/user/sharetribe/lib/certificates/www_example_com.crt
   SSLCertificateKeyFile /home/user/sharetribe/lib/certificates/example.com.key
   SSLCertificateChainFile /home/user/sharetribe/lib/certificates/www_example_com.ca-bundle

   ProxyRequests Off
   <Proxy *>
      Order deny,allow
      Allow from all
   </Proxy>
   ProxyPass / http://localhost:3000/
   ProxyPassReverse / http://localhost:3000/
</VirtualHost>

Solution

  • Just run in to the same error. In config/environments/production.rb make sure you have set:

    config.force_ssl = true
    

    While not strictly related to this issue, after setting this setting you will need to ensure that your reverse proxy (if you have one) is set up to forward the protocol used to rails by sending the X-Forwarded-Proto header from the proxy to rails. The way this is done depends on which reverse proxy you use (Apache, nginx, etc) and how you have configured it so it's best you look up the specific documentation for the reverse proxy you are using.