In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Today, a classmate asked about the multi-path matching of Nginx's site.
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?
The first way to solve this problem is to use Nginx's location instruction directly, but before we give an answer, let's take a look at the basics of the Nginx location instruction.
Nginx block configuration concept
In the configuration file of Nginx, two commonly used Block are usually used to set up:
1.Server block
2.Localtion block
The block here refers to Block, which you can even understand as the configuration between the later pair {}.
The Sever block is mainly about the configuration of the real host, such as the domain name, IP, port and so on. Of course, in a Nginx configuration file, we can specify the configuration of multiple Sever blocks.
The Location block is subdivided into configurations for different paths and requests in the Sever block. Because there are usually a lot of URI in a site, you can also write multiple Location configurations in the Location block settings section.
Let's take a look at the basic syntax of Location configuration:
The configuration content in location optional_modifier location_match {# this {} is a block Block}.
The above optional_modifier configuration items can use regular expressions. Several commonly used ones are as follows:
Leave it blank. Yes, leaving blank is also a way to set up. In the case of leaving blank, the configuration indicates that the request path starts with location_match. The equals sign is still very easy to understand: the request path is exactly equal to the value of the following location_match, which is different from leaving the first blank. ~, the drift sign (note that it is entered in English) indicates a case-sensitive regular match. ~ * indicates a case-insensitive regular match. ^ ~ means that you don't want regular matching to happen here.
The order in which Nginx processes Location blocks
The above learned 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 important:
After each request enters Nginx, Nginx will select the best match of Location to respond. The specific process is to compare it with the configuration of location one by one. This step can be divided into the following steps:
Start with a prefix match (that is, a configuration with an empty optional_modifier for location).
Nginx then looks for an exact match of the location configuration based on URI (that is, the optional_modifier of location =). If there is still no match, match the ^ ~ configuration first. if a configuration is found, the search process will be stopped and the response content will be returned directly. If a match is still not found, a case-sensitive regular match is performed first, followed by a case-insensitive regular match.
Some examples of Nginx Location configuration:
It is useless to say too much. After reading so many theories, it is useless without specific examples, so let's take a look at specific configuration examples:
Location = / {# = equal sign configuration, only match / this route} location / data {# left blank configuration, will match the route starting with / data, and the subsequent matches will match down. } location ^ ~ / img/ {# pay attention to the configuration of ^ ~. If the match starts with / img/, it will be returned directly. } location ~ *. (png | gif | ico | jpg | jpeg) ${# matches requests ending with png, gif, ico, and jpg or jpeg; this is usually used to set the request response for images. }
Two very practical examples:
1. Simple picture hotlink protection
Location ~. (png | gif | jpe?g) ${valid_referers none blocked yourwebsite.com * .yourwebsite.com; # just write your domain name on it. If ($invalid_referer) {return 403;}}
two。 Forbid the execution of php or js for some writable paths
Location ~ * / (media | images | cache | tmp | logs) /. *. (php | jsp | pl | asp | cgi | sh) ${return 403;}
The answer to the question
Finally, let's look at the answer to the question, which can look something like this:
Location / a {root / var/www/domain.com/a;} location / b {root / var/www/domain.com/b;}
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.