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

The method of location matching and rewrite rewriting Jump in Nginx

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces the relevant knowledge of "location matching and rewrite rewriting jump methods in Nginx". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "location matching and rewrite rewriting jump methods in Nginx" can help you solve the problem.

1. Rewrite Jump implementation

Nginx: supports URL rewriting and if condition judgment through ngx_http_rewrite_module module, but does not support else

Jump: jump from one location to another location, the loop can be executed up to 10 times, after which the nginx will return a 500error

PCRE support: perl is compatible with syntax rule matching of regular expressions

Rewrite the module set instruction: create a new variable and assign a value to it

II. Rewrite execution sequence

① executes the rewriter instructions in server

② performs location matching

③ executes the rewrite instruction in the selected location

3. Rewrite jump scene

Adjust the URL browsed by users to look more standardized and meet the needs of developers and product personnel

In order to make the search engine search for website content and better user experience, enterprises will disguise dynamic URL addresses as static addresses to provide services.

After the URL is replaced with a new domain name, the old access will be redirected to the new domain name; for example, accessing JD.com 's 360buy.com will jump to jd.com.

Some service adjustments on the server side, such as URL adjustment according to special variables, directories, client information, etc.

Syntax and flg tag description

Syntax rewrite [flag]

Regex: represents a regular matching rule

Replacement: indicates the content after the jump

Flag: represents the flag tag supported by rewrite

The flag tag says:

Last: after the matching of this rule is completed, continue to match the new location URI rule down, which is generally used in server and if

Break: this rule is terminated upon completion of matching. It no longer matches any of the following rules. It is generally used in location.

Redirect: returns 302 temporary redirection, and the browser address will display the URL address after the jump.

Permanent: returns the 301 permanent redirection, and the browser address bar will display the jumped URL address.

5. Regular expression symbols commonly used in Nginx

6. Location1. classification

Location can be broadly divided into the following three categories:

Exact match: location = / {… }

General match: location / {… }

Regular matching: location ~ / {… }

two。 Common matching rules

3. Priority

First of all, exact match =

Second, the prefix matches ^ ~

The second is the regular matching ~ or ~ * according to the order in the file.

Then there is a prefix match without any modification, that is, a general match.

Finally, it is handed over to / universal matching, with the lowest priority.

4. Example

Localtion = / {}

= for exact matching /, the hostname cannot be followed by any strings, such as access / and / xcf, then / match, / xcf does not match

Location / {}

Because all addresses start with /, this rule will match all requests, such as access / and / data, then / match, / data will also match

However, if it is followed by a regular expression, it will match the longest string first (longest match).

Location / documents/ {}

Match any address that starts with / documents/, and after matching, continue to search for other location

Location / documents/abc {}

Match any address that starts with / documents/abc, and after matching, continue to search for other location

This line will be used only if the regular expressions that follow other location do not match.

Location ^ ~ / images/ {}

Match any address that starts with / images/. After the match, stop searching for the rule and use this

Location *. (gif | jpg | jpeg) ${}

Match all requests ending with gif, jpg, jpeg

However, all pictures under the request / images/ will be processed by location ^ ~ / images/ because ^ ~ has a higher priority, so this rule cannot be reached.

Location / images/abc {}

The longest character matches to / images/abc, with the lowest priority. If you continue to search for other location, you will find that ^ ~ and ~ exist.

Location ~ / images/abc {}

Matching starts with / images/abc, followed by priority, which will be used only if location ^ ~ / images is removed.

Location / images/abc/1.html {}

Match / images/abc/1.html file, if compared with regular ~ / images/abc/1.html, regular priority is higher

Priority summary:

(location = full path) > (location ^ ~ path) > (location, * regular order) > (location partial start path) > (location /)

5. Match rule definition

The first required rule for ①:

Directly match the root of the website, and visit the home page of the website frequently through the domain name. Using this will speed up the processing, such as the official website.

It can be a static home page, or it can be forwarded directly to the backend "application server"-- > PHP, Apache (the application server is shown in the following figure)

Location / {root html; index index.html index.html;}

The second required rule of ②:

Handle static file requests, which is the strength of nginx as a http server (① static request processing ability ② high concurrent processing capacity ③ low resource consumption)

There are two configuration modes, directory matching or suffix matching, either or in combination

Location ^ ~ / static/ {root/ webroot/static/;} location ~ *\. (html | gif | jpg | jpeg | png | css | js | ico) ${root/ webroot/res/;}

The third required rule of ③:

Are general rules, such as forwarding dynamic requests with .php and .jsp suffixes to back-end application servers

Non-static file requests default to dynamic requests (jump / reverse proxy)

Upstream tomcat_server {192.168.126.12 proxy_ pass 80 192.168.126.23 location / {proxy_ pass summary:

1. Compare rewrite and location

What they have in common: all jump

Difference: rewrite changes the path to get resources within the same domain name.

Location is used to control access or reverse proxy for a class of paths, and can also proxy_pass to other machines.

2.rewrite will be written in location, in order of execution.

Execute the rewrite instruction in the server block

Perform location matching

Execute the rewrite instruction in the selected location

How are the 3.location priorities ranked?

Match a specific file

(location = full path) > (location ^ ~ full path) > (location ~ * full path) > (location ~ full path) > (location /)

Use directory matching to access a file

(location = directory) > (location ^ ~ directory) > (location ~ directory) > (location ~ * directory) > (location /)

4. Why do files and directories change only in areas that are case-insensitive?

Regular expressions: the aim is to match as accurately as possible

Files-- > match as accurately as possible, case-sensitive and more accurate

Directory-> match as accurately as possible, case sensitivity is more accurate, and priority is higher.

This is the end of the introduction of "the method of location matching and rewrite rewriting jump in Nginx". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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

Development

Wechat

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

12
Report