This example demonstrates how to use Traefik as a reverse proxy for a web service defined in a Docker Compose file. Traefik routes incoming requests based on specified rules and forwards the traffic to the web service.
- Docker
- Docker Compose
- Traefik installed and configured (e.g., as a container service)
- Traefik acts as a reverse proxy for the web service.
- Incoming requests are routed based on hostnames and custom headers.
- Traefik uses defined middlewares for additional request processing.
- Traefik listens for incoming requests on specified entry points (e.g.,
websecurefor HTTPS). - Requests matching defined routing rules are forwarded to the
webservice. - Middleware can be applied to handle authentication, whitelisting, etc.
The following docker-compose.yml file shows the service configuration with Traefik labels.
- Copy
docker-compose.ymlto your project directory. - Adjust the volumes and environment variables as needed.
- Run
docker-compose up -dto start the services.
version: '3'
services:
web:
image: $WEB_TAG
restart: unless-stopped
networks:
- traefik
volumes:
- "/home/my_user/portal/storage:/var/www/html/storage:rw"
labels:
- 'traefik.enable=true'
- 'traefik.http.routers.portaltest-whitelisted.entrypoints=websecure'
- "traefik.http.routers.portaltest-whitelisted.rule=Host(`some.domain.com`,`some1.domain.com`,) && HeadersRegexp(`X-Forwarded-For`, `.+`)"
- 'traefik.http.routers.portaltest-whitelisted.middlewares=whitelist-portals@file,default@file'
- 'traefik.http.routers.portaltest-internal.entrypoints=websecure'
- "traefik.http.routers.portaltest-internal.rule=Host(`some.domain.local`)"
- 'traefik.http.routers.portaltest-internal.middlewares=default@file'
- 'traefik.http.services.portaltest.loadbalancer.server.scheme=http'
- 'traefik.http.services.portaltest.loadbalancer.server.port=8080'
networks:
traefik:
external: true