Configuration method of Location in Nginx
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/a
needs to return/var/www/domain.com/a/index.html
2.www.domain.com/b
Need 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 above optional_modifier
configuration items can use regular expressions. The commonly used ones are as follows:
Leave
blank. Yes, leaving it blank is also a way to set it. When left blank, the configuration indicates that the request path starts withlocation_match
.=
, 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.~
, the floating number (note that it is the floating number entered in English) indicates case-sensitive regular matching.~*
represents case-insensitive regular matching.^~
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:
First match the
prefix
(that is, the configuration where the optional_modifier of the location is empty).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
=
).-
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. 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 of php
or js
steps
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

Starting an Nginx server requires different steps according to different operating systems: Linux/Unix system: Install the Nginx package (for example, using apt-get or yum). Use systemctl to start an Nginx service (for example, sudo systemctl start nginx). Windows system: Download and install Windows binary files. Start Nginx using the nginx.exe executable (for example, nginx.exe -c conf\nginx.conf). No matter which operating system you use, you can access the server IP
