跳转到内容

验证

部署完成后,执行以下检查以确认 CDN 边缘节点正常运行。执行 terraform apply 后,请等待 2-3 分钟,待 cloud-init 完成 NGINX 安装。

验证 NGINX 是否正在运行并正常响应:

Terminal window
curl -s "http://<PUBLIC_IP>/health" | jq .

预期输出:

{
"status": "healthy",
"component": "cdn-edge",
"engine": "nginx",
"vendor_profiles": [
"akamai",
"cloudflare",
"cloudfront",
"fastly",
"azure-front-door"
]
}

如果使用 Terraform 输出:

Terminal window
curl -s "$(terraform output -raw health_check_url)" | jq .

对任意路径的首次请求将发生缓存未命中——边缘节点从源站获取内容:

Terminal window
curl -I "http://<PUBLIC_IP>/"

在响应中查找以下响应头:

X-Cache-Status: MISS
X-CDN-Edge: cdn-simulator

MISS 表示内容不在缓存中,已从源站服务器获取。

立即重复相同的请求:

Terminal window
curl -I "http://<PUBLIC_IP>/"

预期响应头:

X-Cache-Status: HIT
X-CDN-Edge: cdn-simulator

HIT 确认响应由 NGINX 磁盘缓存提供,未与源站联系。

通过 SSH 登录虚拟机,验证磁盘上是否存在已缓存的内容:

Terminal window
ssh azureuser@<PUBLIC_IP>
# 检查缓存目录是否有内容
sudo find /var/cache/nginx/cdn -type f | head -20
# 检查缓存大小
sudo du -sh /var/cache/nginx/cdn

验证 NGINX 是否正在运行,且配置有效:

Terminal window
ssh azureuser@<PUBLIC_IP>
# 检查服务状态
sudo systemctl status nginx
# 验证配置
sudo nginx -t
# 查看活跃连接数(如果已启用 stub_status)
curl -s http://localhost/health

验证边缘节点是否可以访问源站服务器:

Terminal window
ssh azureuser@<PUBLIC_IP>
# 测试与源站的连接(替换为您的源站 URL)
curl -I "https://your-origin.example.com/"

如果连接失败,请检查:

  1. Terraform 配置中的 origin_server 变量是否正确
  2. 源站服务器是否允许来自边缘节点公共 IP 的入站连接
  3. 虚拟机中的 DNS 解析是否正常(nslookup your-origin.example.com

通过 cloud-init 日志监控配置进度:

Terminal window
ssh azureuser@<PUBLIC_IP> "tail -f /var/log/cloud-init-progress.log"

预期阶段:[init][nic](动态网卡检测)、[complete]

如果 cloud-init 报告错误:

Terminal window
ssh azureuser@<PUBLIC_IP> "sudo cloud-init status --long"

执行完整的请求周期并验证响应链:

Terminal window
# 第一次请求——MISS(从源站获取)
echo "=== Request 1 (expect MISS) ==="
curl -s -o /dev/null -w "HTTP %{http_code} | Cache: %{header:X-Cache-Status}\n" "http://<PUBLIC_IP>/test-path"
# 第二次请求——HIT(从缓存提供)
echo "=== Request 2 (expect HIT) ==="
curl -s -o /dev/null -w "HTTP %{http_code} | Cache: %{header:X-Cache-Status}\n" "http://<PUBLIC_IP>/test-path"

预期输出:

=== Request 1 (expect MISS) ===
HTTP 200 | Cache: MISS
=== Request 2 (expect HIT) ===
HTTP 200 | Cache: HIT

通过请求 /headers 路径,验证所有 CDN 厂商响应头是否被正确注入(使用 httpbin.org 作为源站时):

Terminal window
curl -s "http://<PUBLIC_IP>/headers" | python3 -m json.tool

预期响应包含来自所有五个厂商的响应头:

{
"headers": {
"True-Client-Ip": "<YOUR_CLIENT_IP>",
"Cf-Connecting-Ip": "<YOUR_CLIENT_IP>",
"Cf-Ipcountry": "US",
"Cf-Ray": "<request_id>-SJC",
"Cf-Bot-Score": "85",
"Cf-Ja3-Hash": "e7d705a3286e19ea42f587b344ee6865",
"Cloudfront-Viewer-Country": "US",
"Cloudfront-Viewer-City": "San Jose",
"Cloudfront-Is-Desktop-Viewer": "true",
"Cloudfront-Is-Mobile-Viewer": "false",
"Fastly-Client-Ip": "<YOUR_CLIENT_IP>",
"X-Akamai-Edgescape": "georegion=263,country_code=US,...",
"X-Azure-Clientip": "<YOUR_CLIENT_IP>",
"X-Azure-Fdid": "a0a0a0a0-bbbb-cccc-dddd-e1e1e1e1e1e1",
"X-Geo-Country-Code": "US"
}
}

使用移动端 User-Agent 进行测试,验证设备检测响应头是否发生变化:

Terminal window
curl -s "http://<PUBLIC_IP>/headers?device=mobile" \
-H "User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X)" \
| python3 -m json.tool | grep -E "Is-Mobile|Is-Desktop|is_mobile"

预期结果:

"Cloudfront-Is-Desktop-Viewer": "false",
"Cloudfront-Is-Mobile-Viewer": "true",
Terminal window
ssh azureuser@<PUBLIC_IP>
sudo systemctl status nginx
sudo journalctl -u nginx --no-pager -n 50

执行 terraform apply 后,cloud-init 需要 2-3 分钟。检查进度:

Terminal window
ssh azureuser@<PUBLIC_IP>
sudo cloud-init status
sudo tail -f /var/log/cloud-init-output.log
  • 验证源站返回的响应代码是否可缓存(200、301、302)
  • 检查源站是否未发送 Cache-Control: no-cacheno-store 响应头
  • 检查 NGINX 错误日志:sudo tail -f /var/log/nginx/error.log
  • 验证 NSG 规则是否已应用:az network nsg rule list --nsg-name "$(terraform output -raw nsg_name)" --resource-group "$(terraform output -raw resource_group_name)" -o table
  • 验证 NGINX 是否正在监听:ssh azureuser@<PUBLIC_IP> "sudo ss -tlnp | grep nginx"