A common requirement for applications is to have a subdomain per customer that users belonging to that customer can visit. Example of this include: Slack (https://elixir.slack.com, https://phoenix.slack.com, etc.) and ReadMe.
This blog post will go through how to set up your Phoenix application so that it can be used in the same way.
The source code for this repository is available at https://github.com/Gazler/phoenix-subdomain-demo - there is a commit to represent each step in this post.
Getting Started
The first thing we will need is a new Phoenix application. Since it is focused on subdomains, I am going to call it subdomainer:
mix phoenix.new subdomainer
Once the app has been created and all the dependencies have been installed, start the app and visit it at http://localhost:4000.
mix phoenix.server
Next we need to ensure that it is accessible via a separate domain and subomains, so the following needs to be added to your /etc/hosts
file:
127.0.0.1 subdomainer.dev foo.subdomainer.dev bar.subdomainer.dev
With these additions, you should also be able to access the application via: http://subdomainer.dev:4000, http://foo.subdomainer.dev:4000 and http://bar.subdomainer.dev:4000
Currently these all point to the same page, but we are going to modify it so that it displays information about the particular app that we are trying to visit.