1. Method One: Do not send the request, directly use the cache file
(1.1) Principle
When a browser requests a file through a URL, if the requested file is cached on the client, the cached time is checked for expiration, and if it does not expire, the request is not sent to the server and the client caches the file directly, otherwise it sends the request to the server to request the file.
(1.2) realize
When the service returns to the browser file, add the Expires or Cache-control property to the file header to set the file cache age.
(1.2.1) Method one: If the Web application is a JSP, you can add a filter to the Web application and append the expires or Cache-control attribute to the data flow that returns the client in the filter.
(1.2.2) Method Two: If the Web server is Apache, it can be implemented by the configuration of the Mod_headers and Mod_expires modules.
(1.2.2.1) Ensure that Apache has loaded the mod_headers.so and mod_expires.so, that is, the module folder in the Apache installation directory has mod_deflate.so and Mod_ headers.so file, and the httpd.conf file has the following code: (use Expires to add expiresactive on)
LoadModule Headers_module modules/mod_headers.so
LoadModule Expires_module modules/mod_expires.so
(1.2.2.1) Add header or expires code to the httpd.conf file, for example:
Add the following code to implement "All files returned by the server to the client are cached for 1 months (2.592 million seconds)":
Header set Cache-control "max-age=2592000"
Add the following code to implement the cache duration for all files returned by the server to clients is GMT 2008-12-01 16:00:00:
Header set Expires "Thu, Dec 16:00:00 GMT"
Add the following code to implement "All CSS files returned by the server to the client are cached for 1 months, 15 days, 2 hours":
Expiresactive on
Expiresbytype text/css "Access plus 1 month days 2 hours"
2. Mode two: Send the request, selectively use the cache file
(2.1) principle
When a browser requests a file via a URL, the browser makes a request to the server if the requested file is cached at the client. and compare the client cache file with the server-side file ETag or last-modified value, if the same, the server only returns 304 encoding, does not return the requested file, so the client uses the cached file, otherwise the server sends 200 encoding with the new file to the client.
(2.2) Implementation
The ETag and last-modified are included by default in the file headers that Apache and Tomcat send to the client.
for Apache, to add fileetag none to the file header without the ETag, you can add a httpd.conf in httpd.conf to include last-modified in the header. header unset last-modified .
3. Other instructions
Cache settings are prioritized: Cache-control > Expries > Etag > Last-modified, So when you need to implement the "etag,last-modified" caching function of mode two, be careful not to use the "Expires,cache-control" with mode one, because when used, the browser will never send a request to the server, then "Etag, Last-modified "configuration loses its meaning.
The above configuration is in effect for all files or a class of files on the Web server, and can be used to qualify the file range that the configuration is in effect with FilesMatch or locationmatch. However, the Fileetag and FilesMatch configurations are only valid for static files on Apache and are not valid for files deployed on other application servers that implement a cluster with Apache.
Example: All request paths that the server returns to the client contain/extjs/files that are cached for 1 months (2.592 million seconds)
<"/extjs/"></locationmatch>
HTTP request caching Configuration and rationale