nginx rewrite重写入门

https://blog.csdn.net/github_26672553/article/details/81904114
看看前面配置

location /php{
    proxy_set_header Host $host:$server_port; 
    proxy_pass http://192.168.88.88:9090/;  #为什么这里要加个/
}

/代表根,代理不会把上面的/php带过去,如果去掉/后php网站内置服务器有特殊处理,依然可以访问到。但是java就不行了,除非java代码加入对应的/java @RequestMapping.

rewrite指令

http://nginx.org/en/docs/http/ngx_http_rewrite_module.html

#格式是
rewrite 正则 重写的地址 flag

示例:

rewrite ^/(.*)$/php/$1 break;

比如访问的是/test/abc那么/(.*)$就匹配了所有,$1就是test/abc
如果是^/test/(.*)$,那么$1就是abc

flag说明

last 本条规则匹配完成后,继续向下匹配新的location URL规则
break 本条规则匹配完成则停止,不再匹配后面的任何规则
redirect 返回302临时重定向,浏览器地址会显示跳转后的URL地址
permanent 返回301永久重定向,浏览器地址栏会显示跳转后的URL地址

代理多网站今天的写法

proxy_set_header Host $host:$server_port; 
location /php{
    rewrite /php/(.*)/$1 break;
    proxy_pass http://192.168.88.88:9090;
}
location /java{
    rewrite /java/(.*)/$1 break;
    proxy_pass http://192.168.88.88:8080;
}

猜你喜欢

转载自blog.csdn.net/github_26672553/article/details/81906552