nginx test rewrite

nginx test rewrite

last: Equivalent to the (L) mark in Apache, indicating completion of rewrite matching;
break: After the matching of this rule is completed, the matching will be terminated and subsequent rules will no longer be matched.
# Where last and break are used to implement URL rewriting, the URL address in the browser address bar remains unchanged.
redirect: Returns 302 temporary redirect, and the browser address will display the URL address after the jump.
permanent: Returns 301 permanent redirect, and the browser address bar will display the URL address after the jump.
#If not configured, 304 will be displayed
[root@node1 ~]# yum -y install gcc make pcre-devel openssl-devel
[root@node1 ~]# tar xf nginx-1.22.1.tar.gz
[root@node1 ~]# cd nginx-1.22.1/
[root@node1 nginx-1.22.1]# ./configure --prefix=/usr/local/nginx --user=nginx --with-http_ssl_module
[root@node1 nginx-1.22.1]# make & amp; & amp; make install
[root@node1 nginx-1.22.1]# useradd nginx -s /sbin/nologin
[root@node1 nginx-1.22.1]# /usr/local/nginx/sbin/nginx
[root@node1 nginx-1.22.1]# cd /usr/local/nginx/
[root@node1 nginx]# mkdir html/static
[root@node1 nginx]# echo "html/static/index.html">html/static/index.html
[root@node1 nginx]# echo "html/static/node1.html">html/static/node1.html


#Access display
http://www.myweb.com/static/ --> html/static/index.html
http://www.myweb.com/static/test.html --> html/static/test.html
http://www.myweb.com/forum.php --> 404 Not Found

last: Equivalent to the L) mark in Apache, indicating completion of rewrite matching;

break: After the matching of this rule is completed, the matching will be terminated and subsequent rules will no longer be matched.

redirect: Returns 302 temporary redirect, and the browser address will display the URL address after the jump.

permanent: Returns 301 permanent redirect, and the browser address bar will display the redirected URL address.

Among them, last and break are used to implement URL rewriting, so that the URL address in the browser address bar remains unchanged.

#Visit http://www.myweb.com/forum.php -Jump-> html/static/test.html

