Nginx expires control page Caching _nginx

Source: Internet
Author: User
Tags browser cache
Syntax: Expires [TIME|EPOCH|MAX|PFF] Default: Offexpires Command controls the "expires" and "Cache-control" header header information in an HTTP response, and starts controlling the page cache for time: You can use positive or negative numbers. The value of the "Expires" header will be set by the current system time plus the setting of the duration value. The time value also controls the value of "Cache-control": A negative number indicates a no-cache positive or a zero indicates Max-age=time epoch: The value of the specified "Expires" is 1january,1970,00:00:01 Gmtmax: Specifies " The value of Expires is 31december2037 23:59:59gmt, and the value of "Cache-control" is 10 years. -1: Specifies that the value of "Expires" is the current server time -1s, that is, expires forever. OFF: Do not modify values for "Expires" and "Cache-control"

Expires uses a specific time and requires strict synchronization between the server and the client.
Instead, the Cache-control uses the max-age instruction to specify how long the component is cached.
For browsers that do not support http1.1, you still need expires to control it. So it's a good idea to specify two response headers. However, the HTTP specification stipulates that the Max-age directive overrides the expires header.

If you do not want the proxy or browser cache, add the No-cache parameter or private argument:
# expires 1d;
Add_header Cache-control No-cache;
Add_header Cache-control Private;
When the browser F5 refresh, the return is still 200, not 304.

Record an example of a Nginx control cache:
Expires 1d;
Add_header Cache-control No-cache;
Add_header Cache-control Private;
if (!-e $request _filename) {
Rewrite ^ (. *) http://test.zhaopin.com/index.html break;
# Add_header Cache-control No-cache;
# Add_header Cache-control Private;
When I wrote Add_header in rewrite, I found out that Add_header didn't work. Because it's written in the If.


Apache's Mod_expires module makes it possible to set the date in a relative way, as Max-age did with the expires header, through the expiresdefault instruction. For example: Picture, and so on expires 10 years after the request starts
<filematch "\. (GIF|JPG|JS|CSS) $ ">
ExpiresDefault "Access plus 10years"
</FileMatch>
It sends expires headers and Cache-control max-age headers like the response.

Expires|etag Control Page Cache difference Expires: As mentioned in the previous article: Expires command controls the "expires" and "Cache-control" header information in an HTTP response, starting control page caching effect time: You can use positive or negative numbers. The value of the "Expires" header will be set by the current system time plus the setting of the duration value. The time value also controls the value of "Cache-control": A negative number indicates a no-cache positive or a zero indicates Max-age=time epoch: The value of the specified "Expires" is 1january,1970,00:00:01 Gmtmax: Specifies " The value of Expires "is 31DECEMBER203723:59:59GMT, and the value of" Cache-control "is 10 years. -1: Specifies that the value of "Expires" is the current server time -1s, that is, expires forever. OFF: Do not modify values for "Expires" and "Cache-control"

Expires uses a specific time and requires strict synchronization between the server and the client.
Instead, the Cache-control uses the max-age instruction to specify how long the component is cached.
For browsers that do not support http1.1, you still need expires to control it. So it's a good idea to specify two response headers. However, the HTTP specification stipulates that the Max-age directive overrides the expires header. Generally used for page change is not fast, if the cache expires, the browser before reusing it will first confirm that he is valid, is a "conditional GET request", if valid, return 304 status code. The expires is determined by the last-modified response header. As shown in figure:
First visit:
Request:


Return:


Second visit:
Request:


Return:


The status code returned is 304, and the if-modified-since header is more than the first in the request, compared to the last-modified in the original server, so the browser cache is implemented and the expiration is judged.
Simply put, last-modified and if-modified-since are all HTTP headers that are used to record the last modification time of a page, but last-modified are HTTP headers sent by the server to the client, and If-modified-since is sent by the client to the server header, you can see, again request the local cache page, the client will pass the If-modified-since header to the previous server sent over the Last-modified Last modified timestamp sent back, this is to allow the server side to verify, through this timestamp to determine whether the client's page is the latest, if not the latest, then return the new content, if it is the latest, then return 304 to tell the client its local cache page is the latest , so the client can load the page directly from the local, so that the data transmitted on the network will be greatly reduced, but also reduce the burden on the server.

If you do not want the proxy or browser cache, add the No-cache parameter or private argument:
# expires 1d;
Add_header Cache-control No-cache;
Add_header Cache-control Private;




ETag: Entity tags are a mechanism for confirming the validity of the Web server and browser caching. The original server uses the ETag response header to specify that the component's ETag browser passes ETag back to the original server through the If-none-match header. If match, return 304
As shown in figure:
First visit:
Request:


Return:


Second Request:


Return:


The status code still returned is 304, and the If-none-match header is more than the first time in the request. So the browser cache is implemented. If this value is not aligned, the cache expires.
Etags and If-none-match are commonly used to determine whether resources change. Similar to last-modified and Http-if-modified-since. But the difference is that last-modified and http-if-modified-since only determine the last modification time of the resource, and Etags and If-none-match can be any attribute of the resource.
The work of Etags and If-none-match is to add etags information to the HttpResponse. When the resource is requested again by the client, the If-none-match information (etags value) is added to the HttpRequest. If the server verifies that the resource's etags has not changed (the resource has not changed), it returns a 304 state, otherwise the server returns a 200 state and returns the resource and the new etags.

The ETag format on IIS needs to be modified.

Another article explains it very clearly: http://www.jb51.net/article/33214.htm

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.