February 4, 2019

783 words 4 mins read

Traefik – New reverse proxy!

Traefik – New reverse proxy!

I was recently introduced to a new software called Traefik.

A reverse proxy / load balancer that’s easy, dynamic, automatic, fast, full-featured, open source, production proven, provides metrics, and integrates with every major cluster technology… No wonder it’s so popular!

What else to say? Sounds exactly like a tool I would love. As my setup is already based on a great diversity of docker containers it sounded interesting to me to have a reverse proxy that is handling routing and load balancing automatically to them and can even manage the necessary Let’s encrypt certificates for me.


Traefik config

I am using my own dns server (https://portal.beechy.de) for Let’s encrypt dns verification which is supporting the httpreq provider. If you are interesed in using my dns server aswell, feel free to create an account for nothing, but a smile. It’s supporting all diffrent types of records, low ttls, dyndns with ipv6 support and an inbuild email server with spam protection.

Currently there is a bug in legos (acme libary used by Traefik) httpreq provider which cuts the endpoint url, but the PR is likely to be merged soon. https://github.com/xenolf/lego/pull/781

version: '3'

    image: traefik
    container_name: traefik
      - HTTPREQ_ENDPOINT=https://portal.beechy.de/api/dns/acme/
      - HTTPREQ_USERNAME=apitokeninportalsettings
      - HTTPREQ_PASSWORD=notneeded
      - traefik.enable=true
      - traefik.backend=traefik
      - traefik.frontend.rule=Host:traefik.example.com
      - traefik.port=8080
    restart: always
      - 80:80
      - 443:443
      - /var/run/docker.sock:/var/run/docker.sock
      - ./traefik.toml:/traefik.toml
      - ./acme.json:/acme.json
      - ./rules:/rules
# traefik.toml

#logLevel = "DEBUG"
defaultEntryPoints = ["http", "https"]

address = ":8080"
  users = ["admin:httpasswd-hash"]

  address = ":80"
      entryPoint = "https"
  address = ":443"

email = "root@example.com"
storage = "acme.json"
entryPoint = "https"
onHostRule = true
exposedbydefault = false
acmeLogging = true
  provider = "httpreq"
  delayBeforeCheck = 10

endpoint = "unix:///var/run/docker.sock"
watch = true
exposedByDefault = false
usebindportip = true
swarmMode = false
#swarmModeRefreshSeconds = 15

directory = "/rules"
watch = true

Website configs

I am currently providing 4 types of “websites”:

  • proxies to containers (Docker)
  • proxies via backend VPN
  • PHP websites
  • redirects (alias domains)

Proxies to containers (Docker)

Proxies to Docker containers like gitea or portainer and fusionauth are super simple to setup. I only had to recreate my containers and add a few labels to them:

docker run -td \
     --name portainer \
     --restart always \
     -v /var/run/docker.sock:/var/run/docker.sock \
     -v $PWD/data:/data \
     -l traefik.enable=true \
     -l traefik.backend=portainer \
     -l traefik.frontend.rule=Host:portainer.example.com \
     -l traefik.port=9000 \

Proxies via backend VPN (File)

For my proxies accessed via my secured backend VPN (things like plex on my home server) I decided to use the Traefiks file backend with multiple .toml files. One for each domain I wanted to add.

# rules/plex.example.com.toml

        url = ""
        weight = 1

    entryPoints = ["http", "https"]
    backend = "plex"
    passHostHeader = true
    rule = "Host:plex.example.com"

PHP websites

But the tricky part started by adding my php websites to Traefik. As Traefik is unable to talk to php directly I decided to leave nginx in front of php as the old fast webserver and let Traefik only manange ssl termination. Most of the professionals out there would have one seperate nginx + php container per domain, but my VPS doesn’t have that much disk space fo seperate images and I am lazy in setting php up with smtp and so on in multiple containers, so I took one nginx and one php for all sites and added multiple frontends in Traefik pointing to my “php-backend”:

# rules/php-backend.toml

        url = "http://nginx"
        weight = 1
# rules/beechy.de.toml

    entryPoints = ["http", "https"]
    backend = "php"
    passHostHeader = true
    rule = "Host:beechy.de,www.beechy.de"

Special nginx behind reverse proxy settings

After trying to run Traefik for the first time I faced two problems I already know from the good old days using ha-proxy. First: php isn’t not getting the correct client ip and second: browsers are sent into redirecting loops from some applications, because they are trying to force https which they wont get. This is due Traefik terminating ssl traffic and only requesting http from nginx.

By using real_ip_header X-Forwarded-For; we tell the ngx_http_realipmodule from which header it should get the correct client ip and by passing _fastcgi_param HTTPS $fastcgi_param_https_variable; php knows if it was accessed over https and wont redirect us.

location ~ \.php$ {
  try_files $uri =404;
  fastcgi_split_path_info ^(.+\.php)(/.+)$;
  fastcgi_pass php:9000;
  fastcgi_index index.php;
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  set_real_ip_from ::/0;
  real_ip_header X-Forwarded-For;
  fastcgi_param HTTPS $fastcgi_param_https_variable;
  include fastcgi_params;
# nginx.conf

http {
    map $http_x_forwarded_proto $fastcgi_param_https_variable {
         default '';
         https 'on';



Redirects (alias domains)

redirect from rediret.example.com to example.com

# rules/redirect.example.com.toml

    entryPoints = ["http", "https"]
    rule = "Host:redirect.example.com"