Skip to content

Traefik HTTP TLS 概览文档

HTTP TLS 概览

当 HTTP 路由器配置为处理 HTTPS 流量时,在其定义中包含 tls 字段。 该字段告诉 Traefik 路由器处理 TLS 请求,而忽略非 TLS 流量。

默认情况下,带有 TLS 字段的 HTTP 路由器将终止 TLS 连接(这意味着它将解密的数据发送给服务)。 TLS 配置提供了几个用于微调 TLS 行为的选项,包括自动证书生成、自定义 TLS 选项和显式域指定。

配置示例

YAML 格式:

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

TOML 格式:

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

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

配置选项

字段描述默认值必填
tls启用 TLS。false
tls.certResolver用于自动获取证书的证书解析器名。-
tls.optionsTLS 选项名(定义最小/最大版本、密码套件)。-
tls.domains显式指定要使用的域名列表。-
tls.domains[].main主域名。-
tls.domains[].sans主题备用名称列表。-

TLS 终止 vs TLS 直通

  • TLS 终止(默认):Traefik 接受客户端的 TLS 连接,解密后以明文形式转发到后端。后端不再处理 TLS,性能更好但后端看不到客户端的原始证书。
  • TLS 直通tls.passthrough: true):Traefik 将 TLS 连接透传给后端,后端处理 TLS 终止。性能较差但后端可以看到原始 TLS 信息。

适用场景

  • 启用 HTTPS:通过 tls: {} 启用基本 TLS。
  • 自动证书:使用 certResolver 启用 Let's Encrypt 自动证书。
  • 自定义 TLS 设置:通过 options 引用 TLSOptions 资源。
  • 多域名管理:通过 domains 显式指定要使用的证书。

相关文档


在生产环境使用 Traefik OSS?

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

基于 MIT 协议发布