1
votes

We have a requirement to generate images on the fly and cache using CDN. For this we have configured a backend service with a load balancer enabled cloud CDN. We are using Nginx proxy server. We have added headers specified in the Google cloud CDN docs, but unfortunately it is not caching.

Request:

GET /resize?size=l&url=https://example.com/image.jpeg HTTP/1.1
Host: resize.example.com

Request Headers:

Host: resize.example.com
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:70.0) Gecko/20100101 Firefox/70.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Upgrade-Insecure-Requests: 1

Response headers:

HTTP/1.1 200 OK
Server: nginx/1.17.2
Date: Wed, 15 Jan 2020 15:01:14 GMT
Content-Type: image/jpeg
Content-Length: 62771
cache-control: max-age=86400, public, s-maxage=86400
Via: 1.1 google
1
How are you confirming it’s not caching - via Stackdriver Logging? – elithrar
@elithrar We checked via stackdriver logging, checked cache hit rate in cloud cdn, and also tried requesting via curl(it is not showing the age header). – Adarsh D
Update: you can also use the new Cache Modes to enforce caching on responses that don't set valid cache directives on their own: cloud.google.com/cdn/docs/using-cache-modes – elithrar

1 Answers

1
votes

I suggest you a couple of pages that could help you.

a) Not all HTTP responses are cacheable. Cloud CDN caches only those responses that meet all the requirements in this section. Some of these requirements are specified by RFC 7234, and others are specific to Cloud CDN.

Cacheability for HTTP responses

Responses aren't being cached--Troubleshooting

The following example demonstrates using curl to check the HTTP response headers for http://example.com/style.css:

$ curl -s -D - -o /dev/null http://example.com/style.css HTTP/1.1 200 OK Date: Tue, 16 Feb 2016 12:00:00 GMT Content-Type: text/css Content-Length: 1977 Via: 1.1 google

Although perhaps because of the added response, you may have already read it.