In this new section of the course we'll cover some useful modules and directives outside of the fundamental
engine configuration.
And with the overall focus of the Course leaning toward Web servers a good starting point is configuring
expires headers.
Now if you're not familiar with expires headers they're essentially a response header informing the
client or browser how long it can cached that response for.
So for example let's say we have an image or more specifically a photo on our website that photo data
isn't realistically going to change all that often meaning we can tell the browser to cache a copy of
the photo for a relatively long time.
And in doing so avoid any future requests for that photo drastically improving Web site load times and
often overlooked a reduction in requests to our server.
Another resource like a style sheet however which could very well change more frequently.
We could say to a shorter time but still have the browser cache it none the less.
Over to the configuration file as we lifted before and first thing I'll do is clean up some of these
directives from the previous lesson then before we can figure out a way expires.
Let's also as part of this lesson see how to set a generic response header in this thumb.
Don't be in G location.
I'll use the built in add header directive with the first argument being the header name my header and
the second.
The header value.
Hello world.
Save this reload the configuration and now to test this.
We can make a cold request to thump or be in G with the flag for headers.
I'll paste that you are L. enter and there we have it in the response headers.
My header said to Hello World.
So knowing how to said headers we can configure this location as a typical static resource by first
setting the cache control header to public telling the receiving client that this resource or response
can be cached in any way.
Then the pragma header also to public.
Essentially just an older version of this cache control header then else relate it but equally important
header vary meaning the content of this response can vary with the value being xcept encoding.
This is something we'll cover in more depth in the next lesson but essentially saying the response can
vary based on the value of the request header except encoding.
And finally to set the expires head up on this response.
Engine X provides us a very convenient directive called expires.
We could of course have said this header manually but there would be no logical way of setting a duration
as the expires hid his value which needs to be a date.
So with this expires directive we can simply sit a standard engine X duration for example 60 M which
is 60 Minutes the same unit notations we used with the time out directives or 1 h for one hour.
Or finally.
In this case one m.
Which in uppercase form is month.
With this now you can see better why sit in an actual date.
Wouldn't work.
Let's say you sit in expires header using the ADD header directive with a date 1.
Our way in the future that date would change in the configuration meaning in our time that date would
be in the past whereas with this directive.
Engine X sends the expires head of value as a date.
This amount of time in the future for every request.
Save this so we can test it reload.
Make that same call request again and this time we get an expires header with the date set to exactly
one month ahead of this recording including those additional headers we said and another cache control
header added by the expires directive.
Max h.
With this being the number of seconds in 30 days or one month as we set it.
This is essentially all this to configuring expires headers and given these new headers a browser will
now cache this thumb or PRNG image locally meaning on the user's machine for this amount of time before
requesting it from our server again before we finish up however let's see what a more typical location
for such static resources might look like.
First up I'll change this to be a case insensitive regular expression match looking to any request ending
with an extension.
So dot C S S J S J.
B G or j peg or P E N G.
You can of course add a bunch more year but this is the general idea.
And not forgetting access underscore log off unless you really want to log each and every request.
Save this reload the configuration and this time given that new location try style dotsie SS instead.
Which now responds with those exact same expires headers.
A simple but incredibly valuable addition to any Web server.