WEBVTT

00:00.660 --> 00:08.190
In the last module we have already seen on why the Http expires and the Http date headers are useful

00:08.190 --> 00:10.740
as far as the http caching is concerned.

00:12.380 --> 00:20.120
So in this module we'll configure our Nginx machine to include the expires header for different file

00:20.120 --> 00:20.900
formats.

00:22.330 --> 00:27.460
So for our scenario, we have two file formats that we'll be using.

00:27.460 --> 00:30.560
One is dot txt and one is dot PNG.

00:30.580 --> 00:34.390
So essentially they are the text and image file format.

00:34.630 --> 00:39.190
And for each file format we will set a different expiry header.

00:40.280 --> 00:42.290
So let's see on how we can do that.

00:43.770 --> 00:50.040
So let me do a call on example.com slash logo dot PNG.

00:51.890 --> 00:53.150
Ah, so sorry.

00:54.260 --> 00:54.660
Pinch it.

00:55.430 --> 00:58.910
So if we see over here, the date header is included.

00:58.940 --> 01:04.490
However, there is no expires header for this particular file.

01:05.570 --> 01:08.870
Same goes for the sample dot txt.

01:11.860 --> 01:15.220
The date header is mentioned, but there is no expires header.

01:15.490 --> 01:20.440
So let's see on how we can configure the expires header for both of them.

01:22.350 --> 01:25.590
So I'll go to ATC Nginx.

01:27.670 --> 01:31.480
Conniff And let's open Conniff.

01:33.340 --> 01:35.650
Now, this is a simple configuration file.

01:36.340 --> 01:45.880
So let's add one more location directive that will basically specify for any image file on the website.

01:46.630 --> 01:50.470
Gash that image for, let's assume, 48 hours.

01:51.550 --> 01:53.230
So browser.

01:53.230 --> 01:59.710
When it opens image from example.com, it will cache it for 48 hours.

01:59.920 --> 02:00.820
So.

02:03.270 --> 02:04.860
Include location director.

02:06.090 --> 02:08.790
Say for any image.

02:10.590 --> 02:11.670
Say I'll say.

02:11.700 --> 02:12.480
PNG file.

02:13.080 --> 02:14.970
You can even include Jpeg.

02:16.780 --> 02:18.220
I'll say expires.

02:19.240 --> 02:20.110
48 hours.

02:21.830 --> 02:23.480
You also have to mention the.

02:26.620 --> 02:29.020
Root location for the images, which is.

02:29.440 --> 02:31.060
I'll just copy it from the here.

02:33.820 --> 02:34.120
Okay.

02:45.280 --> 02:45.850
Okay.

02:45.850 --> 02:56.800
So the expires directive basically tells that any file that ends with PNG extension, which is from

02:56.800 --> 03:02.830
this particular directory, the expires of that file should be 48 hours.

03:02.830 --> 03:10.450
So basically what nginx will do is Nginx will add a expires header which will expire after 48 hours

03:10.450 --> 03:12.310
of the time of response.

03:12.580 --> 03:16.690
So i'll save this particular and let's do next.

03:17.020 --> 03:17.530
Okay.

03:17.530 --> 03:18.970
Let's reload nginx.

03:21.180 --> 03:21.410
Awesome.

03:23.320 --> 03:26.740
So now if we do a call on Http.

03:28.190 --> 03:30.680
Example.com logo dot PNG.

03:32.570 --> 03:33.440
Then.

03:34.250 --> 03:37.400
Now you see an expired header has been introduced.

03:39.540 --> 03:42.150
Now, we have already mentioned 48 hours.

03:42.150 --> 03:45.930
So if we look into the date header, date header is.

03:47.180 --> 03:55.660
18th November, Wednesday at 110 and expires is 20th November at 110.

03:55.670 --> 04:01.040
So this is exactly 48 hours from the time the response has come.

04:02.030 --> 04:06.710
Now, if you also notice Enginex has also introduced the cache control header.

04:06.740 --> 04:10.280
Max hyphen h followed by the second.

04:11.540 --> 04:17.170
So this basically if you multiply, then it becomes 48 hours.

04:17.180 --> 04:25.490
So this second corresponds to the number of hours after which the image or the cache should be considered

04:25.490 --> 04:25.940
stale.

04:27.690 --> 04:35.910
Now we have already discussed expires header is specifically for the Http 1.0 protocol in the 1.1,

04:35.940 --> 04:45.750
the nginx or the Http has introduced a new header field which is cache control where the expiry is mentioned

04:45.750 --> 04:47.130
in seconds.

04:47.940 --> 04:49.320
So for.

04:51.000 --> 04:58.080
All the Http 1.1 client, they'll actually read the cache control directive and not the expires header

04:58.080 --> 04:58.960
field over here.

05:00.570 --> 05:07.260
So even if you don't mention explicitly the cache control nginx will automatically add when you add

05:07.260 --> 05:11.790
the expires field within the location directive that we have already added.

05:12.980 --> 05:16.280
Now, this is specifically for the image file.

05:16.280 --> 05:22.730
So if we go on sample dot text, still it is not showing any expires header.

05:22.730 --> 05:24.170
So we have to.

05:25.480 --> 05:27.820
Specify for the text file as well.

05:27.910 --> 05:40.720
So if I will have to add one more location directive, say anything which ends with dot txt route would

05:40.720 --> 05:41.440
be the same.

05:47.080 --> 05:48.550
I'll say expires.

05:48.580 --> 05:49.420
I'll say one hour.

05:52.100 --> 05:55.270
I'll save the file and I'll reload the enginex.

05:57.580 --> 06:02.770
Let's clear the screen and let's do a call again on example.com.

06:04.590 --> 06:05.970
Slash sample.txt.

06:09.840 --> 06:19.710
And now you see the expires header has been added and for ease of readability it is 3600 seconds, which

06:19.710 --> 06:20.760
counts to one hour.

06:20.910 --> 06:31.620
So it will tell the cache that this response can be considered as the latest response for one hour.

06:33.540 --> 06:42.850
Now going back to the web conf, you can actually specify the negative value as well.

06:42.870 --> 06:46.740
So if I say minus one over here and let's do reload

06:49.230 --> 06:50.880
and if we go.

06:54.980 --> 06:57.800
You see basically the.

06:58.940 --> 07:06.560
Okay, date was 140 and the expires is also 114.

07:06.560 --> 07:12.890
So basically this means that do not cache this particular response.

07:12.890 --> 07:15.440
So sample.txt should not be cached.

07:15.470 --> 07:21.080
So even if you see in the cache control, there is a new field which is no cache which has been introduced.

07:21.680 --> 07:30.710
So if you want to include or if you want to instruct the browsers to not cache this particular responses,

07:30.710 --> 07:35.900
then you can actually say expires minus one and this will do the job.

07:37.310 --> 07:45.080
So this is it about how you can include the expires header as far as the responses are concerned?

07:45.410 --> 07:47.530
So this is it about this module.

07:47.540 --> 07:51.350
I hope this has been informative for you and I'd like to thank you for viewing.
