Nginx cache execution sequence (priority)

Source: Internet
Author: User

Architecture diagram
Client <------> nginx cache <------> Source Server

A large number of tests have found that the expiration order of nginx has a priority. The following describes the factors that affect cache expiration:

(1) inactive: the configuration in proxy_cache_path indicates that if a cache is not accessed within the time specified by inactive, it will be deleted from the cache.
(2) Expires in the response header generated on 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 on the php page of the source server. The generated statement is:
Header ("Cache-Control: max-age = 60 & Prime ;);
(4) nginx configuration item proxy_cache_valid: configure the cache time of the cache file in nginx cache. If the configuration item is: proxy_cache_valid 200 304 2 m; it indicates that the cache time for cached files in the status of 200 and 304 is 2 minutes. When you access the cached file two minutes later, the file will expire and the data will be retrieved from the source server.

Note that the expires configuration items of the source server conflict with the expires configuration items of the nginx cache. The scenario is as follows:

(1) the source server has the following php file ta1.php:

<? 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 the nginx cache server is as follows:

.......
Proxy_cache_path/data0/proxy_cache_dir levels = keys_zone = cache_one: 200 m inactive = 5S max_size = 30g;
........
 
Location ~ . *. (Php | jsp | cgi) $
{
Proxy_read_timeout 10 s;
Proxy_connect_timeout 10 s;
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 2 m;
# Proxy_cache_valid any 0 m;
Proxy_pass http:
// Backend_server;
Expires 30 s;
}
.............
The above two items show that the expires configuration in the nginx cache server is 30 s. The expires value directly determines the max-age and expires values seen on the browser side. The max-age in the response header set in the code for source server disconnection is 60, expires is Fri, 07 Sep 2013 08:05:18 GMT. This is because the nginx-cache settings on the source server conflict. How can we set these two attributes?

In this case, the value of max-age and expires on the client is set according to the expires configuration item in the nginx cache, that is:

Expires Fri, 07 Sep 2012 08:59:16 GMT
Cache-Controlmax-age = 30
The max-age and expire values in the 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 started:

After a large number of tests, we found that the priority of the factors that work for cache expiration and clearing is from high to low:
Inactive configuration items, Expires set for the source server, Max-Age set for the source server, and proxy_cache_valid configuration items
The following describes the priorities through several instances.

Instance 1:
Php code on the server:

<? Php
Header ("Expires: Fri, 07 Sep 2012 08:03:18 GMT"); // 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 items
Inactive 4 m // 4 minutes
Proxy_cache_valid 1 m // 1 minute
Symptom: after the first visit to the ta1.php page, the access results at various times are as follows:
1 minute later: HIT // This indicates that valid does not work
2 minutes later: HIT // indicates that the max-age set by the source server does not work.
3 minutes later: MISS // This indicates that the Expires set by the source server takes effect.
Four minutes later: MISS // This indicates that inactive takes effect.

Instance 2:

Php code on the server:

<? 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 items
Inactive 10 s // 10 seconds
Proxy_cache_valid 1 m // 1 minute
Symptom: after the first visit to the ta1.php page, the access results at various times are as follows:
Access in 5 seconds: HIT
Access in 10 seconds: MISS
Access in 15 seconds: HIT
Access in 20 seconds: MISS
Comprehensive analysis of instance 1 and instance 2: If inactive has been set, the cache expiration time is subject to the inactive value.

Instance 3:

Php code on the server:

<? Php
Header ("Expires: Fri, 07 Sep 1977 08:03:18 GMT"); // expire directly
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 items
Inactive 4 m // 4 minutes
Proxy_cache_valid 1 m // 1 minute
Symptom: after the first visit to the ta1.php page, the access results at various times are as follows:
Access once every second: MISS // This indicates that the Expires set on the source server shields the function of the nginx valide and the max-age set on the source server.

Instance 4:
Php code on the server:

<? 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 items
Inactive 4 m // 4 minutes
Proxy_cache_valid 1 m // 1 minute

Symptom: after the first visit to the ta1.php page, the access results at various times are as follows:
1 minute later: HIT // This indicates that valid does not work, because the Expires set by the source server blocks the effect of valid.
2 minutes later: HIT // indicates that the max-age set by the source server does not work, because the Expires set by the source server blocks the max-age
3 minutes later: MISS // This indicates that the expires configured on the server takes effect.

Instance 2 and instance 3: If the inactive setting is large, before the inactive expires, if valid, expires set on the server, and max-age set on the server are all set, the expires set on the server prevails.

Instance 5:

Php code on the server:

<? 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 items
Inactive 4 m // 4 minutes
# The following two lines are used to eliminate the impact of the Expires response header configured on the server.
Proxy_ignore_headers "Expires ";
Proxy_hide_header "Expires ";
Proxy_cache_valid 1 m // 1 minute
Symptom: after the first visit to the ta1.php page, the access results at various times are as follows:
1 minute later, HIT // indicates that the role of valid has been blocked by max-age on the server.
MISS 2 minutes later // The max-age set on the server will take effect

Instance 6:

Php code on the server:

<? 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 = 50"); // 50 seconds
// Header ("Cache-Control: post-check = 0, pre-check = 0", false );
Echo "ta1 ";
?>
Nginx cache configuration items

Inactive 4 m // 4 minutes
# The following two lines are used to eliminate the impact of the Expires response header configured on the server.
Proxy_ignore_headers "Expires ";
Proxy_hide_header "Expires ";
Proxy_cache_valid 2 m // 2 minutes
Symptom: after the first visit to the ta1.php page, the access results at various times are as follows:

50 seconds later: MISS // This indicates that the max-age configured on the server takes effect.
1 minute later: HIT //
100 seconds later: MISS // This indicates that the max-age set on the server takes effect.

Description of instance 5 and instance 6: If the inactive setting is large and the effect of server Expires on cache is canceled in the nginx configuration file. When both proxy_cache_valid and the max-age response header field are set on the server side, cache expiration is processed based on the max-age value set on the server side.

To sum up:

(1) when both the source server Expires, the source server max-age, and the proxy_cache_valid of nginx cahe are set, cache expiration based on the Expires value set on the source server
(2) if the related configuration items are configured in nginx, cancel the influence of the original server Expires on the cache, when both Expires on the source server, max-age on the source server, and proxy_cache_valid on the nginx cahe end are set, cache expiration is processed based on the max-age value on the source server.
(3) If you cancel the influence of Expires on the cache from the source server and max-age on the source server at the same time, cache expiration will be processed based on the value set by proxy_cache_valid.
(4) the Inactive value is not affected by the preceding three factors. That is, after the first request page, each time it passes through inactvie, the corresponding cache should be forcibly cleared. Therefore, inactive has the highest priority.
(5) the cache expiration priority is sorted as inactvie, Expires on the source server, max-age on the source server, and proxy_cache_valid.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.