admin管理员组

文章数量:1129457

I want to configure Traefik to listen on port 80 and then proxy the traffic to my services which are located in docker containers. I don't want to use docker labels, but a single YAML configuration to have all the configurations in a single place.

I have created a docker file like this:

FROM traefik:v3.3

COPY ./traefik.yml /etc/traefik/traefik.yml

EXPOSE 80 8080

then a compose file like below:

services:
  proxy:
    build:
      context: ./proxy
      dockerfile: Dockerfile
    networks:
      - proxy
      - frontend
      - app
      - mongodb_dashboard
      - docker_dashboard
    deploy:
      mode: replicated
      replicas: 1
      endpoint_mode: vip
      restart_policy:
        condition: on-failure
        delay: 5s
        max_attempts: 3
    ports:
      - 80:80
      - 8080:8080

networks:
  proxy:
    name: proxy
  frontend:
    name: frontend
  app:
    name: app
  mongodb_dashboard:
    name: mongodb_dashboard
    external: true
  docker_dashboard:
    name: docker_dashboard
    external: true

and my Traefik configuration file (traefik.yml) is:

# Define the entry points to listen on port 80 for HTTP
entryPoints:
  http:
    address: ":80"  # Listen on port 80 for HTTP requests

# Enable access logs
log:
  level: INFO
  format: common
  filePath: "/dev/stdout"  # Logs to stdout

# Enable the Traefik dashboard (optional, for debugging purposes)
api:
  insecure: true  # Enable insecure dashboard (for local testing, don't use in production)
  dashboard: true  # Enable the dashboard view


# HTTP routers, services, and middlewares
http:
  routers:

    # Backend subdomain
    backend:
      rule: HostRegexp(`^backend\\..+$`)
      entryPoints:
        - http
      service: app-service
      middlewares:
        - add-headers

    # Docker Dashboard subdomain
    dockerdashboard:
      rule: HostRegexp(`^dockerdashboard\\..+$`)
      entryPoints:
        - http
      service: docker-dashboard-service
      middlewares:
        - add-headers

    # MongoDB Dashboard subdomain
    mongodashboard:
      rule: HostRegexp(`^mongodashboard\\..+$`)
      entryPoints:
        - http
      service: mongodb-dashboard-service
      middlewares:
        - add-headers

    # Catch-all router for unmatched subdomains
    catch-all:
      rule: HostRegexp(`.+`)
      entryPoints:
        - http
      service: frontend-service
      middlewares:
        - add-headers

  services:
    frontend-service:
      loadBalancer:
        servers:
          - url: "http://frontend:3000"

    app-service:
      loadBalancer:
        servers:
          - url: "http://app:80"

    docker-dashboard-service:
      loadBalancer:
        servers:
          - url: "http://docker_dashboard:9000"

    mongodb-dashboard-service:
      loadBalancer:
        servers:
          - url: "http://mongodb_dashboard:8081"

  middlewares:
    # Middleware to add headers to all requests
    add-headers:
      headers:
        customRequestHeaders:
          X-Real-IP: {remote_ip}
          X-Forwarded-For: {remote_ip}
          X-Forwarded-Proto: {scheme}

FIY, the whole project is located:

locally, I have setuped a domain named tarhche.loc like below:

127.0.0.1   tarhche.loc dockerdashboard.tarhche.loc mongodashboard.tarhche.loc

by running Traefik, I always get 404 when I go to each subdomain! each subdomain is associated with a service, for example mongodashboard.tarhche.loc should proxy the requests to mongo-express which I run it using: .mongodb_dashboard.yaml

and even when I go to Traefik dashboard I can't see the defined HTTP services, providers and Middlewares!

could you please give me some insight about how I can solve the issue? btw, the whole code exists in:

thanks everyone.

I want to configure Traefik to listen on port 80 and then proxy the traffic to my services which are located in docker containers. I don't want to use docker labels, but a single YAML configuration to have all the configurations in a single place.

I have created a docker file like this:

FROM traefik:v3.3

COPY ./traefik.yml /etc/traefik/traefik.yml

EXPOSE 80 8080

then a compose file like below:

services:
  proxy:
    build:
      context: ./proxy
      dockerfile: Dockerfile
    networks:
      - proxy
      - frontend
      - app
      - mongodb_dashboard
      - docker_dashboard
    deploy:
      mode: replicated
      replicas: 1
      endpoint_mode: vip
      restart_policy:
        condition: on-failure
        delay: 5s
        max_attempts: 3
    ports:
      - 80:80
      - 8080:8080

networks:
  proxy:
    name: proxy
  frontend:
    name: frontend
  app:
    name: app
  mongodb_dashboard:
    name: mongodb_dashboard
    external: true
  docker_dashboard:
    name: docker_dashboard
    external: true

and my Traefik configuration file (traefik.yml) is:

# Define the entry points to listen on port 80 for HTTP
entryPoints:
  http:
    address: ":80"  # Listen on port 80 for HTTP requests

# Enable access logs
log:
  level: INFO
  format: common
  filePath: "/dev/stdout"  # Logs to stdout

# Enable the Traefik dashboard (optional, for debugging purposes)
api:
  insecure: true  # Enable insecure dashboard (for local testing, don't use in production)
  dashboard: true  # Enable the dashboard view


