The configuration of the mandatory cache in the Nginx server and the explanation of the cache priority

  • 2020-05-10 23:30:42
  • OfStack

The nginx proxy is ready and the cache is configured, but the static files css, js, jpg are all cached successful. But the page file is still fetched from the source server.
1. nginx does not cache reasons
By default, whether or not nginx is cached is determined by the nginx cache server and the source server, and the cache server needs to strictly follow the header response of the source server to decide whether or not to cache and how often to cache. header mainly includes the following:


Cache-control : no-cache , no-store

If these two values are present, the nginx cache server will never cache them


Expires : 1980-01-01

If the date appears earlier than the current time, it will not be cached.

2. Solve the non-caching solution
2.1 method 1:
Modify the program or the header response of the source server web program
 
2.2 method 2:
The nginx agent simply adds the following sentence:


proxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie; 

Cache priority
3.1 architecture diagram
client end   < ------------------ >     nginx cache < ------------------ > The source server
After extensive testing, it was found that the expiration order of nginx has a priority. Here are the factors that affect cache expiration:
(1) inactive: configure in the proxy_cache_path configuration entry that a cache will be removed from the cache if not accessed within the time specified by inactive.
(2) Expires in the response header generated in the php page of the source server, the generated statement is:
header("Expires: Fri, 07 Sep 2013 08:05:18 GMT");
(3) the max-age generated by the php page of the source server is as follows:
header("Cache-Control: max-age=60");
(4) configuration item proxy_cache_valid of nginx: configure the cache time of the cache file in nginx cache, if the configuration item is: proxy_cache_valid 200 304 2m; Note that the cache time for cache files with state of 200 and 304 is 2 minutes. When the cache file is accessed after 2 minutes, the file will expire and the data will be retrieved from the source server.
3.2 next, the following is a description of the conflict between the expires configuration items of the source server and nginx cache configuration items of expires. The scenario is as follows
(1) the source server has php file ta1.php contents are as follows:


<?php
header("Expires: Fri, 07 Sep 2013 08:05:18 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: max-age=60");
echo "ta1";
?>

(2) the configuration information on nginx cache server is as follows:


 ... .
proxy_cache_path /data0/proxy_cache_dir levels=1:2  keys_zone=cache_one:200m inactive=5s max_size=30g;
 ... ..
 
location ~ .*\.(php|jsp|cgi)$
{
  proxy_read_timeout 10s;
  proxy_connect_timeout 10s;
  proxy_set_header Host $host;
  proxy_cache_use_stale updating;
  proxy_cache_key $host$uri$is_args$args;
  proxy_cache cache_one;
  #proxy_ignore_headers "Cache-Control";
  #proxy_hide_header "Cache-Control";
  #proxy_ignore_headers "Expires";
  #proxy_hide_header "Expires";
  proxy_hide_header "Set-Cookie";
  proxy_ignore_headers "Set-Cookie";
  #add_header Cache-Control max-age=60;
  add_header X-Cache '$upstream_cache_status from $server_addr';
  proxy_cache_valid 200 304 2m;
  #proxy_cache_valid any 0m;
  proxy_pass http://backend_server;
  expires 30s;
}
 ..................... .

As can be seen from the above two items, the configuration of expires in nginx cache server is 30s, and the value of expires directly determines the values of max-age and expires seen on the browser side. max-age is 60, expires is Fri, 07 Sep 2013 08:05:18 GMT in the response header set in the source server broken code. This is a conflict between the Settings of the source server and nginx-cache, so how should the two properties be set?
At this time, the values of max-age and expires of client end are set according to the configuration item expires in nginx cache, that is:


Expires Fri, 07 Sep 2012 08:59:16 GMT
Cache-Controlmax-age=30

The values of max-age and expire in the cache of nginx cache are set according to the code on the source server. That is:


Expires Fri, 07 Sep 2013 08:05:18 GMT
Cache-Controlmax-age=60

Now let's get down to business:
3.3 after a lot of tests, it is found that the priority of the factors that play a role in the expiration and cleaning of the cache is from high to low once:
inactive configuration items, Expires for source server Settings, Max-Age for source server Settings, proxy_cache_valid configuration items
These priorities are illustrated with a few examples
Example 1:
Server-side php code:


<?php
header("Expires: Fri, 07 Sep 2012 08:03:18 GMT");// Is actually 3 Minutes later 
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: max-age=180");//2 minutes 
//header("Cache-Control: post-check=0, pre-check=0", false);
echo "ta1";
?>

