5

Does anyone have a dead simple configuration example of an incoming http request on port 80 to be redirect to a specific static ip on port 8080 for traefik?

I can't find it absolutely anywhere! Been on their website for a few hours now.

This is their best example from here https://docs.traefik.io/routing/routers/#configuration-example:

## Forwarding all (non-tls) requests on port 3306 to a database service
## Static configuration
entryPoints:
  web:
    address: ":80"
  mysql:
    address: ":3306"   

I would like something like the following from NGINX:

events { }

http {
    upstream mop {
        server mop:3000;
    }
    server {
        listen   80;
        server_name  localhost;
        location /mop {
            proxy_pass http://mop;
        }
        location /mop/1 {
            proxy_pass http://mop;
        }
        location /mop/2 {
            proxy_pass http://mop;
        }
    }
}
basickarl
  • 37,187
  • 64
  • 214
  • 335

2 Answers2

4

For this simple example, in your static configuration you need to create an entryPoint and a provider.

The entryPoint defines on which port traefik will accept incoming requests and the provider defines some existing infrastructure component that traefik can query for service discovery. This can be some orchestration system like Docker or Kubernetes. But in this simple example we need a File provider.

# Static configuration
[entryPoints]
  [entryPoints.web]
    address = ":80"

[providers]
  [providers.file]
    filename = "/path/to/dynamic/conf.toml"

In the file provider we define the path to the dynamic configuration. In the dynamic configuration we create a service (or backend). This is the server to which we want traefik to route request.

We also need to create a router. The router matches rules such as domain or path and routes those requests to the desired service. In this example we route the path prefix / (all requests) to the service my-service, which then points to http://localhost:8080.

# Dynamic configuration
[http]
  [http.routers]
    [http.routers.my-router]
      rule = "PathPrefix(`/`)"
      service = "my-service"

    [http.services]
      [http.services.my-service.loadBalancer]
        [[http.services.my-service.loadBalancer.servers]]
          url = "http://localhost:8080"
Andrija Čehko
  • 443
  • 8
  • 14
2

from some reference of their site: doc.traefik.io

I have a docker-compose.yml file here base on that.

version: "3.3"

services:

  traefik:
    image: "traefik:latest"
    container_name: "traefik"
    ports:
      - "80:80"
      - "443:443"
      - "8080:8080"
    labels:
      # Enable Traefik for this service, to make it available in the public network
      - traefik.enable=true
      # https-redirect middleware to redirect HTTP to HTTPS
      # It can be re-used by other stacks in other Docker Compose files
      - traefik.http.middlewares.https-redirect.redirectscheme.scheme=https
      - traefik.http.middlewares.https-redirect.redirectscheme.permanent=true
      # Dashboard HTTP here
      - traefik.http.routers.traefik.rule=Host(`traefik.your.domain`)
      - traefik.http.routers.traefik.entrypoints=http
      - traefik.http.routers.traefik.middlewares=https-redirect
      # Dashboard HTTPS here
      - traefik.http.routers.traefik-https.rule=Host(`traefik.your.domain`)
      - traefik.http.routers.traefik-https.entrypoints=https
      - traefik.http.routers.traefik-https.tls=true
      # Use the special Traefik service api@internal with the web UI/Dashboard
      - traefik.http.routers.traefik-https.service=api@internal
      # Use the Let's Encrypt resolver created below
      - traefik.http.routers.traefik-https.tls.certresolver=myresolver
    command:
      # Enable the Traefik log, for configurations and errors
      - --log
      # Enable the Dashboard and API
      - --api
      - "--providers.docker=true"
      - "--providers.docker.exposedbydefault=false"
      # Create an entrypoint "http" listening on address 80
      - --entrypoints.http.address=:80
      # Create an entrypoint "https" listening on address 443
      - --entrypoints.https.address=:443
      - "--certificatesresolvers.myresolver.acme.tlschallenge=true"
      # - "--certificatesresolvers.myresolver.acme.httpchallenge.entrypoint=http"
      #- "--certificatesresolvers.myresolver.acme.caserver=https://acme-staging-v02.api.letsencrypt.org/directory"
      - "--certificatesresolvers.myresolver.acme.email=your@email.addr"
      - "--certificatesresolvers.myresolver.acme.storage=/letsencrypt/acme.json"
    volumes:
      - "./letsencrypt:/letsencrypt"
      - "/var/run/docker.sock:/var/run/docker.sock:ro"

  yourServices:
    image: yourServices
    container_name: yourServices
    labels:
      - "traefik.enable=true"
      - traefik.http.routers.yourServices-http.rule=Host(`your.domain`)
      - traefik.http.routers.yourServices-http.entrypoints=http
      - traefik.http.routers.yourServices-http.middlewares=https-redirect
      - "traefik.http.routers.yourServices.rule=Host(`your.domain`)"
      - "traefik.http.routers.yourServices.entrypoints=https"
      - "traefik.http.routers.yourServices.tls.certresolver=myresolver"

I know it's ugly. I'm not an expert here. Fortunately, it works for me. Hope that could do some help.