How to use Nginx Rewrit to realize web page jump

Table of contents

1. Nginx Rewrite

2. Rewrite function

Rewrite jump scene

Rewrite jump implementation

Nginx

Jump

pcre support

Rewrite module

Rewrite actual scenario

Rewrite command/syntax format

flag description

location classification

location priority

rewrite compared to location

3. Jump cases

Realize domain name jump

The first step is to modify the specified configuration file

The second step is to check for syntax errors 

The third step is to troubleshoot if an error is reported. 

Step 4: Add domain name mapping relationship 

Step 5: Restart the service 

Step 6 Test Results 

Redirect old domain name to new domain name

The first step is to modify the configuration file

The second step is to add domain name mapping relationship

Step 3 Restart the service

Step 4: Create web page execution file

Step 5: Test with a browser

Step 6: Handling Errors (Troubleshooting)

Step 7 Retest access

Match jump based on parameters

The first step is to modify the configuration file

Step 2: Check for grammatical errors

Step 3 Restart the service

The fourth step is to set the mapping relationship

Step 5: Test with a browser

Access jump based on client IP address

The first step is to modify the configuration file

The second step is to detect file syntax problems

The third step is to create a web page and return the executable file

Step 4: Restart the service

Step 5: Use a browser to test access

Jump based on PHP files in the directory

The first step is to modify the configuration file

The second step is to check file syntax issues

Step 3 Restart the service

Step 4: Test with a browser

Request jump based on ordinary RUL

The first step is to modify the configuration file

The second step is to check whether there are grammatical problems

Step 3 Restart the service

Step 4: Use a browser to test access


1. Nginx Rewrite

The main function of Rewrite is to implement URL rewriting. Nginx's Rewrite rules use PCRE Perl-compatible regular expression syntax for rule matching. If you use Nginx's Rewrite function, you must compile and install the PCRE library before compiling Nginx. Rewrite mainly implements URL address rewriting and redirection, which is the process of redirecting incoming web requests to other URLs.

The functions of rewrite and location are somewhat similar, and both can achieve jumps. The main difference is that rewrite is often used to change the path to obtain resources within the same domain name, while location controls access and reverse proxy for a type of path, and can proxy_pass to other servers.

The global variables provided by Nginx or the variables set by yourself can be combined with regular expressions and flags to implement URL rewriting and redirection.

rewrite can only be placed in server{}, location{}, if{},

And it can only work on the string after the domain name excluding the passed parameters.

2. Rewrite function

① Rewrite jump scene

②Rewrite jump implementation

③ Rewrite actual scenario

④ Rewrite regular expression (used to match URLs)

⑤ Rewrite command/grammar format

⑥ location classification

⑦ location priority

⑧ Compare Rewrite and location

⑨ Implement the jump based on the above

Rewrite jump scene

① The URL looks more standardized and reasonable

② Enterprises will disguise dynamic URL addresses as static addresses to provide services

③ After the website changes to a new domain name, let the old visits jump to the new domain name

④ Certain business jumps on the server side

URL access jump, support development and design

① Page jump ② Compatibility support ③ Display effect

SEO optimization

maintain

① Backend maintenance ② Traffic forwarding

Safety

① Pseudo-static page

Rewrite jump implementation

Nginx

Support URL rewriting and if conditional judgment

But it does not support else command execution

Jump

The loop can be executed up to 10 times

nginx will return a 500 error after exceeding

pcre support

rewrite uses Nginx global variables or variables set by yourself

Combining regular expressions and flags to implement URL rewriting and redirection

Rewrite module

set command

All the above are based on

ngx_http_rewrite_module module

Rewrite actual scenario

① How to implement Nginx jump requirements

Use rewrite for matching jump (anti-hotlinking)

Use if to match global variables and jump (Nginx's own global variables, not the system's global variables)

location can match local rewriting and cross-server jumps

② rewrite is placed in the server{}, if{}, location{} module sections

Location only works on the string after the domain name that is used to pass parameters.

③ For domain names or parameter strings

Use if global variable matching

Use proxy_pass reverse proxy

Rewrite is to change the path to obtain resources within the same domain name

location itself is a path resource

Rewrite command/syntax format

Command syntax: rewrite [regular] [content after jump] [flag mark supported by rewrite];

