Linux Cache system Learning browser cache

Source: Internet
Author: User
Tags browser cache

Recently found that their learning is too miscellaneous, not a system, so ready to clean up before starting. Finishing is also a distillation. On the way to learn more summed up, feel very good!

Here from the beginning of the cache, for a long time did not write what blog, content is not the place to welcome correction 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0002.gif "alt=" j_0002. GIF "/>

It seems that most of the problems can be solved by adding a cache, what is cache, the cache is to save the calculation of expensive costs, after the access to the direct extraction, the expensive cost can be expensive calculation, but also can be expensive bandwidth cost, etc.

Starting from the client side, first of all, the browser cache, perhaps from a certain point of view, the browser is a Web server, also can be counted as a cache, the internal existence of various cache negotiation process


Last-modified

Principle

This is basically a name to understand what he is, the last time to modify,

Flow chart:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/49/F8/wKioL1QgJQDjfs9vAAEtwaufDuU060.jpg "title=" Last-modified view Map "alt=" wkiol1qgjqdjfs9vaaetwaufduu060.jpg "/>

The first time the Web server returns to the client side last-modified the last modification time, and then once again, the client request header will be if-modified-since with the last modification time, The user asks the Web service side, after this time has not made the modification, if did not, the direct local return 304 state, if has made the modification, returns to the user from the Web service side directly, the user saves a copy locally, and then the next time has this one last modification date to prevail! This is still better understanding!

Configuration

Apache last-modified is turned on by default

The module is: mod_headers.so

Close method

To turn off the last-modified, you need to get rid of the last-modified label first. Then use the header module to control

LoadModule headers_module Modules/mod_headers.so<filesmatch "\. (gif|jpg|png) ">header unset last-modified</filesmatch>

Insufficient

1, last-modified can only be accurate to the second, if in seconds to make a change, is not detected

2, if it is a cluster environment, the same file last-modified may be different, the user may want to cache to get two times

3, no matter whether your content has no real changes

For the time being, I only know these shortcomings


ETag

Principle

ETag: There is a correct understanding on the Internet, my personal understanding is that when the client side initiates the visit, the Web server will cache the resource with the ETag value of the resource in the return header (a unique ID computed by the algorithm on the content), and the ETag value will be saved, When the same resource is requested again, the browser will bring the value returned from the first server to the request header (If-none-match) if the service side content does not have a table to return 304 if there is a change in return 200 content returned from the server, If used with last-modified, ETag priority is higher than last-modified

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/49/F7/wKiom1QgN8vwXbd1AAHyjvQG5kA427.jpg "title=" Etag.png "alt=" Wkiom1qgn8vwxbd1aahyjvqg5ka427.jpg "/>

Configuration

Apache

apache 默认开启Etag

格式:apache Etag由inode+大小+时间戳

关闭方式:

FileETag None
Header unset ETag
Header unset Last-Modified

nginx默认没有这个模块,需要重新编译进去

Insufficient

1、没有进行实际的测试,感觉如果文件多了,大了,还有如果自己写的算法烂也会影响性能



Expires

Principle

The most hanging, directly can not be used with the Web server cache negotiation, local negotiation can be completed, the Web server return to the head in the expiration time, and then each time the browser only need to compare the date with the expires, if it expires on the server to obtain, if not expired on the local return!

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/49/F7/wKiom1QgPNCjyi0yAADoeyhejLg217.jpg "title=" Expirse.png "alt=" Wkiom1qgpncjyi0yaadoeyhejlg217.jpg "/>

Configuration

Apache is implemented via the Expires module

Format:

Expiresactive on
Expiresbytype image/gif "Access plus 1 month"
Expiresbytype image/jpg "Access plus 1 month"
Expiresbytype image/jpeg "Access plus 1 month"

。。。。。

Nginx above default and open status

Format:

Location ~. *\. (JS|CSS)? $
{
Expires 1h;
}

Insufficient:

1, expires by the server and client side time impact, if the client time is slow or fast will affect the cache of valid rows, and client time this kind of uncontrolled information we can not go to achieve the same time



Because of the expires, Cache-control out.

Cache-control

Principle

Cache-control, like expires, is an effective time to describe a resource, but it's not that expires is a valid time for the time given by the date time to the server, The Cache-control effective time is one time after the client accesses the Web server and begins the calculation. If MAX-AGO=60 is the first access after the 60后 resource expires, this way to remove the expires dependence on client time, if expires and Cache-control exist simultaneously, Cache-control priority is higher than expires

Nginx Configuration


if ($request _uri ~* "^/$|^650) this.width=650; src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd; "alt=" Spacer.gif "/>arch/.+/ |^/company/.+/") {
Add_header Cache-control max-age=3600;

}
if ($request _uri ~* "^650) this.width=650;" src= "/e/u261/themes/default/images/spacer.gif" style= "Background:url ("/E /u261/lang/zh-cn/images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>arch-suggest /|^/categories/") {
Add_header Cache-control max-age=86400;
}



Small notes:

Three ways to refresh your browser:
    1. Use the browser's Go button, or enter the address bar directly, or open a new window to reenter the URL

      The browser local cache is preferred, and if there is a local cache, the server will not be accessed at all.

      Note: If it is under Chrome, in the Address bar under this window to enter directly, HTML will not go to the local cache, if the new window in the Address bar input URL Enter, the HTML will still go local cache. It's Chrome's strategy, Firefox will go local cache

    2. Use the browser's refresh button, F5 refresh, or command + R refresh under Mac Chrome

      Do not go to the browser local cache, but will walk the server cache, will return 304

    3. Use a forced refresh of your browser, such as CTRL + F5 or COMMAND + Shift + R under Mac Chrome

      Do not go to the browser local cache, do not walk the server cache, do not return 304

Requests that cannot be cached by the browser:

    1. HTTP Information header contains Cache-control:no-cache,pragma:no-cache, or cache-control:max-age=0, etc. that tell the browser not to cache the request

    2. Dynamic requests that determine input content based on cookies, authentication information, etc. cannot be cached

    3. HTTPS Security encryption Request (someone also tested found that IE in the head to add cache-control:max-age information, Firefox in the head after the addition of Cache-control:public, the ability to cache the HTTPS resources, Refer to the seven misconceptions of HTTPS)

    4. Post request cannot be cached

    5. Requests that do not contain last-modified/etag in the HTTP response header and do not contain cache-control/expires cannot be cached



This article is from the "on the Road" blog, please be sure to keep this source http://xiaochengxiang.blog.51cto.com/2558908/1557137

Linux Cache system Learning browser cache

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.