WEBVTT

00:04.500 --> 00:10.260
In this lesson we're going to talk about using location blocks in the engine configuration.

00:10.260 --> 00:15.630
This is the most used context in any engineered configuration and it's how will define and configure

00:15.660 --> 00:19.750
the behaviour of specific you are our eyes or requests.

00:19.770 --> 00:25.530
Think of location blocks as intercepting a request based on its value and then doing something other

00:25.530 --> 00:32.410
than just trying to serve a matching file relative to the root directory as we saw in the previous lesson.

00:32.550 --> 00:38.610
This standard behaviour is great for static resources such as our style sheet to this image etc..

00:38.880 --> 00:46.290
But if we request for example slash greet or any other you are my engine x cannot match a file and instead

00:46.290 --> 00:49.050
serves this default foro for page.

00:49.170 --> 00:55.410
So let's see how to use a custom location context and intercept that greet request and give it some

00:55.410 --> 00:59.410
hypothetical purpose back to the engine X configuration file.

00:59.410 --> 01:03.300
I'll add a location context inside the server context.

01:03.540 --> 01:08.650
But this time the context takes a parameter before opening the context braces.

01:08.790 --> 01:11.700
This parameter being the you are right to match.

01:11.700 --> 01:18.660
So in our case slash greet meaning we can now respond worth redirect or do whatever else we need to

01:18.660 --> 01:22.920
do with this request and essentially manipulate the response.

01:22.920 --> 01:29.070
Let's just return a text string for now say return which takes two arguments.

01:29.130 --> 01:34.770
The response code and the response data to return to the client 200.

01:34.770 --> 01:40.780
So okay with a response body of hello from Engine X greet location.

01:42.590 --> 01:45.560
Save and reload the configuration.

01:47.850 --> 01:48.440
Refresh.

01:48.460 --> 01:49.660
Greet.

01:49.690 --> 01:50.640
And as expected.

01:50.640 --> 01:53.790
We get that string return to us now.

01:53.820 --> 01:58.920
There are several ways of matching you our eyes on location blocks and the one we just used is called

01:58.950 --> 02:00.390
the prefix match.

02:00.480 --> 02:02.010
Meaning that it matches a request.

02:02.010 --> 02:05.250
You are I starting with Slash greet.

02:05.370 --> 02:11.450
So in other words slash greeting is also matched or even another path.

02:11.580 --> 02:14.640
Literally anything starting with Slash greet.

02:15.590 --> 02:17.630
Should we however not want this behaviour.

02:17.630 --> 02:19.880
We can also do an exact match.

02:20.610 --> 02:24.660
By adding the equal sign or modifier before the pattern.

02:25.530 --> 02:28.410
Change that response to show us the update.

02:29.990 --> 02:32.030
Reload the configuration.

02:32.760 --> 02:34.360
Refresh.

02:34.500 --> 02:38.460
And now we get a 4 0 4 as only slash greet is matched.

02:38.490 --> 02:45.240
And this one no longer exists but if we simply navigate to slash greet we get that new message from

02:45.240 --> 02:47.160
the exact match location.

02:47.560 --> 02:53.280
Okay so prefix and exact matches are great but we can go one step further and matsue our eyes with a

02:53.280 --> 02:54.880
regular expression.

02:54.900 --> 03:00.570
This being the reason for installing the PCR library when we installed engine X this time we'll use

03:00.630 --> 03:06.990
a told the character as the match modifier to indicate that this is a regular expression match.

03:07.020 --> 03:12.810
I'll again do a match for Slash greet and let's say any number from 0 to 9.

03:12.840 --> 03:20.880
After that used this same return statement with a new message safe reload the configuration.

03:23.130 --> 03:27.290
Still at slash greet We now get a foro for as of course that matches.

03:27.300 --> 03:33.550
Also looking for a number from 0 to 9 after slash greet add a number to the you are I however.

03:33.570 --> 03:36.380
And again it works very importantly.

03:36.420 --> 03:40.110
Note that this told a modifier is case sensitive.

03:40.530 --> 03:45.100
So if we change one of these characters to an upper case like so.

03:45.970 --> 03:47.600
We're back to a foro for.

03:49.990 --> 03:56.350
To match case insensitive then so disregard that you are right case all together we can simply add an

03:56.440 --> 03:59.780
asterisk after that older like so.

04:01.050 --> 04:02.420
With a new message.

04:03.690 --> 04:11.360
Save reload dry slash greet with the uppercase character Ghen and now it works.

04:11.360 --> 04:13.830
Engine exercise is a priority to each of these.

04:13.830 --> 04:20.140
Location match modifiers with a regular expression being more important than a prefix match.

04:20.160 --> 04:23.730
We can test this by uncomment ing the original prefix.

04:23.730 --> 04:26.730
Make their capital G too.

04:26.790 --> 04:32.700
Exactly as we have it in the browser whilst also leaving the case insensitive regular expression match

04:33.360 --> 04:34.350
safe.

04:36.400 --> 04:43.090
Reload and the regular expression matched all ones regardless even if the order in which these location

04:43.090 --> 04:50.780
blocks are configured the final modifier is what's called preferential prefix which we write as carit

04:51.040 --> 04:52.440
told her.

04:52.540 --> 04:58.720
This is essentially the same as the basic prefix modifier only more important than a regular expression

04:58.720 --> 05:01.470
match save and reload.

05:03.530 --> 05:06.540
The prefix wins over the regular expression.

05:06.590 --> 05:11.420
It's important to understand the order of priority in which engine X matches requests.

05:11.420 --> 05:14.230
So to sum up they are as follows.

05:14.360 --> 05:16.600
Number one is the exact match.

05:16.760 --> 05:22.310
Should an exact match exist for a you are right that will always be the location block used regardless

05:22.310 --> 05:24.620
of any other matching location block.

05:24.620 --> 05:27.340
Second is the preferential prefix.

05:27.350 --> 05:33.520
Again this is the same as the basic prefix match but have priority over regular expression matches.

05:33.530 --> 05:36.510
Third in equal order is the regular expression.

05:36.510 --> 05:43.010
Matches should however both a case sensitive and case in sensitive location match your request.

05:43.040 --> 05:45.070
Not that this should ever really happen.

05:45.170 --> 05:51.220
The first block will be used as engineers has no other way of prioritizing one over the other.

05:51.470 --> 05:57.090
And finally the lowest of the lot is the very basic prefix matched that we looked at first.

05:57.230 --> 06:01.820
Any other match from the variations above will be prioritized over this one.

06:02.320 --> 06:07.990
That covers the basics of creating location context and matching you are rice in the next list and will

06:07.990 --> 06:10.700
cover the basics of Engineers variables.
