Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to configure traefik as a reverse proxy for a single domain and multiple path

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.

like image 593
Matheus Carino Porto Ramos Avatar asked Aug 29 '18 17:08

Matheus Carino Porto Ramos


People also ask

Can traefik be used as a reverse proxy?

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. Traefik is running on a Host (192.168.0.10). Listening on port 80, 443 and 8080 (traefik dashboard).

Can I use traefik as a docker proxy?

We now have our Traefik proxy running, configured to work with Docker, and ready to monitor other Docker containers. Let’s start some containers for Traefik to act as a proxy for. With the Traefik container running, you’re ready to run applications behind it.

How do I configure the traefik server?

To configure the Traefik server, we’ll create a new configuration file called traefik.toml using the TOML format. TOML is a configuration language similar to INI files, but standardized. This file lets us configure the Traefik server and various integrations, or providers, we want to use.

How do I create a reverse proxy for my server?

Next, edit your main server’s virtual hosts file to create a reverse proxy. Here’s the code you need to add: The ProxyPass directive will create a reverse proxy for the paths specified, while the ProxyPassReverse directive will intercept the HTTP response headers sent through this reverse proxy and rewrite them to match the Apache server.


1 Answers

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"
like image 140
rafambbr Avatar answered Oct 24 '22 04:10

rafambbr