重定向
表示路由跳转,可以把uri跳转到另一个uri
主机重写
rewrite 功能,把uri改造,非跳转
location
路由定位
概念还未明确,先把学到的记录
在nginx中配置proxy_pass代理转发时,如果在proxy_pass后面的url加/,表示绝对根路径;如果没有/,表示相对路径,把匹配的路径部分也给代理走。
假设下面四种情况分别用 http://192.168.1.1/proxy/test.html 进行访问。
第一种:
location /proxy/ {
proxy_pass http://127.0.0.1/;
}
代理到URL:http://127.0.0.1/test.html
第二种(相对于第一种,最后少一个 / )
location /proxy/ {
proxy_pass http://127.0.0.1;
}
代理到URL:http://127.0.0.1/proxy/test.html
第三种:
location /proxy/ {
proxy_pass http://127.0.0.1/aaa/;
}
代理到URL:http://127.0.0.1/aaa/test.html
第四种(相对于第三种,最后少一个 / )
location /proxy/ {
proxy_pass http://127.0.0.1/aaa;
}
代理到URL:http://127.0.0.1/aaatest.html
测试备份
# 所有以/aa开头的地址都会被匹配到:
# nginxtest.com/aa
# /aaa/bbbccc/
location /aa {
# proxy_pass http://test.cc;
default_type text/plain;
return 502 "/aa";
}
# 所有以/bb/开头的地址都会被匹配到:
location /bb/ {
default_type text/plain;
return 200 "/bb/";
}
location ~ /cc {
default_type text/plain;
return 200 "~ /cc";
}
location /cc {
default_type text/plain;
return 200 "/cc/a";
}
# 优先级:(location =) > (location 完整路径) > (location ^~ 路径) > (location ~,~* 正则顺序) > (location 部分起始路径) > (/)
# 如果是最长匹配,优先级更高
location /documents/ {
# 匹配任何以 /documents/ 开头的地址,匹配符合以后,还要继续往下搜索
# 只有后面的正则表达式没有匹配到时,这一条才会采用这一条
# [ configuration C ]
}
location ~ /documents/Abc {
# 匹配任何以 /documents/ 开头的地址,匹配符合以后,还要继续往下搜索
# 只有后面的正则表达式没有匹配到时,这一条才会采用这一条
#[ configuration CC ]
}
location ^~ /images/ {
# 匹配任何以 /images/ 开头的地址,匹配符合以后,停止往下搜索正则,采用这一条。
# [ configuration D ]
default_type text/plain;
return 200 "D";
}
location ~* \.(gif|jpg|jpeg)$ {
# 匹配所有以 gif,jpg或jpeg 结尾的请求
# 然而,所有请求 /images/ 下的图片会被 config D 处理,因为 ^~ 到达不了这一条正则
# [ configuration E ]
#default_type text/plain;
#return 200 "E";
}
location /images/abc {
# 最长字符匹配到 /images/abc,继续往下,会发现 ^~ 存在
# F与G的放置顺序是没有关系的
# [ configuration G ]
default_type text/plain;
return 200 "G";
}
location ~ /images/abc/ {
# 先最长匹配 config G 开头的地址,继续往下搜索,匹配到这一条正则,采用
# [ configuration H ]
default_type text/plain;
return 200 "H";
}