flag description

last (equivalent to Apache's [L] mark, indicating completion of rewrite) is generally used in Server and if

break (this rule terminates when matching is completed and no longer matches any subsequent rules) is generally used for location

redirect (returns 302 temporary redirect, the browser address will display the URL address after the jump, and the crawler will not update the URL)

permanent (returns 301 permanent redirection, the browser address bar will display the URL address after the jump, and the crawler will update the URL)

break is similar to last, but break will not reinitiate the processing process, but directly returns the processing result.

redirect This tag indicates that the redirection operation will be performed immediately after the current rule is successfully matched.

permanent This tag is similar to redirect, but redirects permanently

The set mark indicates that the current rule successfully sets a variable and passes the variable to the next hop rule.

location classification

location = [string]{} #Exact match

location [string]{} #general matching

location [string]{} #regular matching

~ #Perform a regular match, case-sensitive

~* #Not case sensitive

!~ #Case-sensitive negation

!~* #case insensitive negation

^~ #Normal character matching, using prefix matching, if the match is successful, no other matching will be performed

= #Common characters, accurate matching

@ #Define a named location and use it to define it internally

location priority

Prefer exact matches (=)

Secondly, prefix matching (^~)

Secondly, match the regular rules in the order in the file (~ or ~*)

Then match the prefix match without any modification (regular string match)

Universal match (/) Any request can match if there is no other match

Execution order

Exact > Prefix > Regular > General > Universal

In the case of the same priority, the execution order is from top to bottom.

rewrite compared to location

Both can achieve jump (the same point)

rewrite is to change the path to obtain resources within the same domain name

Location controls access or reverse proxy to a class of paths, and can also proxy_pass to other machines.

rewrite will be written in location (executed in order)

Execute the rewrite instruction in the Server block

Perform location matching

Execute the rewrite command in the selected location

3. Jump cases

Realize domain name jump

First, turn off the firewall and security mechanisms

The first step is to modify the specified configuration file

Modify the specified user Nginx 

First, you must have an nginx program user

cat /etc/passwd #Check whether there is an nginx user and whether it is a program user

Modify the corresponding domain name (just customize it)

Modify the specified log file. The following main can be added or not.

Add domain name redirection 

The second step is to check for syntax errors 

Save and exit

nginx -t #Check if there are any syntax problems

The third step is to troubleshoot if an error is reported. 

No log found here

Unable to recognize log format

Then enter the configuration file

Save again and exit

Check again for errors

Create log files manually

and empower

Step 4: Add domain name mapping relationship 

Modify domain name resolution

vim /etc/hosts

Step 5: Restart the service 

Restart service

systemctl restart nginx

Step 6 Test Results 

Access test with browser

View web page elements

Detailed URL and status code

 

Redirect old domain name to new domain name

The first step is to modify the configuration file

The second step is to add domain name mapping relationship

Step 3 Restart the service

Command: systemctl restart nginx

Step 4: Create web page execution file

Step 5: Test with a browser

We jumped successfully

But there is no content in the page

The jump can jump normally but does not display the web page content.

Step 6: Handling Errors (Troubleshooting)

Step 7 Retest access

Match jump based on parameters

The first step is to modify the configuration file

Step 2: Check for grammatical errors

Command: nginx -t

Step 3 Restart the service

Command: systemctl restart nginx

The fourth step is to set the mapping relationship

Command: vim /etc/hosts

Step 5: Test with a browser

Access jump based on client IP address

The first step is to modify the configuration file

The second step is to detect file syntax problems

Command: nginx -t

The third step is to create a web page and return the executable file

Step 4: Restart the service

Command: systemctl restart nginx

Step 5: Use a browser to test access

Jump based on PHP files in the directory

The first step is to modify the configuration file

The second step is to check file syntax issues

Command: nginx -t

Step 3 Restart the service

Command: systemctl restart nginx

Step 4: Test with a browser

Request jump based on ordinary RUL

The first step is to modify the configuration file

The second step is to check whether there are grammatical problems

Command: nginx -t

Step 3 Restart the service

Command: systemctl restart nginx

Step 4: Use a browser to test access

Guess you like

Origin blog.csdn.net/Liu_Fang_Hong/article/details/131430507