Overview
概述¶
要将 HTTP 路由器配置为处理 HTTPS 流量,请在其定义中加入 tls 字段。
此字段告知 Traefik,该路由器只应处理 TLS 请求,并忽略非 TLS 流量。
默认情况下,带有 TLS 字段的 HTTP 路由器会终止 TLS 连接, 也就是说,它会向服务发送解密后的数据。 TLS 配置提供了多个用于微调 TLS 行为的选项, 包括自动生成证书、自定义 TLS 选项以及显式指定域名。
路由器 TLS 替换入口点 TLS
入口点的 http.tls 设置仅在路由器没有定义自身 tls 段时应用。
定义路由器 tls 段——即使为空,或仅包含 certResolver、options、domains 等任意字段——
都意味着 Traefik 不会将该配置与该路由器的入口点默认值合并。路由器 TLS
配置会完全替换入口点 TLS 配置。
例如,配置了以下入口点 TLS 选项:
entryPoints:
websecure:
address: :443
http:
tls:
options: modern-tls@file
而路由器只设置证书解析器:
http:
routers:
sample:
rule: Host(`example.com`)
service: sample
tls:
certResolver: letsencrypt
那么入口点的 options(modern-tls@file)不会应用到 sample。
要保留两者,请在入口点上同时设置它们,或在路由器上同时设置 options 和 certResolver。
配置示例¶
http:
routers:
my-https-router:
rule: "Host(`example.com`) && Path(`/api`)"
service: "my-http-service"
tls:
certResolver: "letsencrypt"
options: "modern-tls"
domains:
- main: "example.com"
sans:
- "www.example.com"
- "api.example.com"[http.routers.my-https-router]
rule = "Host(`example.com`) && Path(`/api`)"
service = "my-http-service"
[http.routers.my-https-router.tls]
certResolver = "letsencrypt"
options = "modern-tls"
[[http.routers.my-https-router.tls.domains]]
main = "example.com"
sans = ["www.example.com", "api.example.com"]labels:
- "traefik.http.routers.my-https-router.rule=Host(`example.com`) && Path(`/api`)"
- "traefik.http.routers.my-https-router.service=my-http-service"
- "traefik.http.routers.my-https-router.tls=true"
- "traefik.http.routers.my-https-router.tls.certresolver=letsencrypt"
- "traefik.http.routers.my-https-router.tls.options=modern-tls"
- "traefik.http.routers.my-https-router.tls.domains[0].main=example.com"
- "traefik.http.routers.my-https-router.tls.domains[0].sans=www.example.com,api.example.com"{
"Tags": [
"traefik.http.routers.my-https-router.rule=Host(`example.com`) && Path(`/api`)",
"traefik.http.routers.my-https-router.service=my-http-service",
"traefik.http.routers.my-https-router.tls=true",
"traefik.http.routers.my-https-router.tls.certresolver=letsencrypt",
"traefik.http.routers.my-https-router.tls.options=modern-tls",
"traefik.http.routers.my-https-router.tls.domains[0].main=example.com",
"traefik.http.routers.my-https-router.tls.domains[0].sans=www.example.com,api.example.com"
]
}配置选项¶
| 字段 | 说明 | 默认值 | 必填 |
|---|---|---|---|
options |
用于配置 TLS 参数(密码套件、最低/最高 TLS 版本、客户端认证等)的 TLS 选项名称。详细配置见 TLS 选项。 | "" (运行时解析为 default) |
否 |
certResolver |
通过 ACME 提供者(如 Let's Encrypt)自动生成证书时使用的证书解析器名称。详见证书解析器一节。 | "" | 否 |
domains |
用于显式指定证书域名的域名和使用者备用名称(SAN)列表。详见自定义域名一节。 | [] | 否 |
证书解析器¶
tls.certResolver 选项允许指定证书解析器,通过 ACME 提供者(如 Let's Encrypt)自动生成证书。
为路由器配置证书解析器后,
Traefik 会自动获取和管理路由器规则(Host 匹配器)或 tls.domains 配置中所指定域名的 TLS 证书(以 tls.domains 为优先)。
前提条件
- 必须在静态配置中定义证书解析器
- 路由器必须启用
tls - 必须为证书解析器配置 ACME 质询类型
自定义域名¶
使用 ACME 证书解析器时,会从路由器规则中自动提取域名,
但 tls.domains 选项支持显式指定需要生成证书的域名和使用者备用名称(SAN)。
这可以精细控制证书生成,并优先于从路由器规则自动提取的域名。
每个域名都必须具有指向 Traefik 的 A/AAAA 记录。
在生产环境使用 Traefik OSS?
如果你在工作中使用 Traefik,可以考虑为 Traefik OSS 增加企业级 API 网关能力或商业支持。
为 Traefik OSS 增加 API 网关能力快捷且平滑,无需推倒重来,现有配置也会完整保留。请通过这个简短视频了解实际效果。