StripPrefix

转发请求之前从路径中删除前缀

从URL路径中删除指定的前缀.

Configuration Examples

# Strip prefix /foobar and /fiibar
labels:
  - "traefik.http.middlewares.test-stripprefix.stripprefix.prefixes=/foobar,/fiibar"
# Strip prefix /foobar and /fiibar
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
  name: test-stripprefix
spec:
  stripPrefix:
    prefixes:
      - /foobar
      - /fiibar
"labels": {
  "traefik.http.middlewares.test-stripprefix.stripprefix.prefixes": "/foobar,/fiibar"
}
# Strip prefix /foobar and /fiibar
labels:
  - "traefik.http.middlewares.test-stripprefix.stripprefix.prefixes=/foobar,/fiibar"
# Strip prefix /foobar and /fiibar
[http.middlewares]
  [http.middlewares.test-stripprefix.stripPrefix]
    prefixes = ["/foobar", "/fiibar"]
# Strip prefix /foobar and /fiibar
http:
  middlewares:
    test-stripprefix:
      stripPrefix:
        prefixes:
          - "/foobar"
          - "/fiibar"

Configuration Options

General

StripPrefix中间件将:

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

Tip

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

prefixes

prefixes选项定义要从请求URL中删除的前缀.

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

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

如果您的后端正在提供资产(例如图像或Javascript文件),则它很可能必须返回正确构造的相对URL.
继续该示例,后端应返回/products/shoes/image.png (而不是/images.png可能无法将其与同一后端关联).

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

forceSlash

可选,默认= true

labels:
  - "traefik.http.middlewares.example.stripprefix.prefixes=/foobar"
  - "traefik.http.middlewares.example.stripprefix.forceslash=false"
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
  name: example
spec:
  stripPrefix:
    prefixes:
      - "/foobar"
    forceSlash: false
"labels": {
  "traefik.http.middlewares.example.stripprefix.prefixes": "/foobar",
  "traefik.http.middlewares.example.stripprefix.forceslash": "false"
}
labels:
  - "traefik.http.middlewares.example.stripprefix.prefixes=/foobar"
  - "traefik.http.middlewares.example.stripprefix.forceSlash=false"
[http.middlewares]
  [http.middlewares.example.stripPrefix]
    prefixes = ["/foobar"]
    forceSlash = false
http:
  middlewares:
    example:
      stripPrefix:
        prefixes:
          - "/foobar"
        forceSlash: false

forceSlash选项可通过在必要时将其替换为/确保所得的剥离路径不是空字符串.

添加此选项是为了保持此中间件的初始(非直观)行为,以避免引入重大更改.

建议将forceSlash显式设置为false .

行为实例
  • forceSlash=true
Path 剥离前缀 Result
/ / /
/foo /foo /
/foo/ /foo /
/foo/ /foo/ /
/bar /foo /bar
/foo/bar /foo /bar
  • forceSlash=false
Path 剥离前缀 Result
/ / empty
/foo /foo empty
/foo/ /foo /
/foo/ /foo/ empty
/bar /foo /bar
/foo/bar /foo /bar