server {<!-- -->
        listen 80;
        server_name www.myweb.com;

#Local directory rewriting
        # Status code 304, the address bar does not change http://www.myweb.com/forum.php
        rewrite ^/forum.php$ /static/node1.html last;

        # Status code 304, the address bar does not change http://www.myweb.com/forum.php
        rewrite ^/forum.php$ /static/node1.html break;
        
        # Status code 301 address bar changes to http://www.myweb.com/static/test.html
        rewrite ^/forum.php$ /static/test.html permanent;
        
        # Status code 302 address bar changes to http://www.myweb.com/static/test.html
        rewrite ^/forum.php$ http://www.myweb.com/static/test.html last;

#This website url jumps the url address bar changes, last, break, redirect are all 302
        # Status code 302 address bar changes to http://www.myweb.com/static/test.html
        rewrite ^/forum.php$ http://www.myweb.com/static/test.html last;

#forum.php status code 301 The address bar changes to http://www.myweb.com/static/test.html
        rewrite ^/forum.php$ http://www.myweb.com/static/test.html permanent;
\t\t
# hoppingbaidu
        # forum.php status code 302 The address bar changes to http://www.baidu.com
        rewrite ^/forum.php$ http://www.baidu.com break; # last and redirect are also 302
        
#forum.php status code 301 The address bar changes to http://www.baidu.com
        rewrite ^/forum.php$ http://www.baidu.com permanent;
        location / {<!-- -->...

Browser Disable cache access
 location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {<!-- -->
        root html; # Absolute path can also complete access /usr/local/nginx/html;
    }

[root@node1 baidu]# wget -r -x http://fjxplz.com/1004.html


[root@node1 html] mkdir weihu
[root@node1 html]# echo "Under maintenance...">weihu/index.html

# http://www.myweb.com Maintenance
server {<!-- -->
        listen 80;
        server_name www.myweb.com;
        # 3. Finally jump to the maintenance page
        rewrite ^/forum.php$ http://www.myweb.com/static/test.html permanent;
        
# 2. Jumping after adding rewrite will affect the upper and lower order of the following locations.
rewrite ^/(.*)$ /weihu/index.html last; # All pages will jump to the maintenance page
        rewrite ^/$ /weihu/index.html last; # Jump to the maintenance page from the root page
        #rewrite ^/(.*)$ /weihu/index.html last;
        
        # 1. When the above rewrite is not added, the default homepage is
        location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {<!-- -->
        # location / { same as above
            root html; # Absolute path can also complete access /usr/local/nginx/html;
        }
        
 
# 3. Finally jump to the maintenance page
 http://www.myweb.com/forum.php--》static --》2 /weihu/
192.168.1.11 - - "GET /forum.php HTTP/1.1" 301 169 "-" "Wget/1.14 (linux-gnu)"
192.168.1.11 - - "GET /static/test.html HTTP/1.1" 200 13 "-" "Wget/1.14 (linux-gnu)"

server {<!-- -->
        listen 80;
        server_name www.myweb.com;
        # There will always be a 301 jump. Check the log or wget to see the process.
        rewrite ^/(.*)$ /weihu/index.html permanent; # Download wget or read the log
        
        # Pass parameters
        rewrite ^/(.*)$ http://www.baidu.com/$1 permanent;
        
        # myweb.com Jump to baidu
        if ($host = 'myweb.com') {<!-- -->
        rewrite ^/(.*)$ http://www.myweb.com/$1 permanent;
        }

wget view

[root@node1 conf]# wget myweb.com
--2023-10-21 17:32:25-- http://myweb.com/
Resolving myweb.com (myweb.com)... 192.168.1.11
Connecting to myweb.com (myweb.com)|192.168.1.11|:80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently # permanent
Location: http://www.myweb.com/ [following]
--2023-10-21 17:32:25-- http://www.myweb.com/ # After the jump
Resolving www.myweb.com (www.myweb.com)... 192.168.1.11
Reusing existing connection to myweb.com:80.
HTTP request sent, awaiting response... 200 OK
Length: 13 [text/html]
Saving to: index.html.2’


# myweb.com Jump to www.myweb.com
        if ($host = 'myweb.com') {<!-- -->
        rewrite ^/(.*)$ http://www.myweb.com/$1 last; # last is not the permanent above
        }
[root@node1 conf]# wget myweb.com
--2023-10-21 17:30:57-- http://myweb.com/
Resolving myweb.com (myweb.com)... 192.168.1.11
Connecting to myweb.com (myweb.com)|192.168.1.11|:80... connected.
HTTP request sent, awaiting response... 302 Moved Temporarily #302 last
Location: http://www.myweb.com/ [following]
--2023-10-21 17:30:57-- http://www.myweb.com/ # After the jump
Resolving www.myweb.com (www.myweb.com)... 192.168.1.11
Reusing existing connection to myweb.com:80.
HTTP request sent, awaiting response... 200 OK
Length: 13 [text/html]
Saving to: index.html.1’

Multiple domain name jumps

# Multiple domain name jumps
if ($host != 'myweb.com'){<!-- -->
    rewrite ^/(.*)s http://www.myweb.com/$1 permanent;
    }
#Jump to non-existent page
if ( !-e $request_filename){<!-- -->
rewrite ^/(.*)$ /weihu/index.html last;
}

# Firefox browser jump
if ($http_user_agent ~ firefox) {<!-- -->
rewrite ^/index.html$ /firefox/index.html;
}

Nginx: Rewrite jump + regular expression + 6 production cases! The content is too real

https://blog.csdn.net/weixin_48190891/article/details/108532802

Detailed introduction to common global variables in Nginx rewrite

https://blog.csdn.net/Blue92120/article/details/129003292

upstream lmsManagerAuth-backend {
server 7.180.171.42:8100 max_fails=3 fail_timeout=10s;
}


location ~ ^/edx/lmsManagerAuth/api/datacenter/.*{
  rewrite /edx/lmsManagerAuth/(.*) /$1 break; #Intercept and retain rewrite as api/datacenter/.*
  proxy_pass http://lmsManagerAuth-backend;
www.myweb.com.zh www.myweb.com/zh
www.myweb.com.en www.myweb.com/en


Add a sentence at the end of http{<!-- -->} in the original configuration file /usr/local/nginx/conf/nginx.conf:
include /usr/local/nginx/conf.d/*.conf;

[root@node1 nginx]# mkdir -p /usr/local/nginx/conf.d/
[root@node1 nginx]# vim conf.d/www.myng.com.conf
server {<!-- -->
listen 80;
server_name www.myng.com;
location / {<!-- -->
root/code;
index index.html;
}
[root@node1 nginx]# mkdir -p /code/{zh,en}

[root@node1 nginx]# echo zh>/code/zh/index.html
[root@node1 nginx]# echo en>/code/en/index.html
[root@node1 nginx]# echo code>/code/index.html
[root@node1 html]# curl www.myng.com
code
[root@node1 html]# curl www.myng.com/en/
en
[root@node1 html]# curl www.myng.com/zh/ #Add / by default in the browser
zh


server {<!-- -->
listen 80;
server_name www.myng.com.zh www.myng.com.en;
root/code;
index index.html;
location / {<!-- -->
\t
        if ($http_host ~* 'zh'){<!-- -->
        #$http_host requested host
        #$http_accept_language The language identifier in the browser request header
        #$http_user_agent ~* "iphone|ipad|androaid" Based on agent
        set $language zh;
        }
        
        if ($http_host ~* 'en'){<!-- -->
        set $language en;
        }
        
     rewrite ^/$ http://www.myng.com/$language redirect;
        
}
http://www.myng.com.zh/ --Jump--> http://www.myng.com/zh/

curl -Lv

[root@node1 html]# curl -Lv www.myng.com.zh #will automatically redirect the URL specified later
* About to connect() to www.myng.com.zh port 80 (#0)
* Trying 192.168.1.11...
* Connected to www.myng.com.zh (192.168.1.11) port 80 (#0)
>GET/HTTP/1.1
> User-Agent: curl/7.29.0
> Host: www.myng.com.zh
> Accept: */*
>
< HTTP/1.1 302 Moved Temporarily
< Server: nginx/1.22.1
< Date: Sun, 22 Oct 2023 12:53:43 GMT
< Content-Type: text/html
< Content-Length: 145
< Connection: keep-alive
< Location: http://www.myng.com/zh
<
* Ignoring the response-body
* Connection #0 to host www.myng.com.zh left intact
* Issue another request to this URL: 'http://www.myng.com/zh'
* About to connect() to www.myng.com port 80 (#1)
* Trying 192.168.1.11...
* Connected to www.myng.com (192.168.1.11) port 80 (#1)
> GET /zh HTTP/1.1
> User-Agent: curl/7.29.0
> Host: www.myng.com
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Server: nginx/1.22.1
< Date: Sun, 22 Oct 2023 12:53:43 GMT
< Content-Type: text/html
< Content-Length: 169
< Location: http://www.myng.com/zh/
< Connection: keep-alive
<
* Ignoring the response-body
* Connection #1 to host www.myng.com left intact
* Issue another request to this URL: 'http://www.myng.com/zh/'
* Found bundle for host www.myng.com: 0x2014450
* Re-using existing connection! (#1) with host www.myng.com
* Connected to www.myng.com (192.168.1.11) port 80 (#1)
> GET /zh/ HTTP/1.1
> User-Agent: curl/7.29.0
> Host: www.myng.com
> Accept: */*
>
< HTTP/1.1 200 OK
< Server: nginx/1.22.1
< Date: Sun, 22 Oct 2023 12:53:43 GMT
< Content-Type: text/html
<Content-Length: 3
< Last-Modified: Sun, 22 Oct 2023 11:48:01 GMT
< Connection: keep-alive
< ETag: "65350bf1-3"
< Accept-Ranges: bytes
<
zh
* Connection #1 to host www.myng.com left intact
# Without -L option
[root@node1 html]# curl -v www.myng.com.zh
* About to connect() to www.myng.com.zh port 80 (#0)
* Trying 192.168.1.11...
* Connected to www.myng.com.zh (192.168.1.11) port 80 (#0)
>GET/HTTP/1.1
> User-Agent: curl/7.29.0
> Host: www.myng.com.zh
> Accept: */*
>
< HTTP/1.1 302 Moved Temporarily
< Server: nginx/1.22.1
< Date: Sun, 22 Oct 2023 12:54:30 GMT
< Content-Type: text/html
< Content-Length: 145
< Connection: keep-alive
< Location: http://www.myng.com/zh
<
<html>
<head><title>302 Found</title></head>
<body>
<center><h1>302 Found</h1></center>
<center>nginx/1.22.1</center>
</body>
</html>
* Connection #0 to host www.myng.com.zh left intact

According to the browser, agent jumps

server {<!-- -->
listen 80;
server_name www.myng.com.zh www.myng.com.en;
root/code;
index index.html;
    location / {<!-- -->
#Scenario 1: Automatically jump to different pages according to the language of the user's browser.
        if ($http_accept_language ~* 'zh'){<!-- -->
        set $language zh;
        }
        
        if ($http_accept_language ~* 'en'){<!-- -->
        set $language en;
        }
        
     rewrite ^/$ http://www.myng.com/$language redirect;
        
}

#2: Jump to different sites or domain names based on the user’s source terminal device
if ($http_user_agent ~* "iphone|ipad|androaid"){<!-- -->
        rewrite ^/$ http://www.myng.com/m;
        }
        
#3: Pass the jump uri
if ($http_user_agent ~* "iphone|ipad|androaid"){<!-- -->
        return 302 http://www.myng.com$request_uri; #Pass uri
        }
        

api address jump

#api.myng.com/bbb www.myng.com/api/bbb

server {
listen 80;
server_name api.myng.com;
if ($http_host ~* (.*)\.(.*)\.(.*)){
set $domain $1;
}
rewrite ^/(.*)$ http://www.myng.com/$domain/$1 last;
}

Maintenance page

server {<!-- -->
listen 80;
server_name www.myng.com.zh www.myng.com.en;
root/code;
rewrite ^/(.*)$ /wh.html break; #Place under server
\t
    location / {<!-- -->...

Jump according to status code

server {<!-- -->
listen 80;
server_name www.myng.com;
root/code;
index index.html;
...
            error_page 500 502 503 504 =@temp;
location @temp {<!-- -->
root/data/error
rewrite ^/(.*)$ /wh.html break; #Place under server
}

Distinguish users based on IP

server {<!-- -->
listen 80;
server_name www.myng.com;
root/code;
set $ip 0;
if ($remote_addr ~ "10.0.0.1") {<!-- --> #X-Forwarded-For proxy ip
set $ip 1;
    }
    if ($ip=0){<!-- -->
    rewrite ^/(.*)$ /wh.html break; #Place under server
    }

return return jump

Requirement: If the user requests www.myng.com/test, return the URL to ww.xuliangwei.com

server {<!-- -->
        listen 80;
        server_name www.myng.com;
        location / {<!-- -->
        
            default_type text/html;# Need to set-->Identify return string
            if ($request_uri ~* '^/test'){<!-- -->
                return 200 "return test";
                #return 302 http://www.baidu.com; #302 Jump to Baidu
            }

            root/code;
            index index.html;
        }
}


#Browser access: return
http://www.myng.com/test return test

last break

When the rewrite rule encounters break, all rewrite/return rules of this locationf and other locations will no longer be executed.
When the rewrite rule encounters last, subsequent rewrite/return rules in this locationh will not be executed, but the rewritten url will execute all rules from the beginning again, whichever one matches will be executed.

server {<!-- -->
listen 80;
server_name www.myng.com;
root/code;
location / {<!-- -->
#http://www.myng.com/1.html b.html
rewrite /1.html /2.html;
            #http://www.myng.com/1.html 2.html
            #rewrite /1.html /2.html break; #break terminates, no longer matched
            #http://www.myng.com/1.html a.html
            #rewrite /1.html /2.html last; #last terminates the matching of the current location
            
rewrite /2.html /3.html;
}
\t
location /2.html {<!-- -->
rewrite /2.html /a.html;
}
\t
location /3.html {<!-- -->
rewrite /3.html /b.html;
}
}

[root@node1 nginx]# echo 1.html>/code/1.html
[root@node1 nginx]# echo 2.html>/code/2.html
[root@node1 nginx]# echo 3.html>/code/3.html
[root@node1 nginx]# echo a.html>/code/a.html
[root@node1 nginx]# echo b.html>/code/b.html