What does HTTP status code 304 mean

The understanding of HTTP status code 304

1. If the client finds that the cached file has last modified when requesting a file, then if modified since will be included in the request. This time is the last modified time of the cached file. Therefore, if the request contains if modified since, it means that it has been cached in the client. Just judge this time and the modification time of the currently requested file to determine whether to return 304 or 200.

2. For static files, such as CSS and pictures, the server will automatically complete the comparison between last modified and if modified since, and complete the cache or update.

3. For dynamic pages, that is, dynamically generated pages, they often do not contain last modified information, so browsers and gateways will not cache, that is, they will complete a 200 request every time they request. Therefore, to speed up the caching of dynamic pages, first add the last modified definition in the HTTP header of the response, and then return 200 or 304 according to if modified since in the request and the update time of the requested content. Although we have done a database query when we return 304, we can avoid the following more database queries, and only an HTTP header without returning the page content, which greatly reduces the consumption of bandwidth and improves the user’s feeling.

2. What is “last modified”

1. When the browser requests a URL for the first time, the server’s return status will be 200. The content is the resource you requested. At the same time, there is a last modified attribute to mark the last modified time of this file at the service end. The format is similar to this:

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

2. 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 whether the file has been modified after this time

If-Modified-Since: Fri, 12 May 2006 18:53:33 GMT

The server-side program first obtains the value of this field, and then compares it with the last modification time of the data on the server. If the server-side resources do not change, it directly returns 304 not modified status code, and then stops. In this way, the amount of data transmission is saved and the bandwidth is saved. When the server code changes or the server is restarted, the resource is reissued, and the return is similar to the first request. So as to ensure that the resources are not sent to the client repeatedly, and that when the server changes, the client can get the latest resources.

Third, why do you use conditional requests

When a user visits a web page, conditional request can speed up the opening time of the web page (because it can save the time of transmitting the whole response body), but there will still be network delay, because the browser still has to generate a conditional request for each resource, and wait until the server returns the http/304 response before reading the cache to display the web page, The server specifies cache control or expires instructions on the response, so that the client can know how long the resource is available, and can skip the step of conditional request and use the resource in the cache directly

conditional requests are still needed in the following cases

1. After exceeding the expiration time specified by the server

2. If the user performs a refresh operation

Reference: HTTP status code 304 http://www.studyofnet.com/news/982.html

Similar Posts: