Home>Article>Backend Development> Configuration method of Location in Nginx

Configuration method of Location in Nginx

不言
不言 Original
2018-07-07 16:30:24 3773browse

This article mainly introduces the configuration method of Location in Nginx. It has certain reference value. Now I share it with you. Friends in need can refer to it.

There is a person today A classmate asked about the multi-path matching of Nginx sites?

1.www.domain.com/aneeds to return/var/www/domain.com/a/index.html
2.www.domain.com/bNeed to return/var/www/domain.com/b/index.html
How to configure Nginx to make it effective?

To solve this problem, the first reaction is to directly use the Nginx location directive to solve it. However, before giving the answer, let us first understand the basics of the Nginx location directive.

Nginx block configuration concept

In the Nginx configuration file, two commonly used blocks (Blocks) are usually used for settings:

1.Server area Block
2.Localtion Block

The block here refers to Block. You can even understand it as the configuration content between the following pair of {}.

Sever block is mainly the configuration of the real host, such as configuring the domain name, IP, port and other contents of the host. Of course, in an Nginx configuration file, we can specify the configuration of multiple Sever blocks.

The Location block is inside the Sever block and is subdivided into configurations for different paths and requests. Because there are usually many URIs in a site, you can also write multiple Location configurations in the Location block settings.

Let’s take a look at the basic syntax of Location configuration first:

location optional_modifier location_match { # 这个 {} 里面的配置内容就是一个区块 Block }

The aboveoptional_modifierconfiguration items can use regular expressions. The commonly used ones are as follows:

  1. Leaveblank. Yes, leaving it blank is also a way to set it. When left blank, the configuration indicates that the request path starts withlocation_match.

  2. =, the equal sign is still very easy to understand: the request path is exactly equal to the value of the followinglocation_match; leave the first item There is still a difference in being empty.

  3. ~, the floating number (note that it is the floating number entered in English) indicates case-sensitive regular matching.

  4. ~*represents case-insensitive regular matching.

  5. ^~means that no regular matching is expected to occur here.

The order in which Nginx processes Location blocks

The above explains the basic concepts and common configurations of the location directive. Let’s take a look at the order in which Location takes effect! This is also very important:

After each request comes to Nginx, Nginx will choose the best match for the Location to respond. The specific process of processing is to compare it with the configuration of the location one by one. This step can be divided into For the following steps:

  1. First match theprefix(that is, the configuration where the optional_modifier of the location is empty).

  2. Nginx will then look for the exact matching location configuration based on the URI (that is, the configuration where the optional_modifier of the location is=).

  3. If there is still no match, then match the^~configuration first. If a configuration is found, the search process will be stopped and the response content will be returned directly.

  4. If no match is found, case-sensitive regular matching will be performed first, and then case-insensitive regular matching will be performed.

Some examples of Nginx Location configuration:

It’s useless to talk more. After reading so many theories, it’s useless without specific examples to support it, so let’s take a look at the specific ones. Configuration examples:

location = / { # = 等号配置符,只匹配 / 这个路由 }
location /data { # 留空配置,会匹配有 /data 开始的路由,后续有匹配会往下匹配。 }
location ^~ /img/ { # 注意 ^~ 配置,这里匹配到 /img/ 开始的话,直接就返回了。 }
location ~* .(png|gif|ico|jpg|jpeg)$ { # 匹配以 png, gif, ico, jpg or jpeg 结尾的请求;这个通常用来设置图片的请求响应。 }

Two very practical examples:

1. Simple image hotlink prevention

location ~ .(png|gif|jpe?g)$ { valid_referers none blocked yourwebsite.com *.yourwebsite.com; # 注意上面写上你的域名就好 if ($invalid_referer) { return 403; } }

2. For some writable path, prohibiting the execution ofphporjssteps

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

Answer to the question

Finally, let’s look at the answer to the question, which can be similar to It looks like this:

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

The above is the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

Use nginx to deploy multiple Web Servers on one server

The above is the detailed content of Configuration method of Location in Nginx. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn