ReplacePathRegex
replacePathRegex 中间件会:
- 将匹配的路径替换为指定路径。
- 将原始路径存储到
X-Replaced-Path头中。
配置示例¶
# Replace path with regex
http:
middlewares:
test-replacepathregex:
replacePathRegex:
regex: "^/foo/(.*)"
replacement: "/bar/$1"# Replace path with regex
[http.middlewares]
[http.middlewares.test-replacepathregex.replacePathRegex]
regex = "^/foo/(.*)"
replacement = "/bar/$1"# Replace path with regex
apiVersion: traefik.io/v1alpha1
kind: Middleware
metadata:
name: test-replacepathregex
spec:
replacePathRegex:
regex: "^/foo/(.*)"
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"# Replace path with regex
- "traefik.http.middlewares.test-replacepathregex.replacepathregex.regex=^/foo/(.*)"
- "traefik.http.middlewares.test-replacepathregex.replacepathregex.replacement=/bar/$1"配置选项¶
| 字段 | 说明 | 默认值 | 必填 |
|---|---|---|---|
regex |
用于从请求 URL 中匹配并捕获路径的正则表达式。 | 是 | |
replacement |
替换路径的格式,可以包含捕获的变量。$1x 等同于 ${1x},而非 ${1}x(参阅 Regexp.Expand),因此应使用 ${1} 语法。 |
否 |
提示
可以使用 Go Playground 或 Regex101 等在线工具测试正则表达式和替换规则。
在 YAML 中定义正则表达式时,任何需要转义的字符都必须转义两次:example\.com 应写成 example\\.com。