Redirect without Host

I set up a docker swarm stack that redirects requests to whoami service based on host.

  1. I need to also redirect to the same service when the request was sent to the public IP address directly, instead of a domain name(and so presumably doesn't have a Host in the request, so I don't know how to match it).
  2. How do I make a wildcard rule that redirects everything from a given entrypoint(web in this case) to my service?

I mainly care about http requests, but it would be nice to know how to do this for TCP and UDP.
Here is my docker compose:

version: "3.3"


    image: "traefik:v2.10"
    container_name: "traefik"
      - "--log.level=WARNING"
      - "--api.insecure=true"
      - "--providers.docker=true"
      - "--api=true"
      - "--dashboard=true"
      - "--providers.docker.exposedbydefault=false"
      - "--entrypoints.web.address=:80"
      - "80:80"
      - "8080:8080"
      - "/var/run/docker.sock:/var/run/docker.sock:ro"

    image: "traefik/whoami"
    container_name: "simple-service"
      - "traefik.enable=true"
      - "traefik.http.routers.whoami"
      - "traefik.http.routers.whoami.rule=Host(`sample.domain`)"
      - "traefik.http.routers.whoami.entrypoints=web"

Check HostRegexp() or maybe ClientIP(``) (doc).

As fallback the priority has to be low, so if the string length of this rule is shorter it will be automatically lower, otherwise set priority 1 (that’s low).

I realized somehow
- "traefik.http.routers.whoami.rule=Host("
works(at least on my local network). Doing this on a public IP I didn't test.

  1. I'm still looking for a solution that doesn't need me to hardcode the IP.
  2. Why does this work? I thought a request directed to an IP doesn't have a Host entry in the header... (It is in the header, don't know why I thought it wouldn't be)

For http router you could try .rule=PathPrefix(`/`) to match all. If the rule is shorter (in length) than your Host match it will automatically have a lower priority and will be treated as fallback. Otherwise set the priority manually.