8
votes

I am configuring Traefik to work as a reverse proxy in my development environment. I currently have applications running on different ports, and different PATHs.

My Environment:

Traefik is running on a Host (192.168.0.10). Listening on port 80, 443 and 8080 (traefik dashboard).

My applications are running on a different host (192.168.0.11).

Web application: 192.168.0.11:8200/web1 Backend: 192.168.0.11:8210/api1 Other web application: 192.168.0.11:8300/web2 Other Backend: 192.168.0.11:8310/api2

I want to redirect all these applications through a same subdomain (dev.domain.com) with Traefik + LetsEncrypt (acme).

For example:

When I access dev.domain.com/web1, I want to redirect all access to 192.168.0.11:8200/web1

When I access dev.domain.com/api1, I want to redirect all access to 192.168.0.11:8210/api1

And so on..

Below is the settings I'm using, Traefik version, etc.

traefil.toml

debug = true
logLevel = "DEBUG"
InsecureSkipVerify = false
defaultEntryPoints = ["https", "http"]

[api]
  entryPoint = "traefik"
  dashboard = true
  address = ":8080"

[entryPoints]
  [entryPoints.http]
  address = ":80"
    [entryPoints.http.redirect]
    entryPoint = "https"
  [entryPoints.https]
  address = ":443"
    [entryPoints.https.tls]

[file]
  directory = "/etc/traefik/rules.d"
  watch = true

[acme]
email = "[email protected]"
storage="/etc/traefik/acme/acme.json"
entryPoint = "https"
acmeLogging=true 
onDemand = true
[acme.dnsChallenge]
  provider = "godaddy"
  delayBeforeCheck = 0
[[acme.domains]]
   main = "domain.com"
[[acme.domains]]
   main = "*.domain.com"

[docker]
endpoint = "unix:///var/run/docker.sock"
domain = "domain.com"
watch = true
exposedbydefault = false

rules.d directory have multiple .toml files.

web1.toml

loglevel = "ERROR"

[backends]
  [backends.web-backend]
    [backends.web-backend.servers.backend_web-backend1]
      url = "http://192.168.0.11:8200/web1"

[frontends]
  [frontends.web-frontend]
    backend = "web-backend"
    X-Custom-Response-Header = true
    SSLRedirect = true
  [frontends.web-frontend.routes.frontend_web-frontend1]
    rule = "Host:dev.domain.com;PathPrefixStrip:/web1"

web2.toml

loglevel = "ERROR"

[backends]
  [backends.web-backend]
    [backends.web-backend.servers.backend_web-backend1]
      url = "http://192.168.0.11:8300/web2"

[frontends]
  [frontends.web-frontend]
    backend = "web-backend"
    X-Custom-Response-Header = true
    SSLRedirect = true
  [frontends.web-frontend.routes.frontend_web-frontend1]
    rule = "Host:dev.domain.com;PathPrefixStrip:/web2"

api1.toml

loglevel = "ERROR"

[backends]
  [backends.api-backend]
    [backends.api-backend.servers.backend_api-backend1]
      url = "http://192.168.0.11:8210"

[frontends]
  [frontends.api-frontend]
    backend = "api-backend"
    X-Custom-Response-Header = true
    SSLRedirect = true
  [frontends.api-frontend.routes.frontend_api-frontend1]
    rule = "Host:dev.domain.com;PathPrefixStrip:/api1"

api2.toml

loglevel = "ERROR"

[backends]
  [backends.api-backend]
    [backends.api-backend.servers.backend_api-backend1]
      url = "http://192.168.0.11:8310"

[frontends]
  [frontends.api-frontend]
    backend = "api-backend"
    X-Custom-Response-Header = true
    SSLRedirect = true
  [frontends.api-frontend.routes.frontend_api-frontend1]
    rule = "Host:dev.domain.com;PathPrefixStrip:/api2"

acme directory is ok! the certificate is created with no erros!

docker-compose.yml

version: "2.1"

