Detail the Nginx Location configuration

  • 2020-05-14 06:10:14
  • OfStack

Today, one of you asked about Nginx's site multi-path matching.

[

. 1. www. domain com/a needs to return/var www/domain com/a/index html
. 2. www. domain com/b needs to return/var www/domain com/b/index html
How do I configure Nginx to take effect?

]

To solve this problem, the first response is to directly use the location directive of Nginx, but before we give an answer, let's look at the basis of the Nginx location directive.

The Nginx block configuration concept

In the Nginx configuration file, two commonly used blocks (Block) are used for setting:

1. Server block

2. Localtion block

[

The block here refers to Block, which you can even understand as the configuration content between the 1 pair of {}.

]

The Sever block is mainly the configuration of the real host, such as the configuration of the host's domain name, IP, port and so on. Of course, in an Nginx configuration file, we can specify the configuration of multiple Sever blocks.

The Location block is subdivided into configurations for different paths and requests within the Sever block. Since URI is usually very large in one site, you can also write multiple Location configurations in the Location block Settings section.

Let's look at the basic syntax of the Location configuration first:


location optional_modifier location_match {
 #  this  {}  That's what the configuration is 1 A block  Block
}

The optional_modifier configuration item above is available using regular expressions. The common ones are as follows:

Left blank. Yes, blank is also one way to set. In the blank case, the configuration indicates that the request path begins with location_match. =, equal sign is very easy to understand: that is, the request path is exactly equal to the value of location_match; It's different from leaving the first term blank. ~, wave signal (note the English input wave signal) represents case-sensitive regular matching. ~* represents case-insensitive regular matching. ^~ means you don't want a regular match to happen.

Nginx handles the order of the Location block

You learned the basic concepts and common configurations of the location directive above. Let's look again at the order in which Location takes effect! This is also important:

After each request comes into Nginx, Nginx will select the best match of Location for response. The specific process of processing is to compare location's configuration with that of Nginx one by one. This step can be divided into the following steps:

Start with a prefix match (location optional_modifier is null).

Nginx then looks for an exact location configuration based on URI (that is, location with optional_modifier =). If there is still no match, match the ^~ configuration first. If one is found, the search will stop and the response will be returned. If a match is still not found, a case-sensitive regular match is performed, followed by a case-insensitive regular match.

Some examples of the Nginx Location configuration:

It is useless to say more. After looking at so many theories, it is useless to have no specific examples to support, so let's take a look at 1 specific configuration example:


location = / {
 # =  Equals configurator, only matches  /  The routing 
}

location /data {
  #  Leave the configuration blank, there will be a match  /data  The initial route, the subsequent matches will be matched down. 
}

location ^~ /img/ {
 #  Pay attention to  ^~  Configuration, and here it matches to  /img/  If you start, you just go back. 
}

location ~* .(png|gif|ico|jpg|jpeg)$ {
 #  Match with  png, gif, ico, jpg or jpeg  A closing request; This is usually used to set the image's request response.  
}

Two very practical examples:

1. Simple pictures are hotlinking proof


location ~ .(png|gif|jpe?g)$ {
 valid_referers none blocked yourwebsite.com *.yourwebsite.com;
 #  Just make sure you put your domain name on it  
 if ($invalid_referer) {
   return  403;
 }
}

2. For 1 writable path, forbid php or js footsteps to execute


location ~* /(media|images|cache|tmp|logs)/.*.(php|jsp|pl|py|asp|cgi|sh)$ {
 return 403;
}

The answer to the question

Finally, the answer to the question could look something like this:


location /a {
  root /var/www/domain.com/a;
}

location /b {
  root /var/www/domain.com/b;
}


Related articles: