Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to configure location in Nginx server

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

Most people do not understand the knowledge points of this article "how to configure location in the Nginx server", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to configure location in the Nginx server" article.

Grammar

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

Rules

Indicates an exact uri match (interested students can take a look at the difference between url and uri)

~: indicates a case-sensitive regular match

~ *: indicates a case-insensitive regular match

! ~ & &! ~ *: represents a case-sensitive mismatch rule and a case-insensitive mismatch rule

/: universal match, any request will match to

Location matching target

The location matching test uses only the part of the request uri, not the parameter part. (reason: parameters are written in too many ways to match exactly)

Location matching order

Under the premise of multiple location configurations, the matching order of location (unverified, hehe, searched on google)

1. First match =

two。 Second, match ^ ~

3. Secondly, regular matching is carried out according to the order of configuration files,

4. Finally, it is handed over to / for universal matching.

Note:

When a match is successful, stop the match immediately and process the request according to the current matching rules

Demo example

The nginx configuration file is divided into three hierarchical structures from bottom to top:

| | 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) to match a specific uri. In addition to simple strings (such as file system paths), more complex matching patterns (pattern) are allowed.

The basic grammatical form of location block is:

Location [= | ~ | ~ * | ^ ~ | @] pattern {...}

[= | ~ * | ^ ~ | @] is called location modifier, which defines how nginx matches the following pattern, as well as the most basic attributes of the pattern (simple string or regular expression).

About location modifier

1. =

This exactly matches the specified pattern, and the pattern here is restricted to simple strings, which means regular expressions cannot be used here.

Example:

Server {server_name jb51.net; location = / abcd {[…] }}

Match:

Http://jb51.net/abcd # is a perfect match

Http://jb51.net/abcd # if the system running nginx server itself is case-insensitive, such as windows, then it also matches

Http://jb51.net/abcd?param1?m2 # ignores the query string parameter (query string arguments). Here is the? param1?m2 after / abcd

Http://jb51.net/abcd/ # does not match because there is a backslash (trailing slash) at the end, which nginx does not consider an exact match

Http://jb51.net/abcde # does not match because it is not an exact match

2. (none)

You don't have to write location modifier, nginx can still match pattern. In this case, match the uri that starts with the specified patern. Note that the uri here can only be a normal string, not a regular expression.

Example:

Server {server_name website.com; location / abcd {[…] }}

Match:

Http://jb51.net/abcd # is a perfect match

Http://jb51.net/abcd # if the system running nginx server itself is case-insensitive, such as windows, then it also matches

Http://jb51.net/abcd?param1?m2 # ignores the query string parameter (query string arguments). Here is the? param1?m2 after / abcd

The existence of a backslash (trailing slash) at the end of http://jb51.net/abcd/ # is also within the matching range.

Http://jb51.net/abcde # still matches because uri starts with pattern

3. ~

The location modifier is case sensitive, and the pattern must be a regular expression

Example:server {server_name jb51.net; location ~ ^ / abcd$ {[…] }}

Match:

Http://jb51.net/abcd # exact match

Http://jb51.net/abcd # does not match, ~ is sensitive to case

Http://jb51.net/abcd?param1?m2 # ignores the query string parameter (query string arguments). Here is the? param1?m2 after / abcd

Http://jb51.net/abcd/ # does not match because there is a backslash (trailing slash) at the end and does not match the regular expression ^ / abcd$

Http://jb51.net/abcde # does not match the regular expression ^ / abcd$

Note: for systems that are case-insensitive, such as windows, ~ and * don't work, mainly because of the operating system.

4. ~ *

Similar to ~, but this location modifier is not case-sensitive, and pattern must be a regular expression

Example:

Server {server_name website.com; location ~ * ^ / abcd$ {[…] }}

Match:

Http://jb51.net/abcd # exact match

Http://jb51.net/abcd # matches, which is its case-insensitive feature

Http://jb51.net/abcd?param1?m2 # ignores the query string parameter (query string arguments). Here is the? param1?m2 after / abcd

Http://jb51.net/abcd/ # does not match because there is a backslash (trailing slash) at the end and does not match the regular expression ^ / abcd$

Http://jb51.net/abcde # does not match the regular expression ^ / abcd$

5. ^ ~

The matching situation is similar to that of 2. (none), where the uri that begins with the specified matching pattern is matched, except that once the match is successful, the nginx stops looking for other location blocks to match (related to the location matching order)

6. @

Used to define a location block that cannot be accessed by an external client and can only be accessed by nginx internal configuration instructions, such as try_files or error_page

Demo example

The results are as follows:

Access the root directory /, match to location /

Visit other php programs except hello.php, match to location ~\ .php $, and run it with php5-fpm

Visit hello.php, match to location = / hello.php, and the visit is redirected to the official website of good contacts.

The above is about the content of this article on "how to configure location in the Nginx server". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report