services:
  traefik:
    hostname: traefik
    image: traefik:latest
    container_name: traefik
    restart: always
    domainname: ${DOMAINNAME}
    networks:
      - default
      - traefik_proxy
    ports:
      - "80:80"
      - "443:443"
      - "8080:8080"
    environment:
      - GODADDY_API_KEY=${GODADDY_API_KEY}
      - GODADDY_API_SECRET=${GODADDY_API_SECRET}
    labels:
      - "traefik.enable=true"
      - "traefik.backend=traefik"
      - "traefik.frontend.rule=Host:traefik.${DOMAINNAME}"  
      - "traefik.port=8080"
      - "traefik.docker.network=traefik_proxy"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - /opt/traefik:/etc/traefik
      - /opt/traefik/shared:/shared

  helloworld:
    image: matheuscarino/simple-nodejs-app:latest
    container_name: helloworld
    restart: always
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    environment:
      - FOO=BAR
    networks:
      - traefik_proxy
    labels:
      - "traefik.enable=true"
      - "traefik.backend=helloworld"
      - "traefik.frontend.rule=Host:helloworld.${DOMAINNAME}"  
      - "traefik.port=3000"

networks:
  traefik_proxy:
    external:
      name: traefik_proxy
  default:
    driver: bridge

Traefik works fine when I need to redirect requests to applications that are running on the host itself through Docker (via Labels). My helloworld.domain.com application works!

Traefik works fine when I redirect only one application. From the moment I configure the second application in the same subdomain, traefik gets lost in redirects through the PATH.

I searched the internet for use cases like mine, but I did not find people using Traefik to redirect the application outside of the Docker Engine, Kubernetes, etc.

1

1 Answers

3
votes

You need to add this parameter in the frontends "AddPrefix:/myprefix" and remove path in backends URL like this: (url="http://192.168.0.11:8200/myprefix") to (url="http://192.168.0.11:8200)

You just need move this "path" to "AddPrefix" param in frontends configurations if you have PATH in your URL.

All the other configurations are Ok!!

web1.toml

loglevel = "ERROR"

[backends]
  [backends.web-backend]
    [backends.web-backend.servers.backend_web-backend1]
      url = "http://192.168.0.11:8200"

[frontends]
  [frontends.web-frontend]
    backend = "web-backend"
    X-Custom-Response-Header = true
    SSLRedirect = true
  [frontends.web-frontend.routes.frontend_web-frontend1]
    rule = "Host:dev.domain.com;PathPrefixStrip:/web1;AddPrefix:/web1"

web2.toml

loglevel = "ERROR"

[backends]
  [backends.web-backend]
    [backends.web-backend.servers.backend_web-backend1]
      url = "http://192.168.0.11:8300"

[frontends]
  [frontends.web-frontend]
    backend = "web-backend"
    X-Custom-Response-Header = true
    SSLRedirect = true
  [frontends.web-frontend.routes.frontend_web-frontend1]
    rule = "Host:dev.domain.com;PathPrefixStrip:/web2;AddPrefix:/web2"

api1.toml

loglevel = "ERROR"

[backends]
  [backends.api-backend]
    [backends.api-backend.servers.backend_api-backend1]
      url = "http://192.168.0.11:8210"

[frontends]
  [frontends.api-frontend]
    backend = "api-backend"
    X-Custom-Response-Header = true
    SSLRedirect = true
  [frontends.api-frontend.routes.frontend_api-frontend1]
    rule = "Host:dev.domain.com;PathPrefixStrip:/api1;AddPrefix:/api1"

api2.toml

loglevel = "ERROR"

[backends]
  [backends.api-backend]
    [backends.api-backend.servers.backend_api-backend1]
      url = "http://192.168.0.11:8310"

[frontends]
  [frontends.api-frontend]
    backend = "api-backend"
    X-Custom-Response-Header = true
    SSLRedirect = true
  [frontends.api-frontend.routes.frontend_api-frontend1]
    rule = "Host:dev.domain.com;PathPrefixStrip:/api2;AddPrefix:/api2"