html的页面
[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地址重写
rewrite模块:rewrite指令
sysntax : rewrite regex replacement [flag];
Default: -
Context: server,location,if
功能:
将regex指定的URL替换成replacement这个新的url可以使用正则表达式
当replacement以http://或者https://或$schema开头,则直接返回302重定向
替换后的url根据flag指定的方式进行处理
last:用replacement这个url进行新的location匹配
break:break指令是停止当前脚本指令的执行,等价于独立的break指令
redirect:返回302重定向(临时重定向)
permanent:返回301重定向(永久重定向)
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';
}
}
测试
[root@python vhast]# curl haha.com:8080/first/1.txt
second!
修改配置
[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/;#到这里找请求的资源
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';
}
}
测试
[root@python vhast]# curl haha.com:8080/first/1.txt
third
修改配置
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'; # 匹配到这个资源
}
}
测试
[root@python vhast]# curl haha.com:8080/first/1.txt
third!
修改配置
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;继承上一个
}
location /redirect4 {
rewrite /redirect4(.*) http://haha.com:$1 permanent; #301
}
}
测试
[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>