Setting up a custom domain name on Heroku with Elixir + Phoenix (without an SSL certificate)

Hey there! This tutorial explains what you need to do in order to set up a custom domain on Heroku with Elixir + Phoenix without an SSL certificate.

Problem: After following the Phoenix instructions to first make my application ready for Heroku and then the custom domain instructions for setting up a custom domain for a Heroku application, I kept falling short. The behaviour I was experiencing was that if I typed my custom domain into the address bar, it would redirect me to my Heroku domain — example.herokuapp.com. This was frustrating because the problem wasn’t initially clear (or at least I couldn’t see it). It turns out that the suggested setup for an application on Heroku built with Elixir + Phoenix enforces that you use a secure setup — with SSL. This is fantastic for applications that need to be secure but not so great for the times where you’re building a quick application or website that doesn’t handle any sensitive data.

WARNING!: DO NOT FOLLOW THIS TUTORIAL IF YOUR WEBSITE DEALS WITH SENSITIVE DATA (CREDIT CARD INFORMATION, USER LOGINS ETC…) INSTEAD, YOU SHOULD SET UP YOUR DOMAIN WITH AN SSL CERTIFICATE.

Insecure Solution: Below we’ll walk through the steps you need to take in order to set up a custom domain on Heroku with Elixir + Phoenix for applications that don’t need to be secure:

The first thing that you need to do in order to set up a custom domain is to make your application Heroku ready”. To do this you can follow the quick and easy guide in the Phoenix documentation which can be found here: https://hexdocs.pm/phoenix/heroku.html#making-our-project-heroku-ready

Once you’ve done this you need to make a couple of small adjustments. Where it says to replace this line

# config/prod.exs
...
url: [host: “example.com”, port: 80],
...

with these lines

# config/prod.exs
...
url: [scheme: "https", host: "mysterious-meadow-6277.herokuapp.com", port: 443],
force_ssl: [rewrite_on: [:x_forwarded_proto]],
...

you need to instead only change the scheme from “https” to “http” and then remove the force_ssl line completely so that you are left with this:

# config/prod.exs
...
url: [scheme: "http", host: "mysterious-meadow-6277.herokuapp.com", port: 443],
...

That’s all you need to do on the application level.

The next thing you’ll want to do is to follow the instructions on the Heroku website for setting up a custom domain that points to your application within the Heroku dns. These instructions can be found here: