Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
In my last article on nginx, I wrote the following about nginx:
- Environment set up
- location
- Gzip compression
- Log on
If you don’t know, go to: Nginx Series – Basic module configuration
Through this article, you will learn the following configuration of nginx:
- Resource Cache Configuration
Nginx default cache configuration
First we configure location as follows and start nginx by referencing an image to the index.html file under the HTML directory.
location / {
root html;
index index.html index.htm;
add_header Cache-Control private;
}
Copy the code
After startup, open the browser to visit, refresh and then check the control panel. We see the response information of the picture as follows:
The response header contains both Etag and Last-Modified information that is used to handle negotiated caching. Nginx has caching enabled for resources by default.
Some resources are 200 from memory cache and some are 304.
By checking the request header, it can be found that the response is 200 from memory cache. The request header does not carry information such as Etag, so the strong cache is used by default.
We also set the cache to private, indicating that the cache is only for individual users. Middlemen cannot cache this response. This response can only be used in the browser’s private cache, of course the default configuration item is private. If public is configured, the response can be cached by any middleman.
Nginx configures the cache time
The location ~ *. (GIF | JPG | PNG | BMP | ico) ${# 1 d set the cache time expires; }Copy the code
Set the image resource cache time to 1 day, restart nginx to see the browser return effect.
Cache-control :max-age=86400 time is returned in the response header. If the next request is within 1 day, the browser Cache is used directly. If the request exceeds the time, the server needs to check. And give the resources to the client.
Nginx disables strong caching
location / { root html; index index.html index.htm; Add_header cache-control no-cache; }Copy the code
After you add add_header cache-control no-cache to the configuration file and refresh the browser for several times, you can find that the file response code is 304 and the negotiation Cache is removed, that is, file verification is initiated to the server each time. Here’s the picture:
Nginx disables caching
location / {
root html;
index index.html index.htm;
add_header Cache-Control no-store;
}
Copy the code
Change the cache-control to no-store and execute the nginx -s reload command. After refreshing the browser several times, you will find that the resource file request is 200 times, and no data is fetched from the Cache.
Cache related identifier
Etag
Etag is a tag associated with web resources. The Etag is sent to the client by the response header. The next request is passed through the if-none-match header. It was designed to address last-Modified deficiencies, such as,
- Some file modifications are made at sub-second speeds, however
If-Modified-Since
The granularity that can be examined iss
Level.
With weak Etag, it is generated based on MTime and can only be accurate to S, so the Etag generated within 1s is the same. Frequent cache flusher caused by a strong Etag can be avoided. Weak Etag starts with W/.
- Some files change periodically, but their contents do not change, and we do not want it to be considered modified, etc.
- Some servers do not know exactly when a file was last modified.
File storage
from memory cache
Do not request network resources, resources are stored in memory, generally stored fonts, pictures, scripts.
from disk cache
Network resources are not requested. Resources are stored on disks, and non-script resources, such as the CSS, are generally stored.
conclusion
Finally, combined with the following flow chart of browser cache policy, are you familiar with the overall process and configuration of cache? In the next article, take a look at how nginx configures caching policies.