Introduction to the use of rewrite jump in nginx and configuration of Rewrite jump connection

One, Rewrite usage introduction

1. Rewrite jump scene

1. The URL that users browse can be adjusted to look more standardized and meet the needs of developers and product personnel.
2. In order to make search engines search for website content and better user experience, companies will disguise dynamic URL addresses as static addresses to provide services .
3. After the URL is changed to a new domain name, let the old visits jump to the new domain name. For example, when you visit 360buy.com on JD.com
, you will be redirected to jd.com. 4. Adjust the URL based on special variables, directories, and client information.

2. Rewrite jump implementation

Nginx supports url rewriting and if condition judgment through the ngx_http_rewrite_module module , but does not support else. In addition, this module needs PCRE support. You should specify PCRE support when compiling Nginx, and it is installed by default. According to the relevant variable redirection and selection of different configurations, jump from one location to another location, but such a loop can be executed up to 10 times, after which Nginx will return a 500 error. At the same time, the rewrite module contains the set instruction to create a new variable and set its value. This is very useful in some situations, such as recording condition identification, passing parameters to other locations, recording what has been done, and so on. The rewrite function is to use global variables provided by Nginx or variables set by yourself, combined with regular expressions and flags to achieve URL rewriting and redirection.

3.Rewrite actual scene

In actual work scenarios, there are three ways to achieve Nginx jump requirements.

  1. You can directly use rewrite for matching jump
  2. You can also use if to jump after matching global variables
  3. In addition, you can also use location to match and then jump,
    so rewrite can only be placed in the server{}, if{}, location{} section,
    location can only work on the string after the domain name except the passed parameters

Two, Rewrite syntax format

1. Rewrite command format

Insert picture description here

2. Commonly used regular expression metacharacters supported

Insert picture description here

3. The content after the jump

It is usually the URL domain name address path information of the webpage or website that is redirected to.

4. Flag mark

4.1. Flag description Insert picture description here
4.2. Comparison of last and break flags
Insert picture description here

Three, location matching rules

1. Classification of location

Insert picture description here

2. Commonly used expressions for location regular matching

Insert picture description here

3. The priority of location matching

Insert picture description here

4. Example of location priority

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

5. Comparison of rewrite and location

Insert picture description here

Fourth, the configuration of redirect based on domain name IP and other methods

The nginx service has been deployed on the server side. The client has completed the domain name resolution service.

The location of the configuration file for domain name resolution:
Linux's hosts file path is: /etc/hosts
Window's hosts file path is: C:\Windows\System32\drivers\etc\hosts

1. Redirect based on domain name

Now the company's old domain name www.wlm.com has business requirements and changes, and the new domain name www.wat.com needs to be used instead. However, the old domain name cannot be abolished, and it needs to be redirected to the new domain name. The specific configuration is as follows.

[root@Nginx ~]# vim /usr/local/nginx/conf/nginx.conf
server {           
        listen       80;
        server_name  www.wlm.com;
        charset utf-8;
        access_log  /var/log/nginx/wlm.access.log;
    location / {
            root   html;
            index  index.html index.htm;
    if ($host = 'www.wlm.com'){     //如果你输入这个网站
   rewrite ^/(.*)$ http://www.wat.com/$1 permanent;
   //跳转到另一个网站的任何位置
}   
        }

Jump test based on domain name
Insert picture description here
achieved the jump
Insert picture description here

2. Access redirect based on client IP

For example, today the company's business version is online, and all IP accesses to any content will display a fixed maintenance page, and only the company's IP access is normal.

[root@Nginx ~]# vim /usr/local/nginx/conf/nginx.conf
server {           
        listen       80;
        server_name  www.wlm.com;
        charset utf-8;
   access_log  /var/log/nginx/wlm.access.log;
set $rewrite true;
if ($remote_addr = '20.0.0.1'){
set $rewrite false;
}
if ($rewrite = true){
rewrite (.+) /wh.html;
}
location = /wh.html {
root  /usr/local/nginx/html/;
}
[root@Nginx ~]# cd /usr/local/nginx/html/
[root@Nginx html]# vim wh.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
</head>
<body>
<h1>维护   中</h1>
</body>
</html>

Test Results
Insert picture description here

Guess you like

Origin blog.csdn.net/wulimingde/article/details/108453805