Table of Contents
Loading headings...
A single application on Heroku can have any number of domains assigned to it, but you can only add a domain to one app. This means that by default you can’t serve example.com from the example-1 while example.com/blog is served from example-2.
We ran into this problem with plane.com recently where we have a constellation of apps (pilot-co, pilot-blog, pilot-stories, etc.) which we wanted to host under a single domain.
We found a way to do that by putting a custom HAProxy instance, also hosted on Heroku, in front of all other Heroku apps we use.
1. Set up
Let’s say you have two apps on Heroku already:
example-com running https://example.com
example-blog running https://blog.example.com
We will need a new app for your load balancer:
Then create an app on Heroku:
2. Installing Docker
You will deploy it to Heroku using Docker. We found it to be easier to manage than creating a custom buildpack.
Luckily, installing Docker on your machine is easy. Get Docker Toolbox and follow its setup instructions.
To verify that you have a working Docker installation, open your terminal and run:
To deploy a Docker container to Heroku you will need heroku-docker:
Heroku requires an app.json and Procfile manifests to be able to run your app.
Your Procfile should look something like this:
Then initialize Docker assets for the app:
3. Configuring HAProxy
Your Dockerfile is where we add instructions for Heroku on how to compile HAProxy:
One last thing we need to do is configure HAProxy to route requests from our main app (called frontend) to all other apps (called backends).
HAProxy’s configuration manual is relatively easy to understand, and after some fine-tuning you should end up with something like this:
You can verify your setup locally by starting Docker:
and opening the browser:
4. Deploying your load balancer to Heroku
If you’re satisfied with the outcome, it’s time to deploy it to Heroku:
After you verified that your new setup works on https://example-lb.herokuapp.com, you can remove the example.com domain from example-com and attach it to example-lb.
5. After you’re done
Requests to
https://example.comwill go through example-lb and be served fromexample-com.Requests to
https://example.com/blogwill also go throughexample-lbbut be served fromexample-bloginstead.All this will be completely hidden from your users. At no point they should see
example-blog.herokuapp.comor any domain other thanexample.com.
If you’re using SSL (which this guide assumes you were) you can safely remove the SSL add on from all apps other than example-lb. Traffic between Heroku apps will be encrypted using their *.herokuapp.com certificate.
Additional resources
Want product news & updates?
Sign up for our newsletter.



