How to create a router rule for a wildcard?

i want to reverse proxy a wildcard subdomain along the subdomain itself for the very same docker container. i have the following in the labels section of that container -

- "traefik.enable=true"
- "traefik.http.routers.container.rule=Host(`adguard.example.com`) || HostRegexp(`{subdomain:[a-z]+}.adguard.example.com`)"
- "traefik.http.services.container.loadbalancer.server.port=80"

adguard.example.com seems be working well but i get 404 page not found error on wildcard - *.adguard.example.com. Any clue on what i must be missing here

The doc states this format

HostRegexp(`example.com`, `{subdomain:[a-z]+}.example.com`, ...)

There is also another (currently open) question about HostRegexp, so it seems to be not an easy topic.

Even with this, only adguard.example.com is working, *.adguard.example.com still gives 404 page not found

- "traefik.http.routers.container.rule=Host(`adguard.example.com`) || HostRegexp(`{subdomain:[a-z]+}.adguard.example.com`)"
version: "3"

services:
  traefik:
    image: traefik:v2.10
    command:
      - --providers.docker=true
      - --providers.docker.exposedbydefault=false
      - --entrypoints.web.address=:80

    ports:
      - "80:80"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro

  whoami:
    image: traefik/whoami:v1.10.1
    labels:
      - traefik.enable=true
      - traefik.http.routers.whoami.rule=Host(`adguard.example.localhost`) || HostRegexp(`{subdomain:[a-z]+}.adguard.example.localhost`)
$ curl adguard.example.localhost
Hostname: 350b021f5a2d
IP: 127.0.0.1
IP: 172.21.0.2
RemoteAddr: 172.21.0.3:35146
GET / HTTP/1.1
Host: adguard.example.localhost
User-Agent: curl/8.2.1
Accept: */*
Accept-Encoding: gzip
X-Forwarded-For: 172.21.0.1
X-Forwarded-Host: adguard.example.localhost
X-Forwarded-Port: 80
X-Forwarded-Proto: http
X-Forwarded-Server: cddcb1945fb1
X-Real-Ip: 172.21.0.1
$ curl foo.adguard.example.localhost
Hostname: 350b021f5a2d
IP: 127.0.0.1
IP: 172.21.0.2
RemoteAddr: 172.21.0.3:35146
GET / HTTP/1.1
Host: foo.adguard.example.localhost
User-Agent: curl/8.2.1
Accept: */*
Accept-Encoding: gzip
X-Forwarded-For: 172.21.0.1
X-Forwarded-Host: foo.adguard.example.localhost
X-Forwarded-Port: 80
X-Forwarded-Proto: http
X-Forwarded-Server: cddcb1945fb1
X-Real-Ip: 172.21.0.1
$ curl bar.adguard.example.localhost
Hostname: 350b021f5a2d
IP: 127.0.0.1
IP: 172.21.0.2
RemoteAddr: 172.21.0.3:35146
GET / HTTP/1.1
Host: bar.adguard.example.localhost
User-Agent: curl/8.2.1
Accept: */*
Accept-Encoding: gzip
X-Forwarded-For: 172.21.0.1
X-Forwarded-Host: bar.adguard.example.localhost
X-Forwarded-Port: 80
X-Forwarded-Proto: http
X-Forwarded-Server: cddcb1945fb1
X-Real-Ip: 172.21.0.1

Everything works as expected, your problem is not related to the rule.

@xd003 please don't edit your posts.

Are you using Traefik v3?

Traefik v3 is in beta state and you are posting in the Traefik v2 category.

Yes i am using traefik v3, my bad, had missed that one, edited to reflect the same

So you are not using the right syntax.


version: "3"

services:
  traefik:
    image: traefik:v3.0
    command:
      - --providers.docker=true
      - --providers.docker.exposedbydefault=false
      - --entrypoints.web.address=:80

    ports:
      - "80:80"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro

  whoami:
    image: traefik/whoami:v1.10.1
    labels:
      - traefik.enable=true
      - traefik.http.routers.whoami.rule=Host(`adguard.example.localhost`) || HostRegexp(`.+\.adguard\.example\.localhost`)
$ curl adguard.example.localhost    
Hostname: 09010fe37486
IP: 127.0.0.1
IP: 172.22.0.2
RemoteAddr: 172.22.0.3:43142
GET / HTTP/1.1
Host: adguard.example.localhost
User-Agent: curl/8.2.1
Accept: */*
Accept-Encoding: gzip
X-Forwarded-For: 172.22.0.1
X-Forwarded-Host: adguard.example.localhost
X-Forwarded-Port: 80
X-Forwarded-Proto: http
X-Forwarded-Server: e323edab02fd
X-Real-Ip: 172.22.0.1
$ curl foo.adguard.example.localhost
Hostname: 09010fe37486
IP: 127.0.0.1
IP: 172.22.0.2
RemoteAddr: 172.22.0.3:43142
GET / HTTP/1.1
Host: foo.adguard.example.localhost
User-Agent: curl/8.2.1
Accept: */*
Accept-Encoding: gzip
X-Forwarded-For: 172.22.0.1
X-Forwarded-Host: foo.adguard.example.localhost
X-Forwarded-Port: 80
X-Forwarded-Proto: http
X-Forwarded-Server: e323edab02fd
X-Real-Ip: 172.22.0.1
$ curl bar.adguard.example.localhost
Hostname: 09010fe37486
IP: 127.0.0.1
IP: 172.22.0.2
RemoteAddr: 172.22.0.3:43142
GET / HTTP/1.1
Host: bar.adguard.example.localhost
User-Agent: curl/8.2.1
Accept: */*
Accept-Encoding: gzip
X-Forwarded-For: 172.22.0.1
X-Forwarded-Host: bar.adguard.example.localhost
X-Forwarded-Port: 80
X-Forwarded-Proto: http
X-Forwarded-Server: e323edab02fd
X-Real-Ip: 172.22.0.1
1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.