验证
部署完成后,执行以下检查以确认 CDN 边缘节点正常运行。执行 terraform apply 后,请等待 2-3 分钟,待 cloud-init 完成 NGINX 安装。
验证 NGINX 是否正在运行并正常响应:
curl -s "http://<PUBLIC_IP>/health" | jq .预期输出:
{ "status": "healthy", "component": "cdn-edge", "engine": "nginx", "vendor_profiles": [ "akamai", "cloudflare", "cloudfront", "fastly", "azure-front-door" ]}如果使用 Terraform 输出:
curl -s "$(terraform output -raw health_check_url)" | jq .缓存未命中(首次请求)
Section titled “缓存未命中(首次请求)”对任意路径的首次请求将发生缓存未命中——边缘节点从源站获取内容:
curl -I "http://<PUBLIC_IP>/"在响应中查找以下响应头:
X-Cache-Status: MISSX-CDN-Edge: cdn-simulatorMISS 表示内容不在缓存中,已从源站服务器获取。
缓存命中(后续请求)
Section titled “缓存命中(后续请求)”立即重复相同的请求:
curl -I "http://<PUBLIC_IP>/"预期响应头:
X-Cache-Status: HITX-CDN-Edge: cdn-simulatorHIT 确认响应由 NGINX 磁盘缓存提供,未与源站联系。
缓存目录检查
Section titled “缓存目录检查”通过 SSH 登录虚拟机,验证磁盘上是否存在已缓存的内容:
ssh azureuser@<PUBLIC_IP>
# 检查缓存目录是否有内容sudo find /var/cache/nginx/cdn -type f | head -20
# 检查缓存大小sudo du -sh /var/cache/nginx/cdnNGINX 状态
Section titled “NGINX 状态”验证 NGINX 是否正在运行,且配置有效:
ssh azureuser@<PUBLIC_IP>
# 检查服务状态sudo systemctl status nginx
# 验证配置sudo nginx -t
# 查看活跃连接数(如果已启用 stub_status)curl -s http://localhost/health验证边缘节点是否可以访问源站服务器:
ssh azureuser@<PUBLIC_IP>
# 测试与源站的连接(替换为您的源站 URL)curl -I "https://your-origin.example.com/"如果连接失败,请检查:
- Terraform 配置中的
origin_server变量是否正确 - 源站服务器是否允许来自边缘节点公共 IP 的入站连接
- 虚拟机中的 DNS 解析是否正常(
nslookup your-origin.example.com)
Cloud-Init 进度
Section titled “Cloud-Init 进度”通过 cloud-init 日志监控配置进度:
ssh azureuser@<PUBLIC_IP> "tail -f /var/log/cloud-init-progress.log"预期阶段:[init]、[nic](动态网卡检测)、[complete]。
如果 cloud-init 报告错误:
ssh azureuser@<PUBLIC_IP> "sudo cloud-init status --long"执行完整的请求周期并验证响应链:
# 第一次请求——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厂商响应头验证
Section titled “厂商响应头验证”通过请求 /headers 路径,验证所有 CDN 厂商响应头是否被正确注入(使用 httpbin.org 作为源站时):
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" }}设备检测测试
Section titled “设备检测测试”使用移动端 User-Agent 进行测试,验证设备检测响应头是否发生变化:
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",NGINX 未运行
Section titled “NGINX 未运行”ssh azureuser@<PUBLIC_IP>sudo systemctl status nginxsudo journalctl -u nginx --no-pager -n 50Cloud-init 未完成
Section titled “Cloud-init 未完成”执行 terraform apply 后,cloud-init 需要 2-3 分钟。检查进度:
ssh azureuser@<PUBLIC_IP>sudo cloud-init statussudo tail -f /var/log/cloud-init-output.log缓存始终显示 MISS
Section titled “缓存始终显示 MISS”- 验证源站返回的响应代码是否可缓存(200、301、302)
- 检查源站是否未发送
Cache-Control: no-cache或no-store响应头 - 检查 NGINX 错误日志:
sudo tail -f /var/log/nginx/error.log
80/443 端口连接被拒绝
Section titled “80/443 端口连接被拒绝”- 验证 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"