跳到正文
Træfɪk

Router

HTTP 路由器

HTTP 路由器负责将传入请求交给能够处理它们的服务。路由器根据规则分析传入请求,找到匹配项后,通过所有已配置的中间件将请求转发到对应服务。

配置示例

http:
  routers:
    my-router:
      entryPoints:
        - "web"
        - "websecure"
      rule: "Host(`example.com`) && Path(`/api`)"
      priority: 10
      middlewares:
        - "auth"
        - "ratelimit"
      tls:
        certResolver: "letsencrypt"
        options: "modern"
        domains:
          - main: "example.com"
            sans:
              - "www.example.com"
      observability:
        metrics: true
        accessLogs: true
        tracing: true
      parentRefs:
        - "parent-router-1"
        - "parent-router-2"
      service: my-service
[http.routers]
  [http.routers.my-router]
    entryPoints = ["web", "websecure"]
    rule = "Host(`example.com`) && Path(`/api`)"
    priority = 10
    middlewares = ["auth", "ratelimit"]
    service = "my-service"
    parentRefs = ["parent-router-1", "parent-router-2"]

    [http.routers.my-router.tls]
      certResolver = "letsencrypt"
      options = "modern"

      [[http.routers.my-router.tls.domains]]
        main = "example.com"
        sans = ["www.example.com"]

    [http.routers.my-router.observability]
      metrics = true
      accessLogs = true
      tracing = true
labels:
  - "traefik.http.routers.my-router.entrypoints=web,websecure"
  - "traefik.http.routers.my-router.rule=Host(`example.com`) && Path(`/api`)"
  - "traefik.http.routers.my-router.priority=10"
  - "traefik.http.routers.my-router.middlewares=auth,ratelimit"
  - "traefik.http.routers.my-router.service=my-service"
  - "traefik.http.routers.my-router.tls.certresolver=letsencrypt"
  - "traefik.http.routers.my-router.tls.options=modern"
  - "traefik.http.routers.my-router.tls.domains[0].main=example.com"
  - "traefik.http.routers.my-router.tls.domains[0].sans=www.example.com"
  - "traefik.http.routers.my-router.observability.metrics=true"
  - "traefik.http.routers.my-router.observability.accessLogs=true"
  - "traefik.http.routers.my-router.observability.tracing=true"
{
  "Tags": [
    "traefik.http.routers.my-router.entrypoints=web,websecure",
    "traefik.http.routers.my-router.rule=Host(`example.com`) && Path(`/api`)",
    "traefik.http.routers.my-router.priority=10",
    "traefik.http.routers.my-router.middlewares=auth,ratelimit",
    "traefik.http.routers.my-router.service=my-service",
    "traefik.http.routers.my-router.tls.certresolver=letsencrypt",
    "traefik.http.routers.my-router.tls.options=modern",
    "traefik.http.routers.my-router.tls.domains[0].main=example.com",
    "traefik.http.routers.my-router.tls.domains[0].sans=www.example.com",
    "traefik.http.routers.my-router.observability.metrics=true",
    "traefik.http.routers.my-router.observability.accessLogs=true",
    "traefik.http.routers.my-router.observability.tracing=true",
  ]
}

配置选项

字段 说明 默认值 必填
entryPoints 路由器附加到的入口点列表。如果未指定,HTTP 路由器会附加到所有入口点。 所有入口点 否
rule 规则是一组配置了取值的匹配器,用于判断特定请求是否满足指定条件。规则成立时,路由器激活、调用中间件,然后将请求转发到服务。详见规则与优先级。 是
priority 为避免路径重叠,路由默认按规则长度降序排列。优先级直接等于规则长度,因此最长规则具有最高优先级。优先级值为 0 时会被忽略。支持负值。详见规则与优先级。 规则长度 否
middlewares 应用到路由器的中间件列表。中间件按声明顺序应用。可用中间件见中间件概览。 否
tls 路由器的 TLS 配置。指定后,路由器只处理 HTTPS 请求。 否
tls.certResolver 用于自动生成证书的证书解析器名称。详见证书解析器。 否
tls.options 用于配置 TLS 参数(密码套件、最低/最高 TLS 版本、客户端认证等)的 TLS 选项名称。TLS 选项映射到路由器规则中的主机名,而非路由器本身,因此在同一入口点上处理同一主机名的所有路由器必须引用相同的 TLS 选项。详细配置见 TLS 选项,未遵守此要求的后果见 TLS 选项冲突。 default 否
tls.domains 用于显式指定证书域名的域名和使用者备用名称(SAN)列表。使用 ACME 证书解析器时,会从路由器规则中自动提取域名,因此此选项可省略。 否
observability 路由器的可观测性配置。支持按路由器精细控制访问日志、指标和链路追踪。详见可观测性。 继承入口点配置 否
observability.traceVerbosity 定义此路由器链路追踪的详细程度。接受的值为 minimal 和 detailed。 minimal 否
parentRefs 引用父路由器名称以实现多层路由。指定后,此路由器成为子路由器,在父路由器应用其中间件后处理请求。详见多层路由。 否
service 处理匹配请求的服务名称。服务可以是负载均衡、加权轮询、镜像或故障转移服务。详见服务。 是

路由器命名

  • 路由器名称中不允许使用字符 @
  • 在特定提供者的配置(Docker、Kubernetes)中,路由器名称通常根据服务名称和规则自动生成

在生产环境使用 Traefik OSS?

如果你在工作中使用 Traefik,可以考虑为 Traefik OSS 增加企业级 API 网关能力或商业支持。

为 Traefik OSS 增加 API 网关能力快捷且平滑,无需推倒重来,现有配置也会完整保留。请通过这个简短视频了解实际效果。


版权所有 © 头文件软件工作室

文档由 HeadFile Studio 翻译。

Traefik v3.7 中文快照 · 原文版权归 Traefik Labs 及贡献者所有 · MIT 许可证