# HTTP routers, services, and middlewares
http:
  routers:

    # Backend subdomain
    backend:
      rule: HostRegexp(`^backend\\..+$`)
      entryPoints:
        - http
      service: app-service
      middlewares:
        - add-headers

    # Docker Dashboard subdomain
    dockerdashboard:
      rule: HostRegexp(`^dockerdashboard\\..+$`)
      entryPoints:
        - http
      service: docker-dashboard-service
      middlewares:
        - add-headers

    # MongoDB Dashboard subdomain
    mongodashboard:
      rule: HostRegexp(`^mongodashboard\\..+$`)
      entryPoints:
        - http
      service: mongodb-dashboard-service
      middlewares:
        - add-headers

    # Catch-all router for unmatched subdomains
    catch-all:
      rule: HostRegexp(`.+`)
      entryPoints:
        - http
      service: frontend-service
      middlewares:
        - add-headers

  services:
    frontend-service:
      loadBalancer:
        servers:
          - url: "http://frontend:3000"

    app-service:
      loadBalancer:
        servers:
          - url: "http://app:80"

    docker-dashboard-service:
      loadBalancer:
        servers:
          - url: "http://docker_dashboard:9000"

    mongodb-dashboard-service:
      loadBalancer:
        servers:
          - url: "http://mongodb_dashboard:8081"

  middlewares:
    # Middleware to add headers to all requests
    add-headers:
      headers:
        customRequestHeaders:
          X-Real-IP: {remote_ip}
          X-Forwarded-For: {remote_ip}
          X-Forwarded-Proto: {scheme}

FIY, the whole project is located: https://github.com/Tarhche/infrastructure/tree/main/proxy

locally, I have setuped a domain named tarhche.loc like below:

127.0.0.1   tarhche.loc dockerdashboard.tarhche.loc mongodashboard.tarhche.loc

by running Traefik, I always get 404 when I go to each subdomain! each subdomain is associated with a service, for example mongodashboard.tarhche.loc should proxy the requests to mongo-express which I run it using: https://github.com/Tarhche/infrastructure/blob/main/compose.mongodb_dashboard.yaml

and even when I go to Traefik dashboard I can't see the defined HTTP services, providers and Middlewares!

could you please give me some insight about how I can solve the issue? btw, the whole code exists in: https://github.com/Tarhche/infrastructure

thanks everyone.

Share Improve this question asked Jan 8 at 9:06 MahdiMahdi 3253 silver badges15 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

Traefik uses static (entrypoints, providers, certresolver, etc.) and dynamic (routers, middlewares, services, tls, etc.) configuration (doc). The dynamic configuration needs to be loaded by a provider in static config, like providers.docker or providers.file.

Check simple Traefik example (and the other folders) for best practice.

I changed my configuration like below:

  • one file for static configurations named traefik.yml contains below configurations:
# Define the entry points to listen on port 80 for HTTP
entryPoints:
  http:
    address: ":80"  # Listen on port 80 for HTTP requests

# Enable access logs
log:
  level: DEBUG
  format: common
  filePath: "/dev/stdout"  # Logs to stdout

# Enable the Traefik dashboard (optional, for debugging purposes)
api:
  insecure: true  # Enable insecure dashboard (for local testing, don't use in production)
  dashboard: true  # Enable the dashboard view

providers:
  file:
    directory: /etc/traefik-dynamic
  • one file for dynamic configurations named dynamic.yml contains below configurations:
# HTTP routers, services, and middlewares
http:
  routers:

    # Backend subdomain
    backend:
      rule: HostRegexp(`^backend\..+\..+$`)
      entryPoints:
        - http
      service: app-service
      middlewares:
        - add-headers

    # Docker Dashboard subdomain
    dockerdashboard:
      rule: HostRegexp(`^dockerdashboard\..+\..+$`)
      entryPoints:
        - http
      service: docker-dashboard-service
      middlewares:
        - add-headers

    # MongoDB Dashboard subdomain
    mongodashboard:
      rule: HostRegexp(`^mongodashboard\..+\..+$`)
      entryPoints:
        - http
      service: mongodb-dashboard-service
      middlewares:
        - add-headers

    # Catch-all router for unmatched subdomains
    catch-all:
      rule: HostRegexp(`.+`)
      entryPoints:
        - http
      service: frontend-service
      middlewares:
        - add-headers

  services:
    frontend-service:
      loadBalancer:
        servers:
          - url: "http://frontend:3000"

    app-service:
      loadBalancer:
        servers:
          - url: "http://app:80"

    docker-dashboard-service:
      loadBalancer:
        servers:
          - url: "http://docker_dashboard:9000"

    mongodb-dashboard-service:
      loadBalancer:
        servers:
          - url: "http://mongodb_dashboard:8081"

  middlewares:
    # Middleware to add headers to all requests
    add-headers:
      headers:
        customRequestHeaders:
          X-Real-IP: {remote_ip}
          X-Forwarded-For: {remote_ip}
          X-Forwarded-Proto: {scheme}

since I use traefik in docker, I configured the docker file like below:

FROM traefik:v3.3

COPY ./traefik.yml /etc/traefik/traefik.yml
COPY ./dynamic.yml /etc/traefik-dynamic/dynamic.yml

EXPOSE 80 8080

then on my /etc/hosts I configured a domain and subdomains:

127.0.0.1   tarhche.loc dockerdashboard.tarhche.loc mongodashboard.tarhche.loc

by opening a subdomain I can see the result.

本文标签: Traefik as proxy always returns 404Stack Overflow