Nginx – Virtual Hosts (Part 2)

Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"

Nginx – Virtual Hosts (Part 2) Enjoy it!

Introduction

This is a continuation of my previous article. So you might want to check that out first. In this article, we’re going to expand upon our existing configuration and explore some of the features that nginx provides.

Nginx – Virtual Hosts – Location Blocks

The location directive is used to perform actions as per the given URI. It’s declared inside the server context. In my case, I have defined my server’s name as openbsd.local. Now, if I define a location directive as:

location /home {
    // some actions here
}

Then nginx will match this block when the URI is openbsd.local/home. With location blocks, we can configure the behaviour of our site for a particular URI or request.

Returning a Response

The simplest type of action we can take after matching a URI is return a string response, which the done with the return statement. This takes two arguments: the response status and the response string.
For example, if we have the following inside our location block:

 location /home {
            return 200 'You are at /home';
}

Then the server will send just a plain response when we visit openbsd.local/home.

Response at /home

Note that the URI we specified previously specifies that the URI needs only to start with /home.

Prefix match

This is something called prefix match. Nginx only matches the prefix of the URI. However, it also has other matching methods.

Matching Methods

Nginx supports mainly four types of matches:

  • Exact Match
  • Preferential Prefix Match
  • Regex Match
  • Prefix Match

I’ve listed those in the order of precedence. The first one has the highest precedence. I have demonstrated the prefix match, so now let’s look into the other types.

Regex Match

We configured nginx with the PCRE library which allows us to use some regular expressions in our configuration. We can use the tilde (~) character after the location keyword to specify that the URI is a regular expression. For example, if we wanted to match URIs like /ahome, /bhome, etc. then we can do so with the following configuration:

location ~ /[a-z]home {
    return 200 'Matched /[a-z]home\n';
}
Regex match

Note that this is a case-sensitive match. To match case-insensitively, use the ~* character instead of ~.

Case-insensitive Regex Match
Preferential Prefix Match

The Prefix Match has less priority than the Regex Match. The Preferential Prefix Match is just like Prefix Match except it has more priority over the regex one. A preferential match is specified using the ^~ character.
Note that, the preferential match doesn’t necessarily have to be with prefix matches whatsoever.
The following example should clarify the usage as well effect of a preferential prefix match.

location ^~ /ahome {
        return 200 'You are at /home - prefix\n';
}

location ~* /[a-z]home {
        return 200 'You are at /home - regex\n';
}

Since the prefix one has higher precedence, we’ll get the first response for URIs like/ahome, /ahomexyz , etc (prefix match) but the regex will still match against the ones that the previous doesn’t cover.

Precedence of Regex vs Prefix Match
Exact Match

Finally we have the exact match, which – as the name implies – matches the URI names exactly. This is specified with the = character.
The following would match only /home and return an error otherwise.

location = /home {
    return 200 'You are at /home - exact\n';
}


Conclusion

We’ve covered the basics of the location block. In my next article, we’ll look into some of the programming language-like feature (variables, if-else, etc.) that nginx provides. Stay tuned and thank you for reading.

Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"
Pratik Devkota
Pratik Devkota
Software engineering student interested in UNIX (GNU/Linux, *BSD, etc.), security as well as free and open-source software.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest articles

Join us on Facebook