In the last module we have already seen on why the Http expires and the Http date headers are useful
as far as the http caching is concerned.
So in this module we'll configure our Nginx machine to include the expires header for different file
formats.
So for our scenario, we have two file formats that we'll be using.
One is dot txt and one is dot PNG.
So essentially they are the text and image file format.
And for each file format we will set a different expiry header.
So let's see on how we can do that.
So let me do a call on example.com slash logo dot PNG.
Ah, so sorry.
Pinch it.
So if we see over here, the date header is included.
However, there is no expires header for this particular file.
Same goes for the sample dot txt.
The date header is mentioned, but there is no expires header.
So let's see on how we can configure the expires header for both of them.
So I'll go to ATC Nginx.
Conniff And let's open Conniff.
Now, this is a simple configuration file.
So let's add one more location directive that will basically specify for any image file on the website.
Gash that image for, let's assume, 48 hours.
So browser.
When it opens image from example.com, it will cache it for 48 hours.
So.
Include location director.
Say for any image.
Say I'll say.
PNG file.
You can even include Jpeg.
I'll say expires.
48 hours.
You also have to mention the.
Root location for the images, which is.
I'll just copy it from the here.
Okay.
So the expires directive basically tells that any file that ends with PNG extension, which is from
this particular directory, the expires of that file should be 48 hours.
So basically what nginx will do is Nginx will add a expires header which will expire after 48 hours
of the time of response.
So i'll save this particular and let's do next.
Okay.
Let's reload nginx.
Awesome.
So now if we do a call on Http.
Example.com logo dot PNG.
Then.
Now you see an expired header has been introduced.
Now, we have already mentioned 48 hours.
So if we look into the date header, date header is.
18th November, Wednesday at 110 and expires is 20th November at 110.
So this is exactly 48 hours from the time the response has come.
Now, if you also notice Enginex has also introduced the cache control header.
Max hyphen h followed by the second.
So this basically if you multiply, then it becomes 48 hours.
So this second corresponds to the number of hours after which the image or the cache should be considered
stale.
Now we have already discussed expires header is specifically for the Http 1.0 protocol in the 1.1,
the nginx or the Http has introduced a new header field which is cache control where the expiry is mentioned
in seconds.
So for.
All the Http 1.1 client, they'll actually read the cache control directive and not the expires header
field over here.
So even if you don't mention explicitly the cache control nginx will automatically add when you add
the expires field within the location directive that we have already added.
Now, this is specifically for the image file.
So if we go on sample dot text, still it is not showing any expires header.
So we have to.
Specify for the text file as well.
So if I will have to add one more location directive, say anything which ends with dot txt route would
be the same.
I'll say expires.
I'll say one hour.
I'll save the file and I'll reload the enginex.
Let's clear the screen and let's do a call again on example.com.
Slash sample.txt.
And now you see the expires header has been added and for ease of readability it is 3600 seconds, which
counts to one hour.
So it will tell the cache that this response can be considered as the latest response for one hour.
Now going back to the web conf, you can actually specify the negative value as well.
So if I say minus one over here and let's do reload
and if we go.
You see basically the.
Okay, date was 140 and the expires is also 114.
So basically this means that do not cache this particular response.
So sample.txt should not be cached.
So even if you see in the cache control, there is a new field which is no cache which has been introduced.
So if you want to include or if you want to instruct the browsers to not cache this particular responses,
then you can actually say expires minus one and this will do the job.
So this is it about how you can include the expires header as far as the responses are concerned?
So this is it about this module.
I hope this has been informative for you and I'd like to thank you for viewing.