Hey, everyone.
And welcome back.
Now, in the earlier lectures we were discussing about the Http cache control headers and we looked
into the store header in detail.
So continuing our journey with the cache control headers today we'll be speaking about if modified since
header.
Now this is quite important header and this specific header is sent quite a lot by the browsers.
So let's go ahead and understand more about this header.
Now, we had looked into the scenarios where there can be multiple same requests by the browser, so
if a user refreshes the page 5 or 10 times, then what Ideally a cache does is it sends the response
from the cache itself instead of connecting to the web server.
So let's look into the scenario.
So let's assume there is a file which is already cached in the.
Cash server.
So what happens is a client sends a request of get my C dot PNG.
Now since the cache already has this file in its local storage, what it will do is it will send the
file from the local store itself.
Now, this is quite good enough.
But the problem is what happens if the file in the web server side is updated?
Now, in that case, the caching server has sent a older response.
That means the new updated file is not sent because of the cache servers misconfiguration related settings.
So in order to solve this kind of a issue where a older response might be sent, a new header is generally
used as if modified since header.
So what really happens is let's assume you have a this is a my C dot PNG file which is there in the
web server side and it is also there in the cache server side.
Now when a client sends a request, a get request on my c dot PNG, although the cache server has this
file, it will still verify whether the file which is there in its local cache is the latest updated
one.
So how can a cache server know how this file is updated or not?
So what it can do is the cache server will send.
A get request on my PNG asking whether this file is modified since this specific amount of time.
So this time is basically the time associated with the last modified header of the specified PNG file.
So the cache server will send a get request asking if this file is modified or not.
After this specific time.
If it is not modified then the web server will send a response saying 304 not modified and then the
cache server will serve the file from the local cache itself.
However, if this file is modified, then the web server will not send 304.
It will send a 200 with a new file.
Perfect.
So let's do one thing.
Let's go into the practical and let's understand this in great detail.
So we have been looking into the practical and we'll try and keep the link same.
So you have Dexter Labs dot in slash JPG.
Now if you do a curl hyphen I on this specific URL
you will see there is one specific header called as last modified and it has a modified or it has the
time at which this file was specifically updated.
My c dot jpg.
Now let's.
Take the example of a Docker instance so that it will become much more easier for us to do practical.
I'll do a curl hyphen I and it contains the last modified time of this specific file.
Now if you look into the I'll copy this key and in my Chrome browser if you look into the about cache.
You see, this specific file is already cached.
Now, whenever I enter this in my URL, the entry should be retrieved from the cache itself.
However, in order to make sure that the older file is not sent, what browser will do, what the browser
cache will do is it will send a get request to the server with a if modified since header.
So let's try this out.
Just remember that this file was modified on Sunday 18th October 2015.
Quite old file.
So this is basically a PNG file.
We are actually recording this lecture at the end of 2017.
But anyways, so let's do one thing.
I'll do a tail on var log nginx access dot log.
And now let's open this file up and let's refresh.
Okay, Now, if we look into the
Enginex access log, what do you see?
You see A304.
So the reason why you're seeing A304 is because my browser cache, my browser cache asked It sent a
get request on the file name asking whether it was modified.
Since now since the file was not modified, the web server sent A304 not modified response back.
So this is the basic let let me show you this output in tcpdump.
So I do a tcpdump hyphen, a hyphen I zero and on port 80.
Now this will give you a much more clear idea just remember, this is the last modified time.
Now I'll start my tcpdump capture.
Okay, so the capture has started.
I'll refresh.
I'll refresh once and again.
The browser must have sent a if modified since header.
Let's see.
And here if you see this is basically the let me go up.
Let's go.
Perfect.
So this is basically the file.
So now you see, this is the if modified since header where the browser is asking whether this file
you see the get request is get Docker dot jpg because we actually requested for Docker dot jpg and now
there is a header which browser has added saying whether this file has been modified since this specific
time.
So whenever a browser or a cache server stores a file, this specific file is stored in the cache.
It will note the last modified date, so it will note this specific date and whenever the cache server
will send a request to the web server in the if modified, it will append the date of the last modified.
So since the last modified is 18th October 2015, it has appended that specific date and said whether
this file is modified since this specific time.
Now, if it is not modified, then the web server.
If you look into the next response, the web server has sent that this specific file is not modified
after this specific date.
And now since my cache or a web browser now received a 304 response, what it will do is it will serve
the file from the cache itself.
Now, one important thing to remember is that whenever a web server sends a 304 not modified, it will
not send a file.
It will just send a response without a file.
So this is the basics about the if modified since header.
I hope you got the basic understanding on the importance of if modified since header.
I hope this lecture has been informative for you.
Again, go ahead and try this out because practical is very important and if you have any issues you
can let us know in our forums.
So this is it.
If you have any questions, doubts, suggestions, feel free to connect us at Twitter, Facebook or
LinkedIn or email us at instructors at the Rate Labs dot n.
Thanks for watching.