“This is the 16th day of my participation in the November Gwen Challenge. See details of the event: The Last Gwen Challenge 2021”.
Nginx Static Resource Deployment [3]
Previous articles:
I met Nginx
The installation of the nginx
Nginx core configuration file structure
Nginx Static Resource Deployment [1]
Nginx Static Resource Deployment [2]
Caching of static resources
What is caching
Cache, in its original meaning, refers to a type of high-speed memory that is accessed faster than regular random access memory (RAM). It usually uses the expensive but faster SRAM technology rather than DRAM technology like system main memory. The cache setup is one of the most important factors in the performance of any modern computer system.Copy the code
What is Web caching
A Web cache is a copy of a Web resource (such as HTML pages, images, JS, data, etc.) that exists between a Web server and a client (browser). The cache stores a copy of the output based on incoming requests; When the next request comes in, if it is the same URL, the cache will decide based on the caching mechanism whether to respond to the access request directly with the copy or send the request to the source server again. It is common for the browser to cache the page that has been visited by the website. When the browser visits this URL again, if the page has not been updated, it will not download the page again, but directly use the local cached page. Only when the site clearly indicates that the resource has been updated will the browser download the page againCopy the code
Type of Web cache
Client cache Browser cache Server cache Nginx/Redis/Memcached, etcCopy the code
Browser cache
In order to save network resources and speed up browsing, the browser stores the recently requested document on the user's disk. When the visitor requests this page again, the browser can display the document from the local disk, which can speed up the page reading.Copy the code
Why browser cache
The lowest-cost cache reduces network bandwidth consumption, reduces server stress, reduces network latency, and speeds up page openingCopy the code
Execution flow of browser cache
HTTP: HTTP: HTTP: HTTP: HTTP: HTTP: HTTP: HTTP
header | instructions |
---|---|
Expires | The date and time the cache expires |
Cache-Control | Set the configuration information related to the cache |
Last-Modified | Request resource last modified time |
ETag | The current value of the entity tag of the request variable, such as the MD5 value of the file |
(1) The user sends a request to the server to obtain data through the browser for the first time, but the client does not have the corresponding cache, so it needs to send a request to obtain data;
(2) After receiving the request, the server will obtain the data of the server and the permission of the server cache, return the success status code of 200 and attach the corresponding resources and cache information on the response head;
(3) When the user accesses the same resource again, the client will look for the cache file of the response in the cache directory of the browser
(4) If no corresponding cache file is found, go to step (2)
(5) If there is a cached file, the next step is to determine whether the cached file Expires. The criterion for determining expiration is (Expires).
(6) If there is no expiration date, the data will be returned directly from the local cache for display
(7) If Expires, the next step is to determine whether the cached file has changed
(8) There are two criteria for judging Entity Tag and Last-Modified
(9) If the judgment result is no change, the server returns 304 and directly obtains data from the cache file
(10) If it is judged that there is a change, it will get the data from the server again and cache the data according to the cache negotiation (whether the setting of the server needs to cache the data).
The browser caches related instructions
If Nginx needs to set up the cache, it needs to use the following instruction
Expires instruction
Expires: This directive controls what page caching does. You can use this directive to Control the “Expires” and” cache-control “responses in HTTP.
grammar | expires [modified] time expires epoch|max|off; |
---|---|
The default value | expires off; |
location | HTTP, Server, location |
Time: specifies the expiration time. If the value is negative, cache-control is no-cache. If the value is integer or 0, cache-control is max-age=time.
Epoch: Specifies Expires with the value ‘1 January,1970 00:00:01 GMT'(1970-01-01 00:00:00) and the cache-control value no-cache
Max: Specifies the Expires value to be ’31 December2037 23:59:59GMT’ (2037-12-31 23:59:59) and the cache-control value to be 10 years
Off: No cache by default.
Add_header instruction
The add_header directive is used to add the specified response header and value.
grammar | add_header name value [always]; |
---|---|
The default value | – |
location | HTTP, server, location… |
Cache-control serves as the response header information. You can set the following values:
Cache response instruction:
Cache-control: must-revalidate
Cache-control: no-cache
Cache-control: no-store
Cache-control: no-transform
Cache-control: public
Cache-control: private
Cache-control: proxy-revalidate
Cache-Control: max-age=<seconds>
Cache-control: s-maxage=<seconds>
Copy the code
instruction | instructions |
---|---|
must-revalidate | Cacheable but must be validated with the source server |
no-cache | You must verify its validity before caching |
no-store | No content of the request or response is cached |
no-transform | Agents cannot change media types |
public | Caching of responses can be provided to any party |
private | Returns a response only to a specific user |
proxy-revalidate | The intermediate cache server is required to validate the cached response |
Max – age = < s > | Maximum Age value of the response |
S – maxage = < s > | The maximum Age value for the public cache server response |
Max – age = [s] :