Home Operation and Maintenance Nginx How to configure location in Nginx server

How to configure location in Nginx server

May 14, 2023 pm 07:16 PM
nginx location

Syntax
location [=|~|~*|^~] /uri/ {...}

Rules
= : Indicates exact uri matching (interested students can take a look at the difference between url and uri)
~: Indicates case-sensitive regular matching
~*: Indicates case-insensitive regular matching
! ~ && !~*: Indicates case-sensitive non-matching regular and case-insensitive non-matching regular
/: Universal matching, any request will be matched to the

location matching target
The location matching test only uses the request uri part, not the parameter part. (Reason: There are too many ways to write parameters and cannot be matched accurately)

location matching order
Under the premise of multiple location configurations, the matching order of location (not verified, hey, google Searched above)
1. First match =
2. Secondly match ^~
3. Then follow the order of the configuration file for regular matching,
4. Finally, hand it over to / for general matching
Note:
When a match is successful, the matching will be stopped immediately and the request will be processed according to the current matching rules

Demo example

nginx configuration file is divided into three types from bottom to top Hierarchical structure:
| http block the protocol level
| server block the server level
v location block the requested uri

nginx allows users to define location block and specify a matching pattern (pattern) matches a specific uri. In addition to simple strings (such as file system paths), more complex matching patterns are also allowed.
The basic syntax form of location block is:

  location [=|~|~*|^~|@] pattern { ... }
Copy after login

[=|~|~*|^~|@] is called location modifier, which will define how nginx matches the following pattern , and the pattern's most basic attributes (simple string or regular expression).

About location modifier

1. =

This will completely match the specified pattern, and the pattern here is restricted into a simple string, which means regular expressions cannot be used here.
example:

server {
  server_name jb51.net;
  location = /abcd {
  […]
  }
}
Copy after login

Matching situation:
http://jb51.net/abcd # Exactly matches
http://jb51.net/abcd # If running nginx The server system itself is not case-sensitive, such as windows, so it will also match
http://jb51.net/abcd?param1?m2 # Ignore the query string arguments, here is the one after /abcd? param1?m2
http://jb51.net/abcd/ # Does not match because there is a trailing slash at the end, nginx does not consider this situation to be a complete match
http://jb51.net /abcde # Does not match because it is not a complete match

2. (none)
You can not write the location modifier, nginx can still match the pattern. In this case, match those uris starting with the specified pattern. Note that the uri here can only be ordinary strings, and regular expressions cannot be used.
example:

server {
  server_name website.com;
  location /abcd {
  […]
  }
}
Copy after login

Matching situation:
http://jb51.net/abcd # Exactly matches
http://jb51.net/abcd # If running nginx The server system itself is not case-sensitive, such as windows, so it will also match
http://jb51.net/abcd?param1?m2 # Ignore the query string arguments, here is the one after /abcd? param1?m2
http://jb51.net/abcd/ # The trailing slash at the end is also within the matching range
http://jb51.net/abcde # Still matches, because the uri is

3 starting with pattern. ~
This location modifier is case-sensitive, and pattern must be a regular expression

example:
server {
  server_name jb51.net;
  location ~ ^/abcd$ {
  […]
  }
}
Copy after login

Matching situation:
http://jb51.net/abcd # Exact match
http://jb51.net/abcd # Does not match, ~ is case sensitive
http://jb51.net/abcd ?param1?m2 # Ignore query string arguments, here is ?param1?m2
after /abcd http://jb51.net/abcd/ # Does not match because there is a backslash at the end (trailing slash), does not match the regular expression^/abcd$
http://jb51.net/abcde #Does not match the regular expression^/abcd$
Note: For some systems that are not case-sensitive, For example, in windows, ~ and ~* do not work. This is mainly due to the operating system.

4. ~*
与 ~ 类似,但这个 location modifier 不区分大小写,pattern 须是正则表达式
example:

server {
  server_name website.com;
  location ~* ^/abcd$ {
  […]
  }
}
Copy after login

匹配情况:
    http://jb51.net/abcd        # 完全匹配
    http://jb51.net/abcd        # 匹配,这就是它不区分大小写的特性
    http://jb51.net/abcd?param1?m2    # 忽略查询串参数(query string arguments),这里就是 /abcd 后面的 ?param1?m2
    http://jb51.net/abcd/    # 不匹配,因为末尾存在反斜杠(trailing slash),并不匹配正则表达式 ^/abcd$
    http://jb51.net/abcde    # 不匹配正则表达式 ^/abcd$

5. ^~
匹配情况类似 2. (none) 的情况,以指定匹配模式开头的 uri 被匹配,不同的是,一旦匹配成功,那么 nginx 就停止去寻找其他的 location 块进行匹配了(与 location 匹配顺序有关)

6. @
用于定义一个 location 块,且该块不能被外部 client 所访问,只能被 nginx 内部配置指令所访问,比如 try_files or error_page

演示实例

How to configure location in Nginx server

产生的效果如下:
访问根目录/,匹配到location /
访问除hello.php之外的其它php程序,匹配到location ~ \.php$,并且用php5-fpm去运行
访问hello.php,匹配到location = /hello.php,访问被重定向到好联系官网

The above is the detailed content of How to configure location in Nginx server. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to check the name of the docker container How to check the name of the docker container Apr 15, 2025 pm 12:21 PM

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 How to configure nginx in Windows Apr 14, 2025 pm 12:57 PM

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 check whether nginx is started How to check whether nginx is started Apr 14, 2025 pm 01:03 PM

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.

How to start containers by docker How to start containers by docker Apr 15, 2025 pm 12:27 PM

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".

How to configure cloud server domain name in nginx How to configure cloud server domain name in nginx Apr 14, 2025 pm 12:18 PM

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 create containers for docker How to create containers for docker Apr 15, 2025 pm 12:18 PM

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]

How to check nginx version How to check nginx version Apr 14, 2025 am 11:57 AM

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.

How to start nginx server How to start nginx server Apr 14, 2025 pm 12:27 PM

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

See all articles