Configure and check CDN caching settings

 

Set caching parameters

Check Cache Settings

 

You can set the period of caching content on CDN servers in the Control Panel. 

After the cache expiration time is over CDN servers send a request to the origin again to compare the HTTP Etag header.

  • If the header values do not match, the CDN caches a new file for the time specified for caching the content in the CDN cache.
  • If the header values match, the CDN will return to the end-users the same file for another Cache Expiry period.

 

Set caching parameters

The CDN controlled option sets сache expiration time for CDN servers.

It has two options:

  • CDN controlled
  • Origin controlled

CDN controlled

CDN servers request content from the origin server and cache it for the time specified in the CDN controlled option.

_______.png

The setting has two parameters:

  • Cache expiry
  • Advanced caching rules

Use the Cache expiry parameter to specify the caching time for a response with codes 200, 206, 301, 302. Responses with 4xx, 5xx will not be cached.

Advanced caching rules values have higher priority than the Cache expiry values

For example,

In the options settings, you selected CDN controlled and set 4 days for the Cache expiry parameter.

In the Advanced caching rules you added 3 rules:

  • Requests with the response code 200 expire in 10 minutes
  • Requests with the response code 404 expire in 1 minute

____________CDN____________.png

As a result, requests will be cached like this:

  • Requests with response code 200 will be cached for 10 minutes
  • Requests with response code 404 will be cached for 1 minute
  • Requests with response code 206, 301, 302 will be cached for 4 days
  • 4xx (except 404), 5xx will not be cached

Note! Regardless of the option settings, the content is purged from the CDN servers cache after 36 hours, if it is not requested by end-users.

 

Origin controlled

CDN servers, when requesting content from the origin server, cache content for the time specified as the Cache-Control header value, configured on the origin.

____________.png

If the origin server does not have a Cache-Control header, the CDN caches content for the time specified in the Default cache expiry parameter. 

Note! Regardless of the option settings, the content is purged from the CDN servers cache after 36 hours, if it is not requested by end-users.

 

How to configure Origin

By defaultwe inherit all HTTP headers set on the origin.

CDN content is stored on servers according to the Cache-Control header directive (unless otherwise specified in the personal account)

In order CDN to cache files from your origin in a proper way, add to the .htacess or nginx.conf. file on your server the Cache-Control header with public and max-age directives.

 

An example for Apache

<ifModule mod_headers.c> 

 <FilesMatch ".(gif|ico)$"> 

     Header set Cache-Control "max-age=31536000, public" 

 </FilesMatch> 

 <FilesMatch ".(js)$"> 

     Header set Cache-Control "max-age=31536000,  

             public" 

 </FilesMatch> 

 </ifModule> 

An example for Nginx

server { 

   #... 

   location ~* .(?:ico|css|js|gif|jpe?g|png)$ { 

     add_header Cache-Control "max-age=88000,  public"; 

   } 

   #... 

} 

 

Check  settings   

Check if necessary cache headers present when requesting a file integrated with CDN.    Use the description of the headers below.

 

Use the cURL command in a terminal (MacOS) and cmd (WindowsOS) 

 1. In the terminalspecifycurl -I http://cdn.example.ru/js/intlTelInput/css/intlTelInput.css

Where http://cdn.example.ru/js/intlTelInput/css/intlTelInput.css is a link to a file integrated with CDN.

2. You will get an output Pay attention to the Cache-ControlCache, X-Cached-Since, X-ID headers:

HTTP/1.1 200 OK 
Servernginx/1.13.1 
DateFri, 09 Jun 2017 12:54:24 GMT 
Content-Typeimage/jpeg 
Content-Length: 124024 
Connectionkeep-alive 
X-Image-Generated: 29 
X-Image-Meta: 1024x768 
X-Image-Read: 71 
ExpiresWed, 06 Dec 2017 12:51:43 GMT
Access-Control-Allow-Origin: * 
Last-ModifiedSun, 01 Jan 2017 12:00:00 GMT 
Cache-Controlmax-age=315360000, public — cache lifetime in seconds 
Cache: HIT - the file was delivered from the CDN cache. 
X-Cached-Since: 2017-06-09T12:51:43+00:00 — when the file was cached.
X-ID: m9-up-e245 — the CDN server from which the file was delivered. 

3. If you have any suspicions on the content caching, check the settings on the sourceread the Troubleshooting Low Cache hit ratio articleor contact support@gcorelabs.com.

 

Use a browser developer console 

1.Open a browser (e.g. Google Chrome).

2.Open your site.

3.Press F12 (to open a developer console)

4. Choose the Network tab.

5. Refresh the page (press F5). You will get the list of all files downloaded from your site.

6.Find the static file (egjpegpngimgintegrated with the CDN and click on itFor a faster searchuse the filter in the panel left corner. 

7. On the Headers tab on the rightyou will find the headers set on your origin.

____________________.png

8. Analyze them using the description of the important headings below.

Important HTTP Headers 

1. Check from which server (your or CDN) the content was sent is possible by the value of the X-Cache HTTP header:

  • X-Cache: HIT — the file has been delivered from CDN 
  • X-Cache: MISS — the file has been delivered from your origin server 

2. Check from which CDN server the content was sent is possible by the header value:

  • X-ID: [m9-up-e245] - the file has been delivered from the m9-up-e245 CDN-server. 

3. The time when the file was cached on the CDN server can be checked by the value of the X-Cached-Since header:

  • X-Cached-Since: 2017-06-09T12:51:43+00:00 — the time when the file was cached in a CDN node. 

Headers compatibility

Cache-Control Header Parameters Compatible with CDN: 

Cache-ControlMax-Age - sets the lifetime of the file in the cache in seconds.

Cache-ControlPublic - indicates that not only the user's end client (browsercan cache the filebut also proxy servers, CDN serversetc.

Cache-Control Header Parameters Incompatible with CDN: 

Cache-ControlPrivate - the opposite of public directive - indicates that the file cannot be cached by proxies.

Cache-ControlNo-cache - the file is not cached on CDN and each time

will be pulled from the origin.

Was this article helpful?
Recently viewed articles