You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Traefik配置跨域名不同路径重定向问题求助(Docker Compose标签方式)

解决Traefik重定向example1.com/update到example2.com/test的配置问题

修正后的完整Docker Compose标签配置:

labels:
  - "traefik.enable=true"
  - "traefik.http.routers.sasms.entrypoints=https"
  # 匹配example1.com的/update路径
  - "traefik.http.routers.sasms.rule=Host(`example1.com`) && Path(`/update`)"
  - "traefik.http.routers.sasms.tls=true"
  - "traefik.http.routers.sasms.tls.certresolver=http"
  # 使用Traefik内置空服务,无需实际后端
  - "traefik.http.routers.sasms.service=noop@internal"
  - "traefik.docker.network=proxy"
  # 关联重定向中间件(保留default@file则用逗号分隔)
  - "traefik.http.routers.sasms.middlewares=sasms,default@file"
  # 精确匹配/update路径的正则
  - "traefik.http.middlewares.sasms.redirectregex.regex=^https://example1.com/update$"
  # 修正拼写错误,指定目标地址
  - "traefik.http.middlewares.sasms.redirectregex.replacement=https://example2.com/test"
  - "traefik.http.middlewares.sasms.redirectregex.permanent=false"

关键修改说明:

  • 路由规则调整:原规则仅匹配域名,新增&& Path(/update),确保只有访问example1.com/update时才触发重定向
  • 正则表达式修正:原(.)*会匹配所有路径,改为精确匹配^https://example1.com/update$,避免误重定向其他路径
  • 拼写错误修复:把exapmple2.com修正为example2.com
  • 服务优化:使用Traefik内置的noop@internal空服务,无需指向实际后端容器(仅重定向场景无需处理请求)
  • 中间件关联:将自定义的sasms重定向中间件加入路由的middlewares列表,确保规则生效

内容的提问来源于stack exchange,提问作者Figaro

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 13:43:17