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

Analysis of matching priority of location configured by Nginx

2025-03-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Preface

The location in the server block in the Nginx configuration is used to match the request URI in order to process the impassable URI.

Location type and conditions for successful matching

Location = expression {} exact matching, only if URI and expression are exactly the same; location expression {} normal matching, as long as the characters before URI are the same as expression; location ^ ~ expression {}, as long as the characters before URI are the same as expression; location ~ regex {} regular matching (case-sensitive); location ~ * regex {} regular matching (case-insensitive)

The summary can be divided into three categories: accurate matching, general matching and regular matching.

Matching logic and priority

For a request to enter a Nginx, its URI may meet the matching criteria of multiple location, but the request will only be processed by one location logic (redirection is not counted), so there must be priority in the matching. The matching logic for Nginx is as follows:

1. Perform precise matching

If the request URI matches location = (meets the exact matching condition), the content of the location is executed and no other matches are made, otherwise see the next entry.

two。 Perform a normal match

Next, Nginx will compare URI with all ordinary matching location (whether ^ ~ type or unsigned type), and then find the location with the highest matching degree. If the location is ^ ~ type, stop matching and execute the location content; if the most matching location is unsigned type, keep the unsigned location and continue with the following regular matching; if URI does not meet any conditions for ordinary matching location, go straight to the next step.

3. Perform regular matching

Regular matching is related to the order of location, and Nginx matches from top to bottom. If a location is matched, the location logic is executed, all subsequent regular location is ignored, and the matching is terminated; if none of the regular matches is matched, the unsigned location retained before is executed, and 404 is returned directly if there is no reservation before.

Summary

The above is the whole content of this article. I hope the content of this article has a certain reference and learning value for everyone's study or work. Thank you for your support.

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