nginx反向代理案例

56 阅读1分钟

配置案例1

请求地址/foo/bar, 只希望/bar可以发送到代理地址, 不希望/foo被发送

rewrite规则匹配/foo/后面的部分, 把地址重写为匹配到的第一部分($1)发送给代理app

location  /foo {  rewrite /foo/(.*) /$1  break;  proxy_pass         http://localhost:3200;  proxy_redirect     off;  proxy_set_header   Host $host;}

配置案例2

请求地址/foo/bar, 希望匹配到/foo的地址,带/foo发送给app

location  /foo/ {  proxy_pass         http://localhost:3200;  proxy_redirect     off;  proxy_set_header   Host $host;}

配置案例3

请求地址/foo/bar, 希望匹配到目标服务器/xxx/foo/bar(目标服务器的子目录)

location /foo/ {    rewrite /api/(.*) /xxx/api/$1 break;    proxy_pass      https://target_host/;}