IPAllowList
ipAllowList 根据客户端 IP 接受或拒绝请求。
配置示例¶
# Accepts request from defined IP
# and rejects other requests with a 404 rather than a 403
http:
middlewares:
test-ipallowlist:
ipAllowList:
sourceRange:
- "127.0.0.1/32"
- "192.168.1.7"
rejectStatusCode: 404# Accepts request from defined IP
# and rejects other requests with a 404 rather than a 403
[http.middlewares]
[http.middlewares.test-ipallowlist.ipAllowList]
sourceRange = ["127.0.0.1/32", "192.168.1.7"]
rejectStatusCode = 404# Accepts request from defined IP
# and rejects other requests with a 404 rather than a 403
labels:
- "traefik.http.middlewares.test-ipallowlist.ipallowlist.sourcerange=127.0.0.1/32, 192.168.1.7"
- "traefik.http.middlewares.test-ipallowlist.ipallowlist.rejectstatuscode=404"// Accepts request from defined IP
// and rejects other requests with a 404 rather than a 403
{
"Tags" : [
"traefik.http.middlewares.test-ipallowlist.ipallowlist.sourcerange=127.0.0.1/32, 192.168.1.7",
"traefik.http.middlewares.test-ipallowlist.ipallowlist.rejectstatuscode=404"
]
}apiVersion: traefik.io/v1alpha1
kind: Middleware
metadata:
name: test-ipallowlist
spec:
ipAllowList:
sourceRange:
- 127.0.0.1/32
- 192.168.1.7
rejectStatusCode: 404配置选项¶
| 字段 | 说明 | 默认值 | 必填 |
|---|---|---|---|
sourceRange |
允许的 IP 列表(或使用 CIDR 表示法定义的允许 IP 范围)。 | 是 | |
ipStrategy.depth |
从右侧开始,在 X-Forwarded-For 头中选择 IP 的深度位置。0 表示不使用深度。 如果大于 X-Forwarded-For 中的 IP 总数,客户端 IP 为空。如果大于 0,则不计算 excludedIPs 选项。更多信息参阅下文的 ipStrategy 和 depth。 |
0 | 否 |
ipStrategy.excludedIPs |
允许 Traefik 扫描 X-Forwarded-For 头,选取第一个不在此列表中的 IP。如果指定了 depth,则忽略 excludedIPs。更多信息参阅下文的 ipStrategy 和 excludedIPs。 |
否 | |
ipStrategy.ipv6Subnet |
如果提供 ipv6Subnet,且选中的 IP 为 IPv6,则将该 IP 转换为其所属子网的第一个 IP。更多信息参阅下文的 ipStrategy.ipv6Subnet 和 excludedIPs。 |
否 | |
rejectStatusCode |
定义拒绝请求时使用的 HTTP 状态码。 | 403 |
否 |
ipStrategy IP 策略¶
ipStrategy 选项定义 depth 和 excludedIPs 两个参数,用于配置 Traefik 如何确定客户端 IP。
如果未设置策略,默认使用请求中的远程地址与 sourceRange 匹配。
允许列表作为中间件,在实际向后端代理请求之前执行。
此外,前一网络跳点仅在代理过程的最后阶段才会追加到 X-Forwarded-For 中,此时请求已经通过允许列表检查。
因此,在允许列表检查期间,前一网络跳点尚未出现在 X-Forwarded-For 中,无法使用它与 sourceRange 匹配。
ipStrategy.depth 深度¶
depth 选项让 Traefik 使用 X-Forwarded-For 头,并取出位于 depth 位置的 IP(从右侧开始)。
- 如果
depth大于X-Forwarded-For中的 IP 总数,则客户端 IP 为空。 - 如果
depth小于或等于 0,则忽略此值。
如果提供 ipStrategy.ipv6Subnet,且选中的 IP 为 IPv6,则将该 IP 转换为其所属子网的第一个 IP。
ipStrategy.ipv6Subnet IPv6 子网¶
此策略仅适用于 Depth 和 RemoteAddr 策略。
如果提供 ipv6Subnet,且选中的 IP 为 IPv6,则将该 IP 转换为其所属子网的第一个 IP。
这可用于将 IPv6 地址按子网分组,防止通过获取新的 IPv6 地址来绕过此中间件。
- 如果
ipv6Subnet≤ 0 或 > 128,中间件创建失败。有效范围:1–128。
ipv6Subnet 示例¶
提供 ipv6Subnet 时,IP 按以下方式转换。
| IP | ipv6Subnet | clientIP(客户端 IP) |
|---|---|---|
"::abcd:1111:2222:3333" |
64 |
"::0:0:0:0" |
"::abcd:1111:2222:3333" |
80 |
"::abcd:0:0:0:0" |
"::abcd:1111:2222:3333" |
96 |
"::abcd:1111:0:0:0" |
Depth 与 X-Forwarded-For 示例¶
如果 depth 设置为 2,且请求的 X-Forwarded-For 头为 "10.0.0.1,11.0.0.1,12.0.0.1,13.0.0.1",则“真实”客户端 IP 为 "10.0.0.1"(深度为 4),但用作判定标准的 IP 是 "12.0.0.1"(depth=2)。
X-Forwarded-For |
depth | clientIP(客户端 IP) |
|---|---|---|
"10.0.0.1,11.0.0.1,12.0.0.1,13.0.0.1" |
1 |
"13.0.0.1" |
"10.0.0.1,11.0.0.1,12.0.0.1,13.0.0.1" |
3 |
"11.0.0.1" |
"10.0.0.1,11.0.0.1,12.0.0.1,13.0.0.1" |
5 |
"" |
ExcludedIPs 与 X-Forwarded-For 示例¶
X-Forwarded-For |
excludedIPs | clientIP(客户端 IP) |
|---|---|---|
"10.0.0.1,11.0.0.1,12.0.0.1,13.0.0.1" |
"12.0.0.1,13.0.0.1" |
"11.0.0.1" |
"10.0.0.1,11.0.0.1,12.0.0.1,13.0.0.1" |
"15.0.0.1,13.0.0.1" |
"12.0.0.1" |
"10.0.0.1,11.0.0.1,12.0.0.1,13.0.0.1" |
"10.0.0.1,13.0.0.1" |
"12.0.0.1" |
"10.0.0.1,11.0.0.1,12.0.0.1,13.0.0.1" |
"15.0.0.1,16.0.0.1" |
"13.0.0.1" |
"10.0.0.1,11.0.0.1" |
"10.0.0.1,11.0.0.1" |
"" |