treafik配置文件汇总版
从中文官网和英文官网汇总整理而来,个人理解,有的可能有错误哈
- entrypoin
[entryPoints] ########################################### # 默认: # [entryPoints] # [entryPoints.http] # address = ":80" #################################################################################### [entryPoints.http] # 定义 http address = ":80" #监听80端口 # compress = true # 启用gzip格式压缩支持 # 白名单 [entryPoints.http.whitelist] sourceRange = ["10.42.0.0/16", "152.89.1.33/32", "afed:be44::/16"] #useXForwardedFor = true # 启用x-forward-for [entryPoints.http.redirect] # 标记 重定向 entryPoint = "https" # 重定向到名为https的entrypoint #! 另一种重定向,URL重写 # [entryPoints.http.redirect] # regex = "^http://localhost/(.*)" # replacement = "http://mydomain/$1" #? permanent = true 永久?可以不设置 # 授权及认证,有各种认证:参:https://docs.traefik.io/configuration/entrypoints/ [entryPoints.http.auth.basic] # 标记 开启基础认证 # 使用2组用户名/密码: 密码可以以MD5、SHA1或BCrypt方式加密:你可以使用htpasswd来生成这些用户名密码。 users = ["test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/", "test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0"] #!另一种基础认证:使用2组用户名/域/密码,可以使用htdigest来生成这些用户名/域/密码 #users = ["test:traefik:a2688e031edb4be6a3797f3882655c05 ", "test2:traefik:518845800f9e2bfb1f1f740ec24f074e"] [entryPoints.http.proxyProtocol] insecure = true # 标记不安全 trustedIPs = ["10.10.10.1", "10.10.10.2"] [entryPoints.http.forwardedHeaders] trustedIPs = ["10.10.10.1", "10.10.10.2"] ####################################################################################### [entryPoints.https] # 定义https address = ":443" [entryPoints.https.tls] # 开启ssl clientCAFiles = ["tests/clientca1.crt", "tests/clientca2.crt"] #? 提供自身证书? optional = false #?? 含义未知 ## 包含以下2行 指定一个https入口点使用一个较低的TLS版本并且指定一组密钥(在crypto/tls包中定义): # MinVersion = "VersionTLS12" ## CipherSuites = ["TLS_RSA_WITH_AES_256_GCM_SHA384"] [[entryPoints.https.tls.certificates]] certFile = "tests/traefik.crt" # 证书 keyFile = "tests/traefik.key" # 密钥 #! 另一组证书和密钥 # #[[entryPoints.https.tls.certificates]] # #CertFile = "integration/fixtures/https/snitest.org.cert" # #KeyFile = "integration/fixtures/https/snitest.org.key"
- frontend
[frontends] ############################################################# [frontends.frontend1] backend = "backend2" # 指定响应的backend passHostHeader = true # 转发客户端请求Header中的 Host 字段到后端 passTLSClientCert = true # 转发客户端的证书信息到backend priority = 10 # 优先级,数值越大,越先匹配。 否则按规则的字符数长度由大到小排序 entrypoints = ["https"] # 覆盖默认的entrypoint,也可以有多个entrypoints = ["http", "https"] #! 注意:多条规则之间是且关系,需同时满足各个规则才能进入本frontend [frontends.frontend1.routes.test_1] # 定义一条路由规则 rule = "Host:test.localhost,test2.localhost" #DetailDown 路由明细, 或关系(逗号分割) [frontends.frontend1.routes.test_2] rule = "Host:test3.localhost;Path:/test" #DetailDown 路由明细, 且关系(分号分割) #ForUp#路由明细,支持类型及说明 # 原文:Rules may be classified in one of two groups: Modifiers and matchers. # Matchers: # Headers: Content-Type, application/json: 通过 Headers 可以添加一个匹配规则来匹配请求头部包含的值。它接受要匹配的键/值对序列。 # HeadersRegexp: Content-Type, application/(text|json): 也可以在 Headers 中使用正则表达式。它接受要匹配的键/值对序列,序列内容解析是通过正则匹配的。 # Host: traefik.io, www.traefik.io: 匹配请求 Host 必需在给定域名列表内。 # HostRegexp: traefik.io, {subdomain:[a-z]+}.traefik.io: 添加匹配请求 Host 的正则表达式。 # Method: GET, POST, PUT: Method 可以添加一个HTTP请求方法的匹配。它接受要匹配的一个或多个请求方法序列。 # Path: /products/, /articles/{category}/{id:[0-9]+}: Path 可以添加一个URL路径的匹配。它接受固定串或正则串。 # PathStrip: 和 Path 相同,但从请求的URL路径中去掉的给定的前缀。 # PathStripRegex: /articles/{category}/{id:[0-9]+} 和PathStrip类似,不过是按正则匹配 # PathPrefix: PathPrefix 可以添加一个URL路径前缀的匹配。它匹配给定模版中的完整URL路径前缀。 # PathPrefixStrip: 和 PathPrefix 相同,但从请求的URL路径中去掉的给定的前缀。 # PathPrefixStripRegex: /articles/{category}/{id:[0-9]+} 和PathPrefixStrip类似,不过是按正则匹配 # Query: foo=bar, bar=baz 匹配查询,接收形如key=value的请求 # Modifies: # AddPrefix: /products: Add path prefix to the existing request path prior to forwarding the request to the backend. # ReplacePath: /serverless-path: Replaces the path and adds the old path to the X-Replaced-Path header. Useful for mapping to AWS Lambda or Google Cloud Functions. # ReplacePathRegex: ^/api/v2/(.*) /api/$1: Replaces the path with a regular expression and adds the old path to the X-Replaced-Path header. Separate the regular expression and the replacement by a space. # Both Matchers and Modifies(先match再modify,以match为主) # PathStrip(含义如上) # PathStripRegex(含义如上) # PathPrefixStrip(含义如上) # PathPrefixStripRegex(含义如上) # 用户自定义header,在request或在response上,rule匹配上了就会自动追加,如果值为“”则表示移除 [frontends.frontend1.headers.customresponseheaders] X-Custom-Response-Header = "True" [frontends.frontend1.headers.customrequestheaders] X-Script-Name = "test" # 追加自定义header X-Custom-Request-Header = "" # 移除自定义header # 安全设置 [frontends.frontend1.headers] FrameDeny = true # 拒绝header中X-Frame-Options存在的请求, SSLRedirect = true # 只接受https请求,否则会进行https 301重定向 # 频率控制,2种都得满足才行 [frontends.frontend1.ratelimit] extractorfunc = "client.ip" # 统计维度。一般有request.host和client.ip,但也可以设置 request.header.ANY_HEADER 中的任意维度 # 规则1: 10s内接收的请求数的平均值不大于100,高峰最大200请求 [frontends.frontend1.ratelimit.rateset.rateset1] period = "10s" average = 100 burst = 200 # 规则2: 3s内接收的请求数的平均值不大于5,高峰最大10请求 [frontends.frontend1.ratelimit.rateset.rateset2] period = "3s" average = 5 burst = 10 ####################### website ##################################### [frontends] [frontends.website] backend = "website" [frontends.website.errors] # website返回5xx状态码时,转向error后端,访问其 url/{status}.html [frontends.website.errors.network] status = ["500-599"] backend = "error" query = "/{status}.html" [frontends.website.routes.website] rule = "Host: website.mydomain.com"
- backend
[backends] ########################################################### [backends.backend1] [backends.backend2.LoadBalancer] # 标记负载均衡策略 method = "drr" # 轮询算法: (默认wrr: 加权轮询)(drr: 动态轮询: 这会为表现比其他服务器好的服务器增加权重) #sticky = true # 启用负载均衡器的粘滞会话(sticky sessions)特性(个人理解为同一个client的请求会被转发到同一个server上),但由于默认会开启,所以本行可以注释 [backends.backend1.loadbalancer.stickiness] # 禁用负载均衡器的粘滞会话特性 [backends.backend1.maxconn] # 标记需要设置 最大连接数 amount = 10 # 最大连接数 extractorfunc = "request.host" # 统计维度。一般有request.host和client.ip,但也可以设置 request.header.ANY_HEADER 中的任意维度 [backends.backend1.circuitbreaker] # 标记 启用断路器 expression = "NetworkErrorRatio() > 0.5" #DetailDown 断路器打开条件 #ForUp# 断路器打开条件,条件运算符支持 # NetworkErrorRatio() > 0.5: 监控网络故障率大于0.5超过10秒后,为这个前端平滑切换,断路条件匹配 # LatencyAtQuantileMS(50.0) > 50: 监控延迟超过50ms时断路条件匹配 # ResponseCodeRatio(500, 600, 0, 600) > 0.5: 监控返回 HTTP状态码在[500-600]之间的数量/HTTP状态码在[0-600]之间的数量 的比例大于0.5时,断路条件匹配 [backends.backend1.healthcheck] # 标记节点健康检查,只要返回2XX或3XX之外的状态码就会被移除 path = "/health" # 附加在backend的url之后的路径 interval = "10s" #检查频率,默认30s hostname = "myhost.com" #? 设置请求的host,放到请求头的host中? scheme = "http" # scheme设置,http或https port = 8080 # 改变下端口以防止端口冲突 [backends.backend1.healthcheck.headers] # 标记 需要设置其他请求头信息 My-Custom-Header = "foo" My-Header = "bar" [backends.backend1.servers.server1] # 标记 服务器 url = "http://172.17.0.2:80" weight = 10 # 服务器权重 [backends.backend1.servers.server2] url = "http://172.17.0.3:80" weight = 1 # 启用buffer,以快速处理大数据请求,一般用不着。会将整个请求读入内存,如果请求大于配置量,拒绝超出部分的数据 # 详细参:https://docs.traefik.io/configuration/commons/之buffer章节 [backends.backend1.buffering] maxRequestBodyBytes = 10485760 memRequestBodyBytes = 2097152 maxResponseBodyBytes = 10485760 memResponseBodyBytes = 2097152 retryExpression = "IsNetworkError() && Attempts() <= 2" ##################### website ###################################### [backends] [backends.website] # 定义website后端 [backends.website.servers.website] url = "https://1.2.3.4" [backends.error] # 定义error后端 [backends.error.servers.error] url = "http://2.3.4.5"
- basic
# 全局配置 # traefik.toml ########################################## 子文件############################################# #! 注意,应该放后面,本文档放这儿是为了引起注意 [file] watch = true # 开启traefik对文件内容的变化监控 filename = "rules.toml" # 可以组合子文件 #################################### basic ############################################ # 启动Grace之前接受请求的时间 #? Grace是什么? # Optional # Default: 0 # # requestAcceptGraceTimeout = "10s" # 设置超时的时间(以秒为单位) # 在热更新期间给还在活动中的请求来完成当前任务的超时时间 # 不会接收新请求了 # 可选 # 默认: 10 # graceTimeOut = 10 # 开启调试模式 # # 可选 # 默认: false # # debug = true # 定期检查是否有新版本产生 # # 可选 # 默认: true # # checkNewVersion = false # Traefik 日志文件,有更详细配置方式(配置方法不同),参:https://docs.traefik.io/configuration/logs/ # 如果没有定义, 日志文件输出到 stdout # # 可选 # # traefikLogsFile = "log/traefik.log" # 日志文件路径,有更详细配置方式(配置方法不同),参:https://docs.traefik.io/configuration/logs/ # # 可选 # # accessLogsFile = "log/access.log" # 日志等级 # # 可选 # 默认: "ERROR" # 接受以下值,按照严重程度排序: "DEBUG", "INFO", "WARN", "ERROR", "FATAL", "PANIC" # 日志等级在配置值或配置值以上的信息将被日志记录。 # # logLevel = "ERROR" # 日志显示方式是 "json" or "common". # # 可选 # Default: "common" # # format = "common" #? 后端节流持续时间:在应用新配置之前,提供者的2个事件之间的最短持续时间(以秒为单位) # 如果在短时间内发送多个事件,它可以避免不必要的重新加载。 # # 可选 # 默认: "2" # # ProvidersThrottleDuration = "5" # 为每个host控制最大空闲连接(keep-alive)。如果设置为0,那么将会使用 # Go语言基础库net/http中的DefaultMaxIdleConnsPerHost。 # 如果造成 'too many open files' 错误,你也可以增加这个值或改变 `ulimit`。 # # 可选 # 默认: 200 # # MaxIdleConnsPerHost = 200 # 如果设置为 true, 无效的 SSL 证书也会被后端所接受。If set to true invalid SSL certificates are accepted for backends. # 注意:这会禁用中间人攻击(man-in-the-middle attacks)监测,所以只能被用在安全的后端网络中。 # # 可选 # 默认: false # # InsecureSkipVerify = true # 被前端所使用的入口点未指定任何入口点。 # 任何前端可以指定它自己的入口点。 # # 可选 # 默认: ["http"] # # defaultEntryPoints = ["http", "https"] # Register Certificates in the rootCA. # # Optional # Default: [] # # rootCAs = [ "/mycert.cert" ] # 允许使用0作为 endback server权重 # - false: a weight 0 means internally a weight of 1. # - true: a weight 0 means internally a weight of 0 (a server with a weight of 0 is removed from the available servers). # # Optional # Default: false # # AllowMinWeightZero = true #? 训练Slash? 为true时训练,否则不训练 # Optional # Default: false # # keepTrailingSlash = true ############################################# 约束 ######################################### # 约束含义:只使用url匹配哪些特征的backend # 定义约束条件 # # 可选 # # 简单约束匹配的条件 # constraints = ["tag==api"] # # 简单约束不匹配的条件 # constraints = ["tag!=api"] # # 约束全局匹配条件 # constraints = ["tag==us-*"] # # 后端指定约束条件 # [consulCatalog] # endpoint = 127.0.0.1:8500 # constraints = ["tag==api"] # # 多个约束条件 # - "tag==" 必需匹配至少一个标签 # - "tag!=" 必需不匹配任何标签 # constraints = ["tag!=us-*", "tag!=asia-*"] # [consulCatalog] # endpoint = 127.0.0.1:8500 # constraints = ["tag==api", "tag!=v*-beta"] ############################################### 重试配置 ######################################## # 当网络故障时启用重发请求 # # 可选 # [retry] # 重试次数 # # 可选 # 默认: (后端服务器数量) -1 # # attempts = 3 #################################### ACME (Let's Encrypt) 配置 ################# #略 参:https://docs.traefik.cn/toml 对应章节 #################################### 超时设置 #################################### ## 响应超时设置 [respondingTimeouts] # readTimeout is the maximum duration for reading the entire request, including the body. # 获取整个请求(包括body)的最大时间 # Optional # Default: "0s" # readTimeout = "5s" # writeTimeout is the maximum duration before timing out writes of the response. # 写响应的最大时间 # Optional # Default: "0s" # writeTimeout = "5s" # idleTimeout is the maximum duration an idle (keep-alive) connection will remain idle before closing itself. # idle 状态的最大持续时间 # Optional # Default: "180s" # idleTimeout = "360s" ## 跳转的超时设置 [forwardingTimeouts] # dialTimeout is the amount of time to wait until a connection to a backend server can be established. # 和backend建立连接的最大等待时间 # Optional # Default: "30s" # dialTimeout = "30s" # responseHeaderTimeout is the amount of time to wait for a server's response headers after fully writing the request (including its body, if any). # 写请求完成后至接收到response header之间的最大持续时间 # Optional # Default: "0s" # responseHeaderTimeout = "0s" ################################## Host Resolver ########################################### [hostResolver] # cnameFlattening is a trigger to flatten request host, assuming it is a CNAME record # 扁平化请求来源的host,如果是一个cname记录 # Optional # Default : false cnameFlattening = true # resolvConf is dns resolving configuration file, the default is /etc/resolv.conf # 指定DNS配置文件 # Optional # Default : "/etc/resolv.conf" # resolvConf = "/etc/resolv.conf" # resolvDepth is the maximum CNAME recursive lookup #? 最大cname查找深度 # Optional # Default : 5 # resolvDepth = 5