您可能不应直接在生产域名上开始,而应在测试或预发布域名上创建负载均衡器。根据基础设施设置,这可能需要对 monitor 和 pool 进行临时更改。
从测试域名开始可以让您在路由生产流量之前验证一切运行正常。
您可以在负载均衡器工作流中或 Monitors(监视器) 选项卡内创建 monitor:
-
前往 Load Balancing(负载均衡)。
-
选择 Monitors(监视器) 选项卡。
-
选择 Create monitor(创建监视器)。
添加以下信息:
- Type(类型):健康 monitor 使用的协议
- 非 Enterprise 客户:选择 HTTP、HTTPS 或 TCP。
- Enterprise 客户:选择 HTTP、HTTPS、TCP、UDP ICMP、ICMP Ping 或 SMTP。
- Path(路径):运行健康 monitor 请求的 endpoint 路径
- Port(端口):健康 monitor 的目标端口
- Type(类型):健康 monitor 使用的协议
对于其他设置,选择 Advanced health monitor settings(高级运行状况监视器设置):
- Interval(时间间隔):
- 增加默认值可以缩短故障转移时间,但也可能增加 endpoint 负载。
- 最短间隔(秒)为 60(Pro)、15(Business)和 10(Enterprise)。
- Timeout(超时) 和 Retries(重试):
- 如果健康 monitor 请求超过 Timeout(超时) 指定的时长(且超过 Retries(重试) 指定的次数),将返回不健康。
- Expected Code(s)(预期状态码):预期的 HTTP 响应码,可单独列出(
200、302)或作为范围(例如,输入2xx将涵盖200范围内的所有响应码)。 - Response Body(响应正文):
- 在响应正文中查找不区分大小写的子字符串。
- 确保该值相对静态,且位于 HTML 页面前 10 KB 内。
- Simulate Zone(模拟区域):
- 建议使用负载均衡器所在的同一 zone。
- 更改健康 monitor 请求的出站 zone 设置,以确保与 Authenticated Origin Pulls (mTLS)、Argo Smart Routing、Bring your own CA (mTLS)、Dedicated CDN Egress IPs 和 HTTP/2 to Origin 等功能兼容。
- Follow Redirects(跟随重定向):
- 健康 monitor 请求会跟随重定向到最终 endpoint,而不是将
301或302码报告为不健康。
- 健康 monitor 请求会跟随重定向到最终 endpoint,而不是将
- Configure Request Header(s)(配置请求标头):
- 如果您的 endpoint 期望特定的传入标头,此选项很有用。
- Header(标头):
- 健康 monitor 中发送的 HTTP 请求标头。建议默认设置 Host 标头。User-Agent 标头无法覆盖。此参数仅对 HTTP 和 HTTPS monitor 有效。
- Interval(时间间隔):
-
选择 Save(保存)。
确保防火墙或 Web 服务器不会阻止或限速您配置的健康 monitor,或与 Cloudflare IP 地址 ↗ 关联的请求。
每个健康 monitor 的 HTTP user-agent 为 "Mozilla/5.0 (compatible; Cloudflare-Traffic-Manager/1.0; +https://www.cloudflare.com/traffic-manager/; pool-id: $poolid)",其中 $poolid 是关联 pool 的前 16 个字符。
有关 monitor 属性的完整列表,请参阅 Create Monitor。如需 API 身份验证帮助,请参阅 Cloudflare API 文档。
Required API token permissions
At least one of the following token permissions is required:Load Balancing: Monitors and Pools Write
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/load_balancers/monitors" \
--request POST \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"type": "https",
"description": "Login page monitor",
"method": "GET",
"path": "/health",
"header": {
"Host": [
"example.com"
],
"X-App-ID": [
"abc123"
]
},
"port": 8080,
"timeout": 3,
"retries": 0,
"interval": 90,
"expected_body": "alive",
"expected_codes": "2xx",
"follow_redirects": true,
"allow_insecure": true,
"consecutive_up": 3,
"consecutive_down": 2,
"probe_zone": "example.com"
}'响应包含新 monitor 的完整定义。
{
"success": true,
"errors": [],
"messages": [],
"result": {
"id": ":monitor-id",
"created_on": "2021-01-01T05:20:00.12345Z",
"modified_on": "2021-01-01T05:20:00.12345Z",
"type": "https",
"description": "Login page monitor",
"method": "GET",
"path": "/health",
"header": {
"Host": [
"example.com"
],
"X-App-ID": [
"abc123"
]
},
"port": 8080,
"timeout": 3,
"retries": 0,
"interval": 90,
"expected_body": "alive",
"expected_codes": "2xx",
"follow_redirects": true,
"allow_insecure": true,
"consecutive_up": 3,
"consecutive_down": 2,
"probe_zone": "example.com"
}
}确保防火墙或 Web 服务器不会阻止或限速您配置的健康 monitor,或与 Cloudflare IP 地址 ↗ 关联的请求。
每个健康 monitor 的 HTTP user-agent 为 "Mozilla/5.0 (compatible; Cloudflare-Traffic-Manager/1.0; +https://www.cloudflare.com/traffic-manager/; pool-id: $poolid)",其中 $poolid 是关联 pool 的前 16 个字符。
示例监控配置
| 字段 | 值 |
|---|---|
| Type | HTTP |
| Path | / |
| Port | 80 |
| Interval | 60 |
| Method | GET |
| Timeout | 5 seconds |
| Retries | 2 |
| Expected Code(s) | 200 |