Last-modified and if-modified-since

Source: Internet
Author: User

From http://ronxin999.blog.163.com/blog/static/4221792020119106319604/

In HTTP, both last-modified and if-modified-since are HTTP header information used to record the last modification time of the page. Note that, the last-modified is the HTTP header sent by the server to the client, and the other if-modified-since is the header sent by the client to the server. You can see that the local
When caching the page, the client sends the last modified timestamp of last-modified sent from the previous server back through the IF-modified-since header to the server for verification, this timestamp is used to determine whether the client page is the latest. If it is not the latest, new content is returned. If it is the latest, then, 304 is returned to inform the client that the page of the local cache is the latest, so the client can directly load the page from the local, so that the data transmitted over the network will be greatly reduced, it also reduces the burden on the server. In addition, in some Ajax applications, the data to be obtained is always up-to-date, rather than reading the data in the cache. This setting is very necessary.

1) What is "last-modified "?

When the browser requests a URL for the first time, the server returns 200, and the content is the resource you requested, at the same time, there is a last-modified attribute to mark the last modification time of this file on the service end. The format is similar to this:

Last-modified: Fri, 12 May 2006 18:53:33 GMT

When the client requests this URL for the second time, according to the HTTP protocol, the browser will send the IF-modified-since header to the server, asking if the file has been modified after the time:

If-modified-since: Fri, 12 May 2006 18:53:33 GMT

If the resources on the server are not changed, the HTTP 304 (not changed.) status code is automatically returned and the content is blank, which saves the amount of data transmitted. When the server code changes or the server is restarted, the resource is re-issued, and the returned result is similar to the first request. This ensures that resources are not repeatedly sent to the client, and that the client can obtain the latest resources when the server changes.

2) What is "etag "?

The HTTP Protocol Specification defines etag as the object Value of the requested variable (See Chapter 14.19 ). In other words, etag is a token that can be associated with web resources ). A typical web resource can be a Web page, but it may also be a JSON or XML document. Server ticket
It is solely responsible for determining what a mark is and its meaning, and transmitting it to the client in the HTTP Response Header. The following is the format returned by the server:

Etag: "50b1c1d4f775c61: df3"

The query update format of the client is as follows:

If-None-Match: W/"50b1c1d4f775c61: df3"

If etag does not change, status 304 is returned and no result is returned, which is the same as last-modified. I personally tested etag mainly for resumable download.


How does last-modified and etags help improve performance?
Smart developers will use the HTTP header of the last-modified and etags requests together, so that the cache of the client (such as the browser) can be used. Because the server first generates the last-modified/etag mark, the server can use it later to determine whether the page has been modified. Essentially, the client requests the server to verify the token by sending it back to the server.
User) cache.
The process is as follows:
1. The client requests a page ().
2. The server returns to page A and adds a last-modified/etag to page.
3. The client displays the page and caches the page together with last-modified/etag.
4. The customer requests page a again and passes the last-modified/etag returned by the server in the last request to the server.
5. The server checks the last-modified or etag and determines that the page has not been modified since the last client request. The server returns the response 304 and an empty response body.

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.