traefik icon indicating copy to clipboard operation
traefik copied to clipboard

Rule Priority v2 vs v3

Open bugficks opened this issue 1 year ago • 2 comments

Welcome!

  • [X] Yes, I've searched similar issues on GitHub and didn't find any.
  • [X] Yes, I've searched similar issues on the Traefik community forum and didn't find any.

What did you do?

Porting router rules from v2 to v3

What did you see instead?

Rule matching priority has changed.

For hostname zzzz.ahost.net:

  • with v2 router from service AAAAA matches first
  • with v3 router from service BBBBB matches first.

According to doc "length of rule" is used but in this case rule length is same for v2 and v3 but priority is not?

What version of Traefik are you using?

Version: 3.1.2 Codename: comte Go version: go1.22.5 Built: 2024-08-06T13:37:51Z OS/Arch: linux/amd64

What is your environment & configuration?

docker-compose.yml:

AAAAA:
  ...
  traefik.http.routers.AAAAA.ruleSyntax: "v2"
  traefik.http.routers.AAAAA.entrypoints: http
  traefik.http.routers.AAAAA.middlewares: "mdw-http-to-https@file"
  traefik.http.routers.AAAAA.rule: "
	  Host(`zzzz.ahost.net`)
	  || Host(`zzzz-host-net.bhost.net`)
	"

BBBBB:
  ...
  traefik.http.routers.BBBBB.ruleSyntax: "v2"
  traefik.http.routers.BBBBB.entrypoints: "http"
  traefik.http.routers.BBBBB.tls: "false"
  traefik.http.routers.BBBBB.middlewares: "mdw-http-to-https@file"
  traefik.http.routers.BBBBB.rule: "
	HostRegexp(`^(.+\\.)?ahost\\.net$`)
	  || HostRegexp(`zzzz(-xxxx)?.bhost.net`)
	"

Add more configuration information here.

If applicable, please paste the log output in DEBUG level

No response

bugficks avatar Aug 20 '24 23:08 bugficks

Hello @bugficks,

Thanks for your interest in Traefik!

At glance, the priority computation did not change between v2 and v3.

When trying the dynamic configuration with v3, I get:

    "test2@file": {
      "entryPoints": [
        "web"
      ],
      "service": "api@internal",
      "rule": "Host(`zzzz.ahost.net`) || Host(`zzzz-host-net.bhost.net`)",
      "ruleSyntax": "v2",
      "priority": 57,
      "status": "enabled",
      "using": [
        "web"
      ]
    },
    "test3@file": {
      "entryPoints": [
        "web"
      ],
      "service": "api@internal",
      "rule": "HostRegexp(`^(.+\\\\.)?ahost\\\\.net$`) || HostRegexp(`zzzz(-xxxx)?.bhost.net`)",
      "ruleSyntax": "v2",
      "priority": 75,
      "status": "enabled",
      "using": [
        "web"
      ]
    }
  },

Unfortunately in v2 the priority is not part of the API, but v3 looks to compute the priority correctly.

Have you tried to configure the priority to see if it fixes your issue?

rtribotte avatar Sep 18 '24 07:09 rtribotte

Yes, that's what I've ended up doing to work around the issue. I've set priority to high number for the rule I want to match first and the other one with default priority.

If you remove priority and change v2 to v3 in your test config, you should be able to reproduce the problem.

bugficks avatar Sep 18 '24 10:09 bugficks

Hello @bugficks,

Sorry for the late feedback,

In the docker compose example you have shared the HostRegexp matcher contains a regular expression that is not supported in v2 (HostRegexp(^(.+\.)?ahost\.net$)), please see the "regex syntax" section in the rule documentation: https://doc.traefik.io/traefik/v2.11/routing/routers/#rule.

However, the new v3 matcher supports this syntax.

I believe this is why this route not matched in v2 because it was broken, and because it is now correctly interpreted in v3, BBBBBB router matches over AAAAAA router.

I'm closing this issue accordingly.

rtribotte avatar Jan 31 '25 15:01 rtribotte