跳到內容

驗證

部署後,執行以下檢查以確認 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>
# Check cache directory has content
sudo find /var/cache/nginx/cdn -type f | head -20
# Check cache size
sudo du -sh /var/cache/nginx/cdn

確認 NGINX 正在執行且設定有效:

Terminal window
ssh azureuser@<PUBLIC_IP>
# Check service status
sudo systemctl status nginx
# Validate configuration
sudo nginx -t
# View active connections (if stub_status is enabled)
curl -s http://localhost/health

確認邊緣節點能夠連接到來源伺服器:

Terminal window
ssh azureuser@<PUBLIC_IP>
# Test connectivity to origin (replace with your origin 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](動態 NIC 偵測)、[complete]

若 cloud-init 回報錯誤:

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

執行完整的請求循環並確認回應鏈:

Terminal window
# First request — MISS (fetches from origin)
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"
# Second request — HIT (served from cache)
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 路徑(使用 httpbin.org 作為來源時)確認所有 CDN 供應商標頭均已注入:

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"