Detailed explanation of Nginx Rewrite usage scenarios and code examples

Detailed explanation of Nginx Rewrite usage scenarios and code examples

Nginx Rewrite usage scenarios

1. URL address jump, for example, when a user visits pm.com, it will be redirected to baidu.com, or when a user visits via http, it will be redirected to https.

2. URL pseudo-static, a technology that displays dynamic pages as static pages, reduces the exposure of excessive parameters of dynamic URL addresses to the outside world, and improves security.

3. Search engine SEO optimization relies on URL paths to support search engine entry

4. The URL browsed by users can be adjusted to look more standardized and meet the needs of developers and product personnel.

Configuration syntax

rewrite regex replacement [flag];

4 flags

last stops processing subsequent rewrite instruction sets, jumps out of the location scope, and starts searching for a location that matches the changed URI. The URL address remains unchanged.

break stops processing subsequent rewrite instruction sets, does not jump out of the location scope, does not re-search, terminates matching, and the URL address remains unchanged

redirect returns a 302 temporary redirect. The browser address bar will display the redirected URL, and the crawler will not update its URL database.

permanent returns a 301 permanent redirect, the browser address bar will display the redirected URL, and the crawler will update its URL database

Note: When using last and break, the address in the browser will not change, but when using redirect and permanent, it will change

For example:

location /abc/ {
  rewrite ^/abc/(.*) /whsir/$1 break;
  return 401;
}
location /opq/ {
  rewrite ^/opq/(.*) /whsir/$1 last;
  return 402;
}
location /whsir/ {
  return 403;
}

After entering abc, if the content of whsir matches, it returns 200, otherwise it returns 404

After entering opq, if the content of whsir matches, it returns 403, otherwise it returns 404

last and break only return a status code, success or failure, while redirect and permanent will first return 302 or 301 before returning success or failure.

Commonly used Nginx global variables:

Example: http://www.pm.com:99/aa/bb/test.php

$host www.pm.com

$server_port 99

$request_uri /aa/bb/test.php

$document_root /pm_code

$request_filename /pm_code/aa/bb/test.php

Case 1: External jump

Open the lb01 virtual machine, close the previous custom configuration file, and create a new /etc/nginx/conf.d/rewrite.conf

Note: ^/ means that all URLs starting with root (here the root directory is /pm_code, and the corresponding domain name is www.pm.com) will be redirected to Baidu.

Modify the /etc/nginx/nginx.conf file

Change the error log level to notice so that it can record redirection information (debug, info, notice, warn, error, crit, the more information is recorded)

Add a line to the http module

rewrite_log on; (turn on rewrite logging)

test

Enter www.pm.com in the browser address bar (already configured in hosts), and it will redirect to the Baidu homepage

Case 2: Internal jump

Change /etc/nginx/conf.d/rewrite.conf content

Create file 2.html (content 222) in /pm_code and enter www.pm.com/1.html in the browser

View the records in the error log

Using return

In the above method, 1.html is displayed in the browser address bar, and the webpage content is the content of 2.html. If you want to display 2.html in the address bar, change the content in location to return 302 /2.html; or rewrite .* /2.html redirect;

Use regular expressions

Example: Redirect www.pm.com/2020/* to www.pm.com/2019/*

The brackets represent a whole, and $1 represents the content in the first bracket.

Redirect http to https

Note: After nginx 1.14, you don’t need to write ssl on. Instead, write listen 443 ssl in the listen field;

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Nginx URL rewriting mechanism principle and usage examples
  • Nginx rewrite regular matching rewriting method example
  • Detailed explanation of location and rewrite usage in nginx
  • How to redirect URL using nginx rewrite
  • Detailed explanation of commonly used nginx rewrite rules
  • Detailed explanation of nginx configuration location summary and rewrite rule writing
  • Detailed explanation of nginx rewrite and location according to URL parameters
  • Analysis of Nginx Rewrite usage scenarios and configuration methods

<<:  Native js to achieve accordion effect

>>:  MySQL stored procedures and common function code analysis

Recommend

MySQL 5.7.15 version installation and configuration method graphic tutorial

This article shares with you a detailed tutorial ...

Solution to inconsistent display of cursor size in input box

The cursor size in the input box is inconsistent T...

JavaScript to implement search data display

This article shares the data display code for Jav...

CSS positioning layout (position, positioning layout skills)

1. What is positioning? The position attribute in...

Steps to transfer files and folders between two Linux servers

Today I was dealing with the issue of migrating a...

Build a Docker private warehouse (self-signed method)

In order to centrally manage the images we create...

Detailed explanation of Vuex environment

Table of contents Build Vuex environment Summariz...

MySQL index knowledge summary

The establishment of MySQL index is very importan...

How to clear the validation prompt in element form validation

Table of contents Problem scenario: Solution: 1. ...

WeChat Mini Program implements the likes service

This article shares the specific code for the WeC...

Detailed description of HTML table border control

Only show the top border <table frame=above>...

Why is the disk space still occupied after deleting table data in MySQL?

Table of contents 1. Mysql data structure 2. The ...

Three ways to copy MySQL tables (summary)

Copy table structure and its data The following s...