This header encompasses the date/time after which the response is considered stale. Normally, such a stale cache entry might not be returned back by a cache (that is, maybe either a proxy cache or user agent cache) until and unless it’s first checked or validated with origin server (or maybe with any intermediate cache that holds a fresh copy of the entity). If there is a presence of Cache-Control header having directives such as “max-age” or “s-maxage” in the response, the Expires header is ignored.
Invalid dates, such as the value 0, indicate a date in the past which means that the resource is already expired. An expiration time in the past or just the specification of the value 0 would work so as to prevent caching. In order to mark up a response as being “already expired”, an Expires date is sent by the origin server which is equal to value of Date header. Now, in order to mark up a response as being “never expires,” an Expires date is sent by origin server holding value of approximately one year and that too from the time the response is sent. This header helps in telling the browser about whether a specific file must be requested by them from server or whether the browser’s cache must be grabbed by them.
Working of header:
This header can be set on specific files or even specific file types. Now, at the time when browser comes to the website it can view what was the last time it had downloaded those particular file types. If done recently it would display them from cache itself. But, if not having visited the site in a while it would download recent version from web server. The whole point is to provide late expiry times for the items that do not change on our website (such as, logo, colors etc). Short expiry times must be set for the things that change frequently.
Importance of header:
The addition of this header is actually important so as to reduce the HTTP requests which thereby help in reducing the time taken for server to communicate with browser. Besides that, it also is helpful in allowing users to reuse cache files which have been already stored in browser so as to reduce amount of required files they need for downloading. The main idea behind this header is not just about reducing load of downloads which is coming from the server (that is, constant downloading of the same file while it’s unmodified is rather wasting the precious load time) but rather about reducing number of the HTTP requests for server.
Enablement in Apache:
The module for Expires isn’t compiled by default. So, its enablement must be done in “httpd.conf” file. It must be made sure that below line is present and also uncommented (that is, remove the preceding #).
LoadModule expires_module modules/mod_expires.so
Now, in order to set this header the below mentioned lines must be added to the <virtualHost> section of our Apache vhost configuration.
ExpiresByType image/gif “access plus 1 months”
ExpiresByType image/jpg “access plus 1 months”
ExpiresByType image/jpeg “access plus 1 months”
ExpiresByType image/png “access plus 1 months”
ExpiresByType image/vnd.microsoft.icon “access plus 1 months”
ExpiresByType image/x-icon “access plus 1 months”
ExpiresByType image/ico “access plus 1 months”
ExpiresByType text/css “now plus 1 months”
ExpiresDefault “access plus 1 days”
Or, the above can be added to our “htaccess” file present in an <ifModule mod_expires.c></ifModule> block.
Enablement in IIS:
First get access to the setting from “IIS management” console. There, select “HTTP Response Headers” module. Then click “Add Common Headers” link which is present on the right hand side of the console.
The dialog box which comes up next holds an “Expire Web Content” checkbox, from where we can select so as to expire the content immediately if in case we don’t want browser to cache, or maybe expire after a specific period of time (such as, time in minutes, hours, days, etc.), or have it set so as to expire it on a specific date.
Enablement in Nginx:
The below mentioned syntax is added to “nginx.cong” file.
add_header “Expires” “-1”;