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
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:
|
<<: Native js to achieve accordion effect
>>: MySQL stored procedures and common function code analysis
This article shares with you a detailed tutorial ...
The cursor size in the input box is inconsistent T...
This article shares the data display code for Jav...
1. What is positioning? The position attribute in...
Today I was dealing with the issue of migrating a...
After installing wamp for the first time, all ser...
In order to centrally manage the images we create...
Table of contents Build Vuex environment Summariz...
The establishment of MySQL index is very importan...
Table of contents Problem scenario: Solution: 1. ...
This article shares the specific code for the WeC...
Only show the top border <table frame=above>...
Table of contents 1. Mysql data structure 2. The ...
Today I have nothing to do, so I listed some tool...
Copy table structure and its data The following s...