1. Strong Cache
1. definition
• Strong cache tells the browser directly:No need to communicate with the server before the cache expires, directly use local cache.
• Pass the response header by the serverCache-Control
andExpires
control.
2. Response header
• Cache-Control: max-age=3600
Indicates that the resource is3600 seconds (1 hour)Valid within (priority higher thanExpires
)。
• Expires: Thu, 31 Dec 2030 23:59:59 GMT
Specifies an absolute expiration time (dependent on client local time, errors may occur).
3. Nginx configuration example
location /static/ {
# Set strong cache: valid within 1 year
add_header Cache-Control "public, max-age=31536000";
expires 1y;
}
4. Behavior
• When the browser first requests a resource, the server returns the resource and comes with a cache header.
• During subsequent requests, the browser directly reads the local cache (status code)200 (from disk cache)
),Do not send requests to the server。
5. Applicable scenarios
• Static resources (such as CSS, JS, pictures, font files) that remain unchanged for a long time.
2. Negotiation cache (Weak Cache)
1. definition
• Negotiate cache requirements browserVerify with the server every time that the cache is expired, if it has not expired, it will return304 Not Modified
, continue to use local cache.
• Pass the response header by the serverLast-Modified
andETag
control.
2. Response header
• Last-Modified: Wed, 21 Oct 2023 07:28:00 GMT
Indicates the resource's last modification time (precision is seconds, which may be invalid due to time synchronization problems).
• ETag: "5d8c72a5-264"
A unique identifier (hash value or version number) of the resource, with higher precision.
3. Nginx configuration example
location /dynamic/ {
# Enable negotiation cache (supported by default, no explicit configuration is required)
add_header Last-Modified "";
etag on;
}
4. Behavior
- When the browser first requests a resource, the server returns the resource and comes with it
Last-Modified
orETag
。 - On subsequent requests, the browser verifies the cache through the following request header:
•If-Modified-Since: [Last-Modified value]
Ask the server if the resource has been modified after the specified time.
•If-None-Match: [ETag value]
Verify the resource to the serverETag
Whether it changes. - If the resource is not modified, the server returns
304 Not Modified
, the browser continues to use the cache; if modified, return a new resource (status code200
)。
5. Applicable scenarios
• Resources that are frequently updated (such as HTML pages, dynamic API responses).
3. Key differences
characteristic | Strong cache | Negotiate cache |
---|---|---|
Communication costs | No network requests (direct read cache) | Request verification cache is required |
Response status code | 200 (from disk cache) | 304 Not Modified |
Priority | Priority over negotiated cache | Triggered after the strong cache expires |
Applicable resources | Static resources that remain unchanged for a long time | Frequently updated dynamic resources |
IV. Nginx best practices
-
Mix two caches
location / { # Strong cache for 1 hour, and enable negotiation cache after expiration add_header Cache-Control "public, max-age=3600"; etag on; }
-
Distinguish policies by file type
# Strong cache of pictures, fonts, etc. location ~* \.(jpg|png|gif|woff2)$ { expires 1y; add_header Cache-Control "public, max-age=31536000"; } # HTML files are disabled for strong cache (always negotiated) location ~* \.html$ { add_header Cache-Control "no-cache, must-revalidate"; }
-
Solve cache update issues
• Strong cache resources are recommended to passFile name hashControl version (e.g.main.
)。
• Negotiation cache can be modifiedETag
orLast-Modified
Trigger update.
5. Debugging tools
- Browser Developer Tools (Network Tag):
• Check200 (from disk cache)
(Strong cache) or304 Not Modified
(Negotiation cache).
• Check the request headerCache-Control
、If-Modified-Since
、If-None-Match
。 - Command line tools:
curl -I /
By rationally configuring strong caching and negotiated caching, website performance can be significantly improved and server load can be reduced.