http-header : cache-control

http-header : cache-control

It is a general-header that is used to specify directives for both caching mechanisms that is requests and responses. The caching directives are unidirectional, which means that a given directive in a request is not implying that the same directive is to be given in the response.
This HTTP header is useful for specifying the browser caching policies that involves both the client requests as well as the server responses. Such policies involve the way a resource is cached, where the resource is cached and what is its maximum age before getting expired (that is, time to live). HTTP/1.1 has introduced headers belonging to a new class, which are “Cache-Control” response headers that are helpful in allowing the web publishers so as to clearly define the way pages must be handled through caches.

Whenever any file is said to be accessed by a browser then it’s noted that the retrieval of HTTP headers also takes place. If the header, Cache-Control is put for inclusion then browser would respect values which is found there.
If browser notices that any file must be cached for a period of five minutes then it would stay in browser cache for that specific five minutes, keeping in mind that file wouldn’t have to be accessed again if called once again.
For example let’s consider our logo image. When a visitor comes to any one of our web pages, he/she would download that logo image. Now, let’s say when the user moves to yet another webpage present on our site, that logo image wouldn’t be downloaded again. Rather a cached version would be put to use.

Enablement in Apache:
The below mentioned syntax is added in the “.htaccess” file.
Header set Cache-Control “max-age=2628000, public”
The code mentioned above, however doesn’t mention any ability so as to provide the different caching instructions in regards to different file types. The code given below serves the purpose.
# One month for most static assets

Header set Cache-Control “max-age=2628000, public”

So, what the code above is saying is that if file type matches any one of the types mentioned above then the header would be applied to it.
Besides the use of “.htaccess” file, headers can be set through the use of “config” file which is rather faster and recommended too.

Enablement in Nginx:
By use of the directive “expires”, we would be able to add the cache instructions to any server or any location block.
Location ˜* \.(jpg|jpeg|png|gif|ico|css|js)$ {
Expires 365d;
}
The code is applicable to each of the file types mentioned in the first line which can be added or removed. There are also privileges of having multiple blocks for fine tuning the different file types.

Copyrights ©2008: Valency Networks Pvt Ltd.