nginx 根据上下文跳转
比如要根据域名中的上下文来跳转到不同的服务环境
test.xxx.com/test1 跳转到 192.168.0.100,
test.xxx.com/test2 跳转到 192.168.0.101,
配置如下:
server {
listen80;
location/ test1/ {
proxy_pass http://192.168.0.100/;
}
location/ test2/ {
proxy_pass http://192.168.0.1010/;
}
}
nginx 通过请求中的关键字跳转
如果需要从客户端发过来的请求中判断是否含有某些关键字,根据关键字再另做处理,则可以在server块中另入这样配置,判断$request_uri:
location/ {
proxy_passhttp://backend;
#当请求中包含有”123,abc,你好”,关键字时,则跳转到另一个服务http://192.168.0.105
if ($request_uri ~* =(123|abc|你好|)){
proxy_passhttp://192.168.0.105;
}
}
或者跳转到指定的本地页面,当请求中是index.php或者是index.jsp时,跳转至html目录:
location/index.php{
root html;
}
location/index.jsp{
root html;
}
当请求中含有 *.php时,跳转至html目录
location~*.php{
root html;
}