Skip to content

Traefik HTTP 路由器文档

HTTP 路由器

HTTP 路由器负责将传入请求连接到可以处理它们的服务。路由器根据规则分析传入请求,当找到匹配时,通过配置的任何中间件将请求转发到相应的服务。

配置示例

YAML 格式:

yaml
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

TOML 格式:

toml
[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 格式:

yaml
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"

Kubernetes 格式:

yaml
apiVersion: traefik.io/v1alpha1
kind: IngressRoute
metadata:
  name: my-router
spec:
  entryPoints:
    - web
    - websecure
  routes:
    - match: Host(`example.com`) && Path(`/api`)
      kind: Rule
      services:
        - name: my-service
          port: 80
      middlewares:
        - name: auth
        - name: ratelimit
      priority: 10
      observability:
        metrics: true
        accessLogs: true
        tracing: true
  tls:
    certResolver: letsencrypt
    options: modern
    domains:
      - main: example.com
        sans:
          - www.example.com

配置选项

字段描述默认值必填
entryPoints路由器监听的入口点列表。-
rule匹配请求的规则表达式。-
priority路由器优先级,数值越大优先级越高。0
middlewares要附加到路由器的中间件列表。[]
service路由器要转发请求到的服务名。-
tls启用并配置 TLS。-
tls.certResolver用于获取证书的证书解析器。-
tls.optionsTLS 选项名。-
tls.domains主域名和 SAN 列表。-
parentRefs父路由器列表(用于多层路由)。[]
observability路由器级可观测性配置。-
observability.metrics是否为该路由器启用指标。false
observability.accessLogs是否为该路由器启用访问日志。false
observability.tracing是否为该路由器启用链路追踪。false

路由器名

  • 路由器名中不能包含 @ 字符。
  • 在文件 Provider 中,路由器名必须唯一。

适用场景

  • 入口点路由:将不同的入口点(如 webwebsecure)路由到不同的服务。
  • 域名路由:根据请求的 Host 头路由到不同的服务。
  • 路径路由:根据请求的 URL 路径路由到不同的服务(如 API 版本、API 路径)。
  • TLS 终止:在路由器级别配置 TLS 证书和选项。

在生产环境使用 Traefik OSS?

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

基于 MIT 协议发布