Analysis of Nginx Rewrite usage scenarios and configuration methods

Analysis of Nginx Rewrite usage scenarios and configuration methods

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
  • Detailed explanation of Nginx Rewrite usage scenarios and code 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

<<:  How to declare a cursor in mysql

>>:  Three ways to delete a table in MySQL (summary)

Recommend

CSS Sticky Footer Several Implementations

What is "Sticky Footer" The so-called &...

ffmpeg Chinese parameter description and usage examples

1. When ffmpeg pushes video files, the encoding f...

Summary of using the exclamation mark command (!) in Linux

Preface Recently, our company has configured mbp,...

Linux uses NetworkManager to randomly generate your MAC address

Nowadays, whether you are on the sofa at home or ...

Solve the problem of MySql8.0 checking transaction isolation level error

Table of contents MySql8.0 View transaction isola...

How to import/save/load/delete images locally in Docker

1. Docker imports local images Sometimes we copy ...

Usage of Node.js http module

Table of contents Preface HTTP HTTP Server File S...

The difference between html empty link href="#" and href="javascript:void(0)"

# contains a location information. The default anc...

Summary of ten principles for optimizing basic statements in MySQL

Preface In the application of database, programme...

TCP socket SYN queue and Accept queue difference analysis

First we must understand that a TCP socket in the...

MySQL merges multiple rows of data based on the group_concat() function

A very useful function group_concat(), the manual...

MySQL 5.7.10 installation and configuration tutorial under Windows

MySQL provides two different versions for differe...

How to update Ubuntu 20.04 LTS on Windows 10

April 23, 2020, Today, Ubuntu 20.04 on Windows al...