StripPrefixRegex

转发请求之前,使用路径删除前缀(使用正则表达式)

从URL路径中删除匹配的前缀.

Configuration Examples

labels:
  - "traefik.http.middlewares.test-stripprefixregex.stripprefixregex.regex=/foo/[a-z0-9]+/[0-9]+/"
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
  name: test-stripprefixregex
spec:
  stripPrefixRegex:
    regex:
      - "/foo/[a-z0-9]+/[0-9]+/"
- "traefik.http.middlewares.test-stripprefixregex.stripprefixregex.regex=/foo/[a-z0-9]+/[0-9]+/"
"labels": {
  "traefik.http.middlewares.test-stripprefixregex.stripprefixregex.regex": "/foo/[a-z0-9]+/[0-9]+/"
}
labels:
  - "traefik.http.middlewares.test-stripprefixregex.stripprefixregex.regex=/foo/[a-z0-9]+/[0-9]+/"
[http.middlewares]
  [http.middlewares.test-stripprefixregex.stripPrefixRegex]
    regex = ["/foo/[a-z0-9]+/[0-9]+/"]
http:
  middlewares:
    test-stripprefixregex:
      stripPrefixRegex:
        regex:
          - "/foo/[a-z0-9]+/[0-9]+/"

Configuration Options

General

StripPrefixRegex中间件将:

  • 删除匹配的路径前缀.
  • 将匹配的路径前缀存储在X-Forwarded-Prefix标头中.

Tip

如果您的后端在根路径( / )上侦听,但应在特定前缀上可路由,请使用stripPrefixRegex中间件.

regex

regex选项是正则表达式,用于匹配请求URL中的路径前缀.

Tip

可以使用在线工具(例如Go PlaygroundRegex101)测试正则表达式.

例如, /products可以匹配/products ,也可以匹配/products /products/shoes/products/shirts .

由于路径是在转发之前剥离的,因此您的后端应在/上侦听.

如果您的后端正在提供资产(例如图像或Javascript文件),则它很可能必须返回正确构造的相对URL.

继续该示例,后端应返回/products/shoes/image.png (而不是/images.png可能无法将其与同一后端关联).

可以查询X-Forwarded-Prefix标头来动态构建此类URL.