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

Detailed explanation of nginx location configuration regular expression example

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

1.location introduction

Location is configured in the server block and is used to classify and handle different requests by matching the received uri, such as reverse proxy, fetching static files, etc.

There can be multiple location in the server block, which is sequential and will be processed by the first matching location

Localtion matching function is only used for matching distribution, and it will not change the content or other functions of uri. I confused some concepts when I first understood it. I suggest doing more tests to see the actual results.

2.localtion matching rule

Location [= | ~ | ~ * | ^ ~] uri {… }

Location @ name {… }

Note 1: rules cannot be mixed.

Note 2: the following examples are based on this server

Server {listen 8861; server_name abc.com;}

2.1 "=" exact match

The content must be exactly the same as the expression to match successfully.

Example:

Location = / {.} # only match http://abc.com# http://abc.com [match succeeded] # http://abc.com/index [match failed]

2.2 "~", case sensitive

Example:

Location ~ / Example/ {.} # http://abc.com/Example/ [match succeeded] # http://abc.com/example/ [match failed]

2.3. "~ *", ignore case

Example:

Location ~ * / Example/ {.} # ignores the case of the uri section # http://abc.com/test/Example/ [matches successfully] # http://abc.com/example/ [matches successfully]

2.4. "^ ~", which only starts with uri

Example:

Location ^ ~ / index/ {.} # requests that begin with / img/ will be matched with # http://abc.com/index/index.page [match succeeded] # http://abc.com/error/error.page [match failed]

2.5. "@", nginx internal jump

Example:

Location / index/ {error_page 404 @ index_error;} location @ index_error {.} # requests that start with / index/, if the status of the link is 404. Will match to the @ index_error rule.

2.6 No rules are added

When no rules are added, the default is case-sensitive and prefix matching, which is equivalent to adding "~" and "^ ~".

Only / indicates matching all uri

Location / index/ {.} # http://abc.com/index [match succeeded] # http://abc.com/index/index.page [match succeeded] # http://abc.com/test/index [match failed] # http://abc.com/Index [match failed] # matched to all urilocation / {.}

Summary

The above is the regular expression of nginx location configuration introduced by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to the website!

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

Servers

Wechat

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

12
Report