nginx cache configuration item


inactive 4m//4 minutes 
proxy_cache_valid 1m//1 minutes 

Phenomenon: page ta1 was visited for the first time. Results of visits at various times after php:

1 minute later: HIT// this means valid is not working After 2 minutes: HIT// this indicates that the max-age set up by the source server is not working After 3 minutes: MISS// this indicates that the Expires set up by the source server is working 4 minutes later: MISS// this means inactive is working

Example 2:

Server-side php code:


<?php
header("Expires: Fri, 07 Sep 2012 08:03:18 GMT");//3 Minutes later 
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: max-age=180");//2 minutes 
//header("Cache-Control: post-check=0, pre-check=0", false);
echo "ta1";
?>

nginx cache configuration item


Expires : 1980-01-01
0

Phenomenon: page ta1 was visited for the first time. After php, the result of the visit at each time:

5 seconds later access: HIT 10 seconds later access: MISS 15 seconds later access: HIT After 20 seconds access :MISS

By combining instance 1 and instance 2: if inactive is already set, the cache expiration time is based on the value set by inactive

Example 3:
Server-side php code:


Expires : 1980-01-01
1

nginx cache configuration item


Expires : 1980-01-01
2

Phenomenon: page ta1 was visited for the first time.
Access once every 1 second: MISS// this indicates that the Expires set on the source side blocks the valide set on nginx and max-age set on the source side
Example 4:
Server-side php code:


<?php
header("Expires: Fri, 07 Sep 2012 08:03:18 GMT");//3 Minutes later 
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: max-age=120");//2 minutes 
//header("Cache-Control: post-check=0, pre-check=0", false);
echo "ta1";
?>

nginx cache configuration item


Expires : 1980-01-01
4

Phenomenon: page ta1 was visited for the first time. After php, the result of the visit at each time:

After 1 minute:     HIT// this indicates that valid is not working because the Expires set by the source server blocks the effect of valid After 2 minutes:     HIT// this means that the max-age set by the source server is not working because the Expires set by the source server blocks max-age After 3 minutes:       MISS// this indicates that expires set on the server is working

The phenomenon of instance 2 and instance 3 shows that: if inactive setting is relatively large, before the expiration of inactive, if valid, expires set on the server side, max-age set on the server side have been set, expires set on the server side shall prevail.

Example 5:
Server-side php code:


Expires : 1980-01-01
5

nginx cache configuration item


Expires : 1980-01-01
6

Phenomenon: after the first visit to the page ta1.php, the result of the visit at each time:

1 minute later,     HIT // this means that the effect of valid has been blocked by max-age on the server side 2 minutes later,     MISS// max-age set on the server comes into effect

Example 6:

Server-side php code:


Expires : 1980-01-01
7

nginx cache configuration item


inactive 4m//4 minutes 
# The next two lines remove the server-side configuration Expires Impact of response headers 
proxy_ignore_headers "Expires";
proxy_hide_header "Expires";
proxy_cache_valid 2m//2 minutes 

Phenomenon: the first visit to the page ta1. After php, the visit result of each time:

After 50 seconds:     MISS// this indicates that the server-side configuration of max-age is in effect After 1 minute:     HIT// After 100 seconds:     MISS// this indicates that max-age set on the server is in effect

This is illustrated by the phenomenon of instance 5 and 6: if inactive is set to be large and the effect of Expires on the cache on the server side is cancelled in the nginx configuration file. In the case that proxy_cache_valid and max-age response header fields are set at the same time, the cache expiration is processed based on the value max-age set on the server side.

3.4 in summary:
(1) when Expires on the source server side, max-age on the source server side, proxy_cache_valid on cahe on the source server side are set at the same time, the cache expiration is processed according to the Expires value set on the source server side
(2) if relevant configuration items are configured in nginx, the effect of the original server-side Expires on the cache is cancelled. In the case of setting Expires on the source server-side, max-age on the source server-side and nginx cache_valid on the source server-side, the value of max-age on the source server-side is taken as the standard for cache expiration
(3) if the effect of source server side Expires and source server side max-age on the cache is cancelled at the same time, the cache expiration is processed according to the value set by proxy_cache_valid
(4) the value of     Inactive is not affected by the above three factors, that is, after the first page request, every time after inactvie specified time, the corresponding cache cleaning should be forced. So inactive has the highest priority.
(5) so the priority of cache expiration impact is sorted as follows: inactvie, source server Expires, source server max-age, proxy_cache_valid



Related articles: