Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of controlling page cache by nginx expires

2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains the "nginx expires control page cache method", the article explains the content is simple and clear, easy to learn and understand, now please follow the editor's train of thought slowly in depth, together to study and learn "nginx expires control page cache method"!

Syntax: expires [time | epoch | max | pff] default value: the offexpires instruction controls the "expires" and "cache-control" header header information in the http response, and the role of starting the control page cache time: you can use positive or negative numbers. the value of the "expires" header will be set by the current system time plus the set time value. The time value also controls the value of "cache-control": negative numbers for no-cache positive numbers or zero for max-age=time epoch: specify that the value of "expires" is 1januaryjanuaryjanuaryjanuaryjanuaryjanuaryjanqianjanuaryjanuaryjanuaryjanqianjanuaryjanuaryjanqianjanuaryjanqianjanqianjanqiao 0000no-cache 01gmtmax: specify that 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, it expires forever. Off: do not modify the values of "expires" and "cache-control"

Expires uses a specific amount of time and requires strict synchronization between the server and the client.

Cache-control, on the other hand, uses the max-age instruction to specify how long the component is cached.

For browsers that do not support http1.1, expires is still needed to control it. So it's best to specify two response heads. However, the http specification states that the max-age directive will override the expires header.

If you do not want the proxy or browser to cache, add the no-cache parameter or the private parameter:

# expires 1d

Add_header cache-control no-cache

Add_header cache-control private

In this way, when the browser f5 refreshes, it still returns 200 instead of 304.

Record an example of nginx controlling caching:

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 after rewrite, I found that add_header didn't work.. Because it was written in if..

Apache's mod_expires module makes it possible to set dates in a relative manner like max-age when using expires headers, which is done through expiresdefault instructions. For example, the expiration time of pictures, etc., is 10 years after the beginning of the request.

Expiresdefault "access plus 10years"

It is like sending the address header and the cache-control max-age header in the response.

Expires | etag controls page cache distinction expires: as mentioned in the above article: the expires instruction controls the "expires" and "cache-control" header header information in the http response, and starts to control the role of page cache time: positive or negative numbers can be used. the value of the "expires" header will be set by the current system time plus the set time value. The time value also controls the value of "cache-control": negative numbers for no-cache positive numbers or zero for max-age=time epoch: specify that the value of "expires" is 1januaryjanuaryjanuaryjanuaryjanuaryjanuaryjanqianjanuaryjanuaryjanuaryjanqianjanuaryjanuaryjanqianjanuaryjanqianjanqianjanqiao 0000no-cache 01gmtmax: specify that 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, it expires forever. Off: do not modify the values of "expires" and "cache-control"

Expires uses a specific amount of time and requires strict synchronization between the server and the client.

Cache-control uses the max-age instruction to specify how long the component will be cached.

For browsers that do not support http1.1, expires is still needed to control it. So it's best to specify two response heads. However, the http specification states that the max-age directive will override the expires header. Generally used when the page change is not very fast, if the cache expires, the browser will first confirm whether it is valid before reusing it, which is a "conditional get request" and, if valid, returns a 304 status code. Expires is determined by the last-modified response header. As shown in the figure:

First visit:

Request:

Return:

Second visit:

Request:

Return:

At this time, the returned status code is 304, and there are more if-modified-since headers in the request than the first time, and compared with the last-modified creation in the original server, so implement the browser cache and determine whether it expires.

To put it simply, both last-modified and if-modified-since are the http header information used to record the last modification time of the page, but last-modified is the http header sent by the server to the client, while if-modified-since is the header sent by the client to the server. You can see that when the cache page that exists locally is requested again, the client will send back the last modification time stamp of last-modified sent from the server through the if-modified-since header. This is for the server to verify, through this timestamp to determine whether the client page is up-to-date, if not up-to-date, return new content, if it is up-to-date, return 304 to tell the client that its local cache page is up-to-date, 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 to cache, add the no-cache parameter or the private parameter:

# expires 1d

Add_header cache-control no-cache

Add_header cache-control private

Etag: entity tags, a mechanism used to confirm the validity of web server and browser caches. The original server uses the etag response header to specify that the etag browser of the component passes the etag back to the original server through the if-none-match header. If it matches, return 304

As shown in the figure:

First visit:

Request:

Return:

Second request:

Return:

The status code still returned is 304, and there are more if-none-match headers in the request than the first time. So implement browser caching. If this value does not match, the cache expires.

Etags and if-none-match are commonly used methods to determine whether a resource has changed. 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, while etags and if-none-match can be any attribute of the resource.

Etags and if-none-match work by adding etags information to httpresponse. When the client requests the resource again, the if-none-match information (the value of etags) is added to the httprequest. If the server verifies that the etags of the resource has not changed (the resource has not changed), it returns a 304state; otherwise, the server returns the 200status and returns the resource and the new etags.

The etag format on iis needs to be modified.

Thank you for your reading, the above is the content of "nginx expires control page cache method", after the study of this article, I believe you have a deeper understanding of the nginx expires control page cache method, the specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report