blog-image

Nov 01, 2024

27 min read

Using Traefik as a Reverse Proxy for Multiple Hosts with Docker Compose

Written by

Abdelhadi Dyouri
This guide will cover how to use Traefik as a reverse proxy to manage multiple hosts with Docker Compose. Suppose you had deployed several microservices using Docker and you want to connect these microservices to the outside world. You will probably use some reverse proxy solution that will act as a frontend for these microservices. Nginx is the most popular and efficient reverse proxy solution, but it was written before container technology (like Docker) became so popular. The modern solution for hosting multiple hosts with Docker is now Traefik.

What is Traefik?

Traefik is a modern reverse proxy solution written in GO to meet modern containerized challenges.
Unlike a traditional reverse proxy, which requires manual configuration, Traefik uses service discovery to dynamically configure routing. Traefik supports all major protocols, leveraging a rich set of middleware for load balancing, rate-limiting, circuit-breakers, mirroring, authentication, and more. Traefik also supports SSL termination and works with ACME providers (like Let’s Encrypt) for automatic certificate generation. Source.
Traefik was developed with containerized technology in mind, which means there’s a lot of new features available in comparison with other reverse proxy solutions. When running, Traefik does the following and more:
  • Dynamically updates the configuration and loads it without a restart.
  • Load balances requests from the outside world using multiple available algorithms.
  • Enables HTTPS for microservices using Let’s Encrypt.
  • Provides a clean web UI.
  • Supports for WebSocket, HTTP/2, GRPC.
  • Provides metrics to Rest, Prometheus, Datadog, Statsd, InfluxDB.
Traefik Reverse Proxy for Multiple Hosts with Docker Compose

Why Use Traefik as a Reverse Proxy for Multiple Hosts?

Say, for example, you want to remove one of the microservices from the container or change its port, and, as a result, you have to edit the Nginx configuration file to reflect the new container settings. Now, consider another situation where you add, delete, or scale the microservices application several times in a day—now, you’re editing the routes and other settings several times in the reverse proxy configuration file. In this article, we will check how to install Traefik and configure it in a standalone and Dockerized environment, and then give examples of connecting it to your microservices in the backend.

Prerequisites to Install Traefik

  • A VPS running Ubuntu 24.04. Get your Linux VPS hosting from a reputable and trustworthy provider like SSD Nodes. We offer powerful Ubuntu servers and the best deals. Take a look at our offerings and prepare for your mind to be blown 🤯.
  • A non-root, sudo-enabled user. If you only have a root user, see our SSH tutorial for details on creating new users.
  • A working Docker installation—for information about how to install Docker, check out our getting started with Docker tutorial.

Using Traefik without Docker

Our first goal is to install Traefik  and use it to route a few applications (backend) using Traefik configuration without Dockerizing them. To do that, we’ll install Apache and Nginx and configure them to use different ports, as they will act as a backend. First, update your system package index:
$ sudo apt update
Install Apache:
$ sudo apt install apache2
Change the port number to 8083 from 80 in /etc/apache2/ports.conf and restart Apache.
$ sudo nano /etc/apache2/ports.conf
$ sudo systemctl restart apache2
Next, install Nginx:
$ sudo apt install nginx
Create a custom welcome page for the Nginx server to distinguish between the two servers:
$ sudo mkdir /var/www/nginx_html
$ sudo echo '<!DOCTYPE html><html><body><h1>Welcome to My Nginx Page</h1></body></html>' | sudo tee /var/www/nginx_html/index.html
Next, edit the default Nginx configuration file:
$ sudo nano /etc/nginx/sites-available/default 
Point Nginx to the new /var/www/nginx_html directory, and the 8082 port number:
server {
        listen 8082 default_server;
        listen [::]:8082 default_server;
        # ...
        # ...
        # ...
        
        root /var/www/nginx_html;
        # ...
        # ...
        # ...
        }
Next, restart Nginx:
$ sudo systemctl restart nginx
Now both Apache and Nginx will be available in the port number 8083 and 8082, respectively.

Step 1: Install Traefik

To install Traefik as a standalone environment, we need to download the binary and edit the Traefik configuration file and rules files. Download the Traefik binary from the releases page:
$ wget https://github.com/traefik/traefik/releases/download/v3.1.4/traefik_v3.1.4_linux_amd64.tar.gz
Remember to replace v3.1.4 with the latest release, or another binary that is compatible with your system. Check the integrity of the downloaded file and compare it to the value in the traefik_v3.1.4_checksums.txt file:
$ sha256sum ./traefik_v3.1.4_linux_amd64.tar.gz
Next, extract the compressed file:
$ tar -zxvf traefik_v3.1.4_linux_amd64.tar.gz
You can now use ./traefik to run Traefik configurations.

Step 2: Traefik Configuration

To host multiple websites with Traefik, you’ll need to create a configuration file that routes each domain to the corresponding port. First, we’ll create a traefik.yml file that gets automatically detected by Traefik:
log:
  level: "DEBUG"

entryPoints:
  web:
   address: ":80"

providers:
  file:
    filename: "./rules.yml"
    watch: true
In this traefik.yml file, you have the following:
  • log: Sets log level to DEBUG for detailed output.
  • entryPoints: Defines web entry point to listen on port 80 for HTTP traffic.
  • providers: This tells Traefik to load dynamic configuration (such as routes and services) from the rules.yml file, and watch for changes.

Step 3: Set up Traefik Rules

Next, create a rules.yml file to define dynamic routing rules:
http:
  routers:
    router1:
      rule: "Host(`nginx.example.com`)"
      service: service1
      entryPoints:
        - web
    router2:
      rule: "Host(`apache.example.com`)"
      service: service2
      entryPoints:
        - web

  services:
    service1:
      loadBalancer:
        servers:
          - url: "http://127.0.0.1:8082"
    service2:
      loadBalancer:
        servers:
          - url: "http://127.0.0.1:8083"
Here, you definerouters to define traffic rules and route them to services:
  • router1: Routes traffic requests for nginx.example.com to service1.
  • router2: Routes traffic requests for apache.example.com to service2.
The services are defined as follows:
  • service1: Forwards traffic to the Nginx server at
Continue reading this article
by subscribing to our newsletter.
Subscribe now

A note about tutorials: We encourage our users to try out tutorials, but they aren't fully supported by our team—we can't always provide support when things go wrong. Be sure to check which OS and version it was tested with before you proceed.

If you want a fully managed experience, with dedicated support for any application you might want to run, contact us for more information.

Leave a Reply