参考: https://serverfault.com/questions/379675/nginx-reverse-proxy-url-rewrite

有个需求

http://myserver:80/foo/bar 反向代理到后台端 http://localhost:3200/bar

两种方法,第二种是最正确的

第一种

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

第二种

location /foo {
  proxy_pass http://localhost:3200/;
}

upstream后面是否带反斜线很重要,
请注意proxy_pass指令末尾的附加/。NGINX将去掉匹配的前缀/foo,并将剩余的前缀传递给后端服务器的/路径。
因此,http://myserver:80/foo/bar 将发布到后端http://localhost:3200/bar.

相关文章:

  • 2021-05-29
  • 2021-09-19
  • 2021-09-21
  • 2021-09-19
猜你喜欢
  • 2022-02-18
  • 2022-01-01
  • 2022-12-23
  • 2021-08-29
  • 2022-12-23
  • 2022-12-23
  • 2021-05-27
相关资源
相似解决方案