1 00:00:04,510 --> 00:00:10,210 In this new section of the course we'll cover some useful modules and directives outside of the fundamental 2 00:00:10,240 --> 00:00:12,040 engine configuration. 3 00:00:12,280 --> 00:00:18,190 And with the overall focus of the Course leaning toward Web servers a good starting point is configuring 4 00:00:18,190 --> 00:00:20,050 expires headers. 5 00:00:20,050 --> 00:00:25,540 Now if you're not familiar with expires headers they're essentially a response header informing the 6 00:00:25,540 --> 00:00:30,380 client or browser how long it can cached that response for. 7 00:00:30,430 --> 00:00:37,780 So for example let's say we have an image or more specifically a photo on our website that photo data 8 00:00:37,820 --> 00:00:43,810 isn't realistically going to change all that often meaning we can tell the browser to cache a copy of 9 00:00:43,810 --> 00:00:46,560 the photo for a relatively long time. 10 00:00:46,720 --> 00:00:53,860 And in doing so avoid any future requests for that photo drastically improving Web site load times and 11 00:00:53,950 --> 00:00:58,660 often overlooked a reduction in requests to our server. 12 00:00:58,660 --> 00:01:03,460 Another resource like a style sheet however which could very well change more frequently. 13 00:01:03,460 --> 00:01:08,230 We could say to a shorter time but still have the browser cache it none the less. 14 00:01:08,560 --> 00:01:14,320 Over to the configuration file as we lifted before and first thing I'll do is clean up some of these 15 00:01:14,320 --> 00:01:19,270 directives from the previous lesson then before we can figure out a way expires. 16 00:01:19,270 --> 00:01:25,540 Let's also as part of this lesson see how to set a generic response header in this thumb. 17 00:01:25,540 --> 00:01:27,310 Don't be in G location. 18 00:01:27,310 --> 00:01:35,830 I'll use the built in add header directive with the first argument being the header name my header and 19 00:01:35,830 --> 00:01:36,520 the second. 20 00:01:36,550 --> 00:01:37,720 The header value. 21 00:01:37,930 --> 00:01:39,610 Hello world. 22 00:01:39,610 --> 00:01:44,780 Save this reload the configuration and now to test this. 23 00:01:44,800 --> 00:01:51,290 We can make a cold request to thump or be in G with the flag for headers. 24 00:01:51,310 --> 00:01:56,090 I'll paste that you are L. enter and there we have it in the response headers. 25 00:01:56,140 --> 00:01:58,470 My header said to Hello World. 26 00:01:58,780 --> 00:02:05,020 So knowing how to said headers we can configure this location as a typical static resource by first 27 00:02:05,020 --> 00:02:12,940 setting the cache control header to public telling the receiving client that this resource or response 28 00:02:13,270 --> 00:02:15,510 can be cached in any way. 29 00:02:15,670 --> 00:02:18,970 Then the pragma header also to public. 30 00:02:18,970 --> 00:02:26,020 Essentially just an older version of this cache control header then else relate it but equally important 31 00:02:26,020 --> 00:02:34,550 header vary meaning the content of this response can vary with the value being xcept encoding. 32 00:02:34,570 --> 00:02:40,000 This is something we'll cover in more depth in the next lesson but essentially saying the response can 33 00:02:40,000 --> 00:02:44,890 vary based on the value of the request header except encoding. 34 00:02:45,190 --> 00:02:48,850 And finally to set the expires head up on this response. 35 00:02:48,850 --> 00:02:53,640 Engine X provides us a very convenient directive called expires. 36 00:02:53,650 --> 00:02:58,980 We could of course have said this header manually but there would be no logical way of setting a duration 37 00:02:59,020 --> 00:03:02,860 as the expires hid his value which needs to be a date. 38 00:03:02,890 --> 00:03:10,870 So with this expires directive we can simply sit a standard engine X duration for example 60 M which 39 00:03:10,870 --> 00:03:19,040 is 60 Minutes the same unit notations we used with the time out directives or 1 h for one hour. 40 00:03:19,360 --> 00:03:20,200 Or finally. 41 00:03:20,200 --> 00:03:22,400 In this case one m. 42 00:03:22,450 --> 00:03:25,270 Which in uppercase form is month. 43 00:03:25,420 --> 00:03:28,890 With this now you can see better why sit in an actual date. 44 00:03:28,900 --> 00:03:29,890 Wouldn't work. 45 00:03:30,070 --> 00:03:34,800 Let's say you sit in expires header using the ADD header directive with a date 1. 46 00:03:34,830 --> 00:03:40,990 Our way in the future that date would change in the configuration meaning in our time that date would 47 00:03:40,990 --> 00:03:43,820 be in the past whereas with this directive. 48 00:03:43,840 --> 00:03:47,500 Engine X sends the expires head of value as a date. 49 00:03:47,500 --> 00:03:51,070 This amount of time in the future for every request. 50 00:03:51,660 --> 00:03:55,290 Save this so we can test it reload. 51 00:03:55,470 --> 00:04:02,670 Make that same call request again and this time we get an expires header with the date set to exactly 52 00:04:02,670 --> 00:04:09,210 one month ahead of this recording including those additional headers we said and another cache control 53 00:04:09,210 --> 00:04:12,270 header added by the expires directive. 54 00:04:12,480 --> 00:04:13,780 Max h. 55 00:04:13,800 --> 00:04:19,650 With this being the number of seconds in 30 days or one month as we set it. 56 00:04:20,040 --> 00:04:26,160 This is essentially all this to configuring expires headers and given these new headers a browser will 57 00:04:26,160 --> 00:04:33,420 now cache this thumb or PRNG image locally meaning on the user's machine for this amount of time before 58 00:04:33,420 --> 00:04:39,570 requesting it from our server again before we finish up however let's see what a more typical location 59 00:04:39,570 --> 00:04:42,930 for such static resources might look like. 60 00:04:42,990 --> 00:04:49,920 First up I'll change this to be a case insensitive regular expression match looking to any request ending 61 00:04:49,920 --> 00:04:51,180 with an extension. 62 00:04:51,180 --> 00:04:55,380 So dot C S S J S J. 63 00:04:55,380 --> 00:04:59,150 B G or j peg or P E N G. 64 00:04:59,220 --> 00:05:03,350 You can of course add a bunch more year but this is the general idea. 65 00:05:03,690 --> 00:05:10,810 And not forgetting access underscore log off unless you really want to log each and every request. 66 00:05:11,010 --> 00:05:19,920 Save this reload the configuration and this time given that new location try style dotsie SS instead. 67 00:05:20,890 --> 00:05:24,570 Which now responds with those exact same expires headers. 68 00:05:24,880 --> 00:05:28,890 A simple but incredibly valuable addition to any Web server.