如何验证 Cloud CDN 响应是否缓存命中(或来自 CDN)

Ahm*_*gle 10 google-cloud-cdn google-cloud-networking google-cloud-load-balancer

我在启用了 Cloud CDN 的 Google Cloud HTTP(S) 负载均衡器后面有一个后端 GCS 存储桶。

我试图根据响应标题回答这些问题:

  • 此响应是由 CDN 提供的吗
  • 如果是的话,哪个位置/地区
  • 这是缓存命中/未命中吗

这是响应标头。根据cache-control,理论上,这个应该被缓存。但是,我没有看到可以验证 CDN 正常工作的迹象。同样,所有其他标头x-goog-*Server: UploadServer似乎都来自 GCS 服务器,而不是 CDN。

accept-ranges: bytes
age: 551
alt-svc: clear
cache-control: public, max-age=3600
content-length: 298303
content-type: image/jpeg
date: Wed, 05 Aug 2020 23:07:33 GMT
etag: "f0b6c60f635c784dd7f34ab9c1527867"
expires: Thu, 06 Aug 2020 00:07:33 GMT
last-modified: Wed, 05 Aug 2020 23:07:16 GMT
server: UploadServer
status: 200
X-DNS-Prefetch-Control: off
x-goog-generation: 1596668836233926
x-goog-hash: crc32c=rD4sZw==
x-goog-hash: md5=8LbGD2NceE3X80q5wVJ4Zw==
x-goog-metageneration: 1
x-goog-storage-class: STANDARD
x-goog-stored-content-encoding: identity
x-goog-stored-content-length: 298303
x-guploader-uploadid: AAANsUktJ98kPCHjiR2oBi6N-[...]
Run Code Online (Sandbox Code Playgroud)

例如,Cloudflare 提供以下响应标头:

  • 请求在哪里得到满足: cf-ray: 5be4505beb76bca2-SEA
  • 缓存状态是什么:cf-cache-status: REVALIDATED
  • 我的请求是直接到达 CDN 还是我的后端server: cloudflare

eli*_*rar 13

{cdn_cache_status}现在您可以在响应中设置一个新变量: https://cloud.google.com/load-balancing/docs/custom-headers#variables

\n

使用 gcloud SDK v309.0.0 或更高版本:

\n
\xe2\x9e\x9c  gcloud beta compute backend-services update my-backend --global \\\n--enable-cdn \\\n--custom-response-header=\'Cache-Status: {cdn_cache_status}\' \\\n--custom-response-header=\'Cache-ID: {cdn_cache_id}\'\n
Run Code Online (Sandbox Code Playgroud)\n

输出示例:

\n
< HTTP/2 200\n< content-type: text/plain; charset=utf-8\n< date: Mon, 14 Sep 2020 21:40:05 GMT\n< server: Google Frontend\n< content-length: 1098\n< via: 1.1 google\n< cache-control: public, max-age=10\n< age: 2\n< x-frame-options: DENY\n< cache-status: hit\n
Run Code Online (Sandbox Code Playgroud)\n