ReplacePathRegex

转发请求之前更新路径(使用正则表达式)

ReplaceRegex用正则表达式匹配和替换来替换从URL到另一个的路径.

Configuration Examples

# Replace path with regex
labels:
  - "traefik.http.middlewares.test-replacepathregex.replacepathregex.regex=^/foo/(.*)"
  - "traefik.http.middlewares.test-replacepathregex.replacepathregex.replacement=/bar/$$1"
# Replace path with regex
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
  name: test-replacepathregex
spec:
  replacePathRegex:
    regex: ^/foo/(.*)
    replacement: /bar/$1
# Replace path with regex
- "traefik.http.middlewares.test-replacepathregex.replacepathregex.regex=^/foo/(.*)"
- "traefik.http.middlewares.test-replacepathregex.replacepathregex.replacement=/bar/$1"
"labels": {
  "traefik.http.middlewares.test-replacepathregex.replacepathregex.regex": "^/foo/(.*)",
  "traefik.http.middlewares.test-replacepathregex.replacepathregex.replacement": "/bar/$1"
}
# Replace path with regex
labels:
  - "traefik.http.middlewares.test-replacepathregex.replacepathregex.regex=^/foo/(.*)"
  - "traefik.http.middlewares.test-replacepathregex.replacepathregex.replacement=/bar/$1"
# Redirect with domain replacement
[http.middlewares]
  [http.middlewares.test-replacepathregex.replacePathRegex]
    regex = "^/foo/(.*)"
    replacement = "/bar/$1"
# Redirect with domain replacement
http:
  middlewares:
    test-replacepathregex:
      replacePathRegex:
        regex: "^/foo/(.*)"
        replacement: "/bar/$1"

Configuration Options

General

ReplacePathRegex中间件将:

  • 用指定的路径替换匹配路径.
  • 将原始路径存储在X-Replaced-Path标头中.

regex

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

Warning

定义替换扩展变量时应小心: $1x等效于${1x} ,而不是${1}x (请参见Regexp.Expand ),因此请使用${1}语法.

Tip

可以使用Go PlaygroundRegex101等在线工具测试正则表达式和替换 .

replacement

replacement选项定义了如何修改路径以具有新的目标路径.