Let's discuss how we can make the configuration for our demo site more robust by adding location directives.
Location directives allow us to extend our configuration based on URI or a uniform resource indicator
that makes up the request being processed by the server.
When we define a location, we use the location keyword, an optional modifier and a required location
definition.
Location directives are formatted as blocks and are defined inside server blocks.
They can also be nested inside other location blocks.
Inside the location block, we can include other directives.
Specifically, we can include just about any directive that can be defined inside a server block.
This makes locations very useful since we can configure Engine X to process different requests in ways
similar to server blocks without having to create additional servers.
When Engine X processes locations, it uses a series of matches to determine the best one to use.
First, considering exact matches, then locations with prefixes and then locations with regular expressions.
For more information on locations, take a look at the documentation on engine x dot org.
Let's continue by going back to our development server as the root user and editing the configuration
for our demo site.
Let's add a new location block for an empty request or just the slash character.
This will correspond to the root directory of our site.
Inside this block, we'll add another directive Tree files.
The Tree Files directive gives engine X a list of files or directories to look for relative to the location,
The first file or directory that matches gets processed.
If no items in the list match, then the last item in the list is used as a URI or an error code.
For this location we'll use the URI variable to first look for a file that matches the URI being processed.
Then we'll add the URI variable followed by a slash to test for any directories that might match.
And lastly, we'll add equals 404 to tell engine X to serve a 404 error if no file or directory matches
the URI being requested.
Let's add another location for the images directory and in this location let's add the directive Auto
index and use the keyword on the auto index directory.
Will let the browser list the files in that directory.
This means we'll be able to see the files in that directory from a browser.
This behavior is turned off by default.
And now let's add a couple more directives and locations for custom error pages.
When Engine X can't find any content to respond to a request, it displays a 404 error page.
And if there are any server side issues, Engine X will display a 500 error page.
In these cases we've added an error page directive to tell engine X to use our custom pages instead
of the defaults.
For these custom pages, we've also added a location directive with an equal sign, also known as the
exact match modifier.
And inside the location block, we've added the internal directive which tells Engine X to process any
redirects to the custom 404 page as an internal redirect.
Both of these, the exact match modifier and the internal redirect will help Engine X serve our custom
error page more quickly.
Testing the 404 page will be easy.
We can just request a page that we know our site isn't set up to serve.
But since we don't have any server side processing in place, we need to add a specific location to
test our 500 page.
To do this, we can create a fake fast CGI pass that we know will fail.
Okay, now let's save the file and exit and test the configuration with engine x dash dt.
The configuration looks good so we can load it with system CTL.
Reload.