Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Traefik - header matching

Traefik routing

Proxy to services based on Accept header.

Run

docker-compose up -d

Test

Headers

  1. Get latest version: curl -H "Accept: application/json" http://localhost/
  2. Get version 1: curl -H "Accept: application/json+v1" http://localhost/
  3. Get specificaly version 2 (aka latest): curl -H "Accept: application/json+v2" http://localhost/

URL prefix and path matching

  1. Get projects: curl http://localhost/api/projects
  2. Get all events: curl http://localhost/api/events
  3. Get all events related to a project: curl http://localhost/api/projects/1/events
version: '2'
services:
v1:
image: nginx:alpine
ports:
- 80
links:
- reverse-proxy
labels:
- "traefik.enable=true"
- "traefik.frontend.rule=PathPrefix:/;Headers: Accept, application/json+v1"
volumes:
- ./v1.json:/usr/share/nginx/html/index.html:ro
v2:
image: nginx:alpine
ports:
- 80
links:
- reverse-proxy
labels:
- "traefik.enable=true"
- "traefik.frontend.rule=PathPrefix:/;HeadersRegexp: Accept, application/(json|json+v2)"
volumes:
- ./v2.json:/usr/share/nginx/html/index.html:ro
prefixed:
image: nginx:alpine
ports:
- 80
links:
- reverse-proxy
labels:
- "traefik.enable=true"
- "traefik.frontend.priority=10"
- "traefik.frontend.rule=PathPrefixStrip:/api/projects"
volumes:
- ./v1.json:/usr/share/nginx/html/index.html:ro
events:
image: nginx:alpine
ports:
- 80
links:
- reverse-proxy
labels:
- "traefik.enable=true"
- "traefik.frontend.priority=20"
- "traefik.frontend.rule=Path:/api{path:.*}/events"
volumes:
- ./v2.json:/usr/share/nginx/html/api/events:ro
- ./v3.json:/usr/share/nginx/html/api/projects/1/events:ro
reverse-proxy:
image: traefik
command: --web --web.address=:81 \
--docker --docker.watch --docker.domain=localhost \
--debug --logLevel=DEBUG \
--defaultentrypoints=http \
--entrypoints='Name:http Address::80' \
--docker.exposedbydefault=false
ports:
- 80:80
- 81:81
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- /dev/null:/traefik.toml
{"version": 1}
{"version": 2}
{"version": 3}
@juanpabloaj

This comment has been minimized.

Copy link

juanpabloaj commented Mar 29, 2019

Thanks for your example.

Have you used Traefik header matching with consul tags?

@juanpabloaj

This comment has been minimized.

Copy link

juanpabloaj commented Mar 29, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.