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

Mobile browser Viewport parameters (web front-end design)

Mobile browsers place web pages in a virtual "...

Summary of using the reduce() method in JS

Table of contents 1. Grammar 2. Examples 3. Other...

Summary of Linux ps and pstree command knowledge points

The ps command in Linux is the abbreviation of Pr...

Detailed tutorial for installing mysql5.7.18 on centos7.3

1 Check the Linux distribution version [root@type...

Why do we need Map when we already have Object in JavaScript?

Table of contents 1. Don’t treat objects as Maps ...

Detailed explanation of this pointing problem in JavaScript function

this keyword Which object calls the function, and...

How to understand the difference between computed and watch in Vue

Table of contents Overview computed watch monitor...

Add a copy code button code to the website code block pre tag

Referring to other more professional blog systems...

Springboot+VUE to realize login and registration

This article example shares the specific code of ...

Robots.txt detailed introduction

Robots.txt is a plain text file in which website ...

Implementation of nginx proxy port 80 to port 443

The nginx.conf configuration file is as follows u...

SQL Optimization Tutorial: IN and RANGE Queries

Preface "High Performance MySQL" mentio...