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:
|
<<: How to declare a cursor in mysql
>>: Three ways to delete a table in MySQL (summary)
There are two types of dead link formats defined b...
On Linux, bash is adopted as the standard, which ...
This article shares with you how to use the Vue c...
Preface By default, MySQL will initialize a large...
In the past, it was quite troublesome to achieve ...
On Unix-like systems, you may know when a command...
Because I want to write a web page myself, I am al...
Sometimes you need to debug remotely in a server ...
This article shares the specific code of JavaScri...
I was working on a pop-up ad recently. Since the d...
For front-end developers, ensuring that the code ...
This article example shares the specific code of ...
MySQL master-slave replication allows data from o...
Main library execution CREATE DATABASE test CHARA...
Problems: After adding the -v parameter to docker...