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-serviceTOML 格式:
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 = trueLabels 格式:
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.options | TLS 选项名。 | - | 否 |
tls.domains | 主域名和 SAN 列表。 | - | 否 |
parentRefs | 父路由器列表(用于多层路由)。 | [] | 否 |
observability | 路由器级可观测性配置。 | - | 否 |
observability.metrics | 是否为该路由器启用指标。 | false | 否 |
observability.accessLogs | 是否为该路由器启用访问日志。 | false | 否 |
observability.tracing | 是否为该路由器启用链路追踪。 | false | 否 |
路由器名
- 路由器名中不能包含
@字符。 - 在文件 Provider 中,路由器名必须唯一。
适用场景
- 入口点路由:将不同的入口点(如
web、websecure)路由到不同的服务。 - 域名路由:根据请求的
Host头路由到不同的服务。 - 路径路由:根据请求的 URL 路径路由到不同的服务(如 API 版本、API 路径)。
- TLS 终止:在路由器级别配置 TLS 证书和选项。
在生产环境使用 Traefik OSS?
如果你在工作中使用 Traefik,可以考虑为其添加企业级 API 网关能力或获取 Traefik OSS 的商业支持。