html pages
[root@python html]# echo first > first/1.txt [root@python html]# echo second > second/1.txt [root@python html]# echo third > second/1.txt [root@python html]# echo second > second/1.txt [root@python html]# echo third > third/1.txt
url address rewrite
rewrite Module: rewrite instruction
sysntax : rewrite regex replacement [flag];
Default: -
Context: server,location,if
Functions:
Replace the URL specified by regex with replacement, a new URL that can use regular expressions
When the replacement begins with http: / /, https: / /, or $schema, a 302 redirect is returned directly.
The replaced url is processed according to the way specified by the flag.
last: use the replacement url to make a new location match
Break: the break instruction is to stop the execution of the current script instruction, which is equivalent to the independent break instruction.
Redirect: return 302 redirect (temporary redirect)
Persistent: returns 301 redirection (persistent redirection)
server { server_name haha.com; listen 8080; error_page 404 /403.html; #return 405; location /{ #return 404 "find nothing!\n"; } root html/; location /first { rewrite /first(.*) /second$1 last; return 200 1first!\n'; } location /second { #rewrite /second(.*) /third$1 break; rewrite /second(.*) /third$1; return 200 'second!\n'; } location /third { return 200 'third!\n'; } }
Testing
[root@python vhast]# curl haha.com:8080/first/1.txt second!
Modify configuration
[root@python vhast]# cat test.conf server { server_name haha.com; listen 8080; error_page 404 /403.html; #return 405; location /{ #return 404 "find nothing!\n"; } root html/;#Find the requested resource here location /first { rewrite /first(.*) /second$1 last; return 200 1first!\n'; } location /second { rewrite /second(.*) /third$1 break; Indicates after the instruction block is skipped #rewrite /second(.*) /third$1; return 200 'second!\n'; } location /third { return 200 'third!\n'; } }
Testing
[root@python vhast]# curl haha.com:8080/first/1.txt third
Modify configuration
server { server_name haha.com; listen 8080; error_page 404 /403.html; #return 405; location /{ #return 404 "find nothing!\n"; } root html/; location /first { rewrite /first(.*) /second$1 last; return 200 1first!\n'; } location /second { #rewrite /second(.*) /third$1 break; rewrite /second(.*) /third$1 last; # Continue matching after replacement return 200 'second!\n'; } location /third { return 200 'third!\n'; # Match to this resource } }
Testing
[root@python vhast]# curl haha.com:8080/first/1.txt third!
Modify configuration
server { server_name haha.com; #listen 8080; error_page 404 /403.html; #return 405; location /{ #return 404 "find nothing!\n"; } root html/; location /first { rewrite /first(.*) /second$1 last; return 200 1first!\n'; } location /second { #rewrite /second(.*) /third$1 break; rewrite /second(.*) /third$1 last; return 200 'second!\n'; } location /third { return 200 'third!\n'; } location /redirect1 { rewrite /redirect1(.*) $1 permanent; #301 } location /redirect2 { rewrite /redirect2(.*) $1 redirect; #302 } location /redirect3 { rewrite /redirect3(.*) http://Haha.com: $1; ා302; inherit previous } location /redirect4 { rewrite /redirect4(.*) http://haha.com:$1 permanent; #301 } }
Testing
[root@python vhast]# curl haha.com/redirect1 <html> <head><title>301 Moved Permanently</title></head> <body> <center><h1>301 Moved Permanently</h1></center> <hr><center>nginx/1.15.9</center> </body> </html> [root@python vhast]# curl haha.com/redirect2 <html> <head><title>302 Found</title></head> <body> <center><h1>302 Found</h1></center> <hr><center>nginx/1.15.9</center> </body> </html> [root@python vhast]# curl haha.com/redirect3 <html> <head><title>302 Found</title></head> <body> <center><h1>302 Found</h1></center> <hr><center>nginx/1.15.9</center> </body> </html> [root@python vhast]# curl haha.com/redirect4 <html> <head><title>301 Moved Permanently</title></head> <body> <center><h1>301 Moved Permanently</h1></center> <hr><center>nginx/1.15.9</center> </body> </html>