Nginx under Configuration Cache-control

Source: Internet
Author: User
Tags browser cache

The Cache-control of the HTTP protocol specifies the caching mechanism that requests and responses follow.
Setting Cache-control in a request message or response message does not affect the caching process in another message processing process.
The cache instructions for the request include No-cache, No-store, Max-age, Max-stale, Min-fresh, only-if-cached, and so on.
The directives in the response message include public, private, No-cache, No-store, No-transform, Must-revalidate, Proxy-revalidate, and Max-age.

in the browser about the cache of 3 Properties:

Cache-control:

Sets the relative expiration time, max-age indicates the cache time in seconds. If you cache a static resource only once, you can set the value of Max-age to 315360000000 (10,000 years).

Http Cache-control of the Agreement the common values and their combined interpretation:

No-cache: The data content cannot be cached, each request is re-accessed to the server, and if there is max-age, the server is not accessed during the cache.

No-store: Not only can not cache, even staging is not possible (that is, the temporary folder cannot be staged in the resource)

Private (default): Only in the browser cache, only when the first request to access the server, if there is max-age, the cache during the server is not accessed.

Public: can be cached by any buffer, such as: Browser, server, proxy server, etc.

Max-age: The relative expiration time, which is the cache time in seconds.

No-cache, Private: Re-access the server when a new window is opened, and if Max-age is set, the server is not accessed during the cache.

Private, Positive max-age: Do not access the server when back

No-cache, Positive max-age: accesses the server when back

Click Refresh: The server will be accessed anyway.

Expires:

Sets the absolute expiration time in minutes, with a lower priority than Cache-control, while setting expires and Cache-control takes effect.

Last-modified:

The last modification time of this resource, the browser will send a request to the server, and attach a if-unmodified-since header to indicate the last modification time of the browser's cached resource, if the server discovers that there is no modification, return 304 (not Modified) responds to the message to the browser (with little content) and returns the requested resource as usual if the server's comparison time is found to be modified.

Attention:

The Last-modified property is usually used in conjunction with the expires or Cache-control property, because even if the browser sets the cache, when the user taps the Refresh button, the browser ignores the cache and continues to send requests to the server. At this point last-modified will be able to reduce the response cost very well.

The ETag returns a resource ID to the browser, and if a new version is sent with the new ID, it returns 304, but in the case of a server cluster, each server returns a different ID, so the etag is not recommended.

The client browser cache described above refers to the storage location in the client browser, but the actual setup of the client browser cache is done in the resources on the server. Although we have just described the properties of the client browser cache, the actual settings for these properties need to be set in the server's resources. We have two ways to set the browser cache, one is set by the page directive declaration, the other is set programmatically.

Nginx Ngx_http_headers_module module can be configured for Cache-control-related things
For example:
# related Page Setup Cache-control header information
Example one:
if ($request _uri ~* "^/$|^/search/.+/|^/company/.+/") {
Add_header Cache-control max-age=3600;
}

Example two:

Location ~. *\. (css|js|swf|php|htm|html) $ {
Add_header Cache-control No-store;
}

Example three:
Location ~. *\. (JS|CSS) $ {
Expires 10d;
}
Reference Documentation:
Http://www.cnblogs.com/jason-zy/archive/2012/03/06/2381433.html
Http://www.cnblogs.com/wenanry/archive/2012/02/19/2358708.html
Http://zhidao.baidu.com/link?url=rr7aH9tLx2-PyfPMxp-PnRDa-RYRNSzhapmOgViBhkBAICEaNqbQV5QumAvNLUPKSbRD0I5_ D0hwyrp134f-s6t8ruqhb5w3hmq3lipndyu

Nginx under Configuration Cache-control

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.