GithubHelp home page GithubHelp logo

PathPrefix RegEx about traefik HOT 3 CLOSED

Bugs5382 avatar Bugs5382 commented on September 24, 2024
PathPrefix RegEx

from traefik.

Comments (3)

Bugs5382 avatar Bugs5382 commented on September 24, 2024 1

@youkoulayley Didn't get a chance before you closed, yes. That worked.

from traefik.

youkoulayley avatar youkoulayley commented on September 24, 2024

Hi @Bugs5382,

Thanks for your interest in Traefik!

PathPrefix doesn't support regexp in v3, you have to use PathRegexp to use regexp now (https://doc.traefik.io/traefik/v3.0/routing/routers/#path-pathprefix-and-pathregexp).

Here's how I rewrote your IngressRoute for my test (I focused exclusively on the frontend part on my tests and used two different whoami to see if the routing seems correct):

---
apiVersion: traefik.io/v1alpha1
kind: IngressRoute
metadata:
  name: web
  namespace: default
spec:
  entryPoints:
    - websecure
  routes:
    - kind: Rule
      match: Host(`toto.localhost`) && (PathRegexp(`^/((assets.*|locales.*))`) || Path(`/`))
      services:
        - kind: Service
          name: whoami1
          port: 80
    - kind: Rule
      match: Host(`toto.localhost`) && (!PathRegexp(`^/((assets.*|locales.*))`) && !Path(`/`))
      middlewares:
        - name: middleware-frontend-replacement-path
      services:
        - kind: Service
          name: whoami2
          port: 80
  
---
apiVersion: traefik.io/v1alpha1
kind: Middleware
metadata:
  name: middleware-frontend-replacement-path
spec:
  replacePath:
    path: /

I replaced the PathPrefix by PathRegexp, and changed the PathPrefix('/') by a strict Path('/') because the it would match everything so it sounded weird (but maybe I'm wrong here).

Details

❯ curl -k https://toto.localhost:8443
Hostname: whoami1-7b6f7cbc7f-jw8zx
IP: 127.0.0.1
IP: ::1
IP: 10.42.0.13
IP: fe80::c848:baff:fe52:9201
RemoteAddr: 10.42.0.12:48320
GET / HTTP/1.1
Host: toto.localhost:8443
User-Agent: curl/8.4.0
Accept: */*
Accept-Encoding: gzip
X-Forwarded-For: 127.0.0.1
X-Forwarded-Host: toto.localhost:8443
X-Forwarded-Port: 8443
X-Forwarded-Proto: https
X-Forwarded-Server: traefik-564cdb4977-4sz4h
X-Real-Ip: 127.0.0.1


❯ curl -k https://toto.localhost:8443/assets/test.js
Hostname: whoami1-7b6f7cbc7f-jw8zx
IP: 127.0.0.1
IP: ::1
IP: 10.42.0.13
IP: fe80::c848:baff:fe52:9201
RemoteAddr: 10.42.0.12:48320
GET /assets/test.js HTTP/1.1
Host: toto.localhost:8443
User-Agent: curl/8.4.0
Accept: */*
Accept-Encoding: gzip
X-Forwarded-For: 127.0.0.1
X-Forwarded-Host: toto.localhost:8443
X-Forwarded-Port: 8443
X-Forwarded-Proto: https
X-Forwarded-Server: traefik-564cdb4977-4sz4h
X-Real-Ip: 127.0.0.1


❯ curl -k https://toto.localhost:8443/locales/test.js
Hostname: whoami1-7b6f7cbc7f-jw8zx
IP: 127.0.0.1
IP: ::1
IP: 10.42.0.13
IP: fe80::c848:baff:fe52:9201
RemoteAddr: 10.42.0.12:48320
GET /locales/test.js HTTP/1.1
Host: toto.localhost:8443
User-Agent: curl/8.4.0
Accept: */*
Accept-Encoding: gzip
X-Forwarded-For: 127.0.0.1
X-Forwarded-Host: toto.localhost:8443
X-Forwarded-Port: 8443
X-Forwarded-Proto: https
X-Forwarded-Server: traefik-564cdb4977-4sz4h
X-Real-Ip: 127.0.0.1


❯ curl -k https://toto.localhost:8443/list
Hostname: whoami2-5f74765f4-cxhrn
IP: 127.0.0.1
IP: ::1
IP: 10.42.0.14
IP: fe80::9460:5bff:fe70:f36f
RemoteAddr: 10.42.0.12:53922
GET / HTTP/1.1
Host: toto.localhost:8443
User-Agent: curl/8.4.0
Accept: */*
Accept-Encoding: gzip
X-Forwarded-For: 127.0.0.1
X-Forwarded-Host: toto.localhost:8443
X-Forwarded-Port: 8443
X-Forwarded-Proto: https
X-Forwarded-Server: traefik-564cdb4977-4sz4h
X-Real-Ip: 127.0.0.1
X-Replaced-Path: /list

It seems to work nicely with this configuration.
If you have any other issues with the rules, don't hesitate to check the migration guide.
If this works for you, I will close the issue.

Edit: I'm closing the issue, if you still have a problem with this subject, don't hesitate to comment and we will reopen it if needed.

from traefik.

Bugs5382 avatar Bugs5382 commented on September 24, 2024

@youkoulayley Just waking up for the day. I totally looked around to see if there was a new "matcher". Once I am in the office I will give it a try. :)

from traefik.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.