Nginx如何按位置路由到反向代理

Nginx如何按位置路由到反向代理,nginx,nginx-reverse-proxy,nginx-config,Nginx,Nginx Reverse Proxy,Nginx Config,目前我正在使用nginx服务器,并使用nodejs服务器作为反向代理。 我想让nginx服务器代理按位置传递不同的服务器 所以,假设我的域名是subdomain.domain.com 加载subdomain.domain.com/时,应提供静态文件 加载subdomain.domain.com/example1/req1/req2时,应路由到127.0.0.1:3000/req1/req2 加载subdomain.domain.com/example2/req1/req2时,应该路由到127.0

目前我正在使用nginx服务器,并使用nodejs服务器作为反向代理。 我想让nginx服务器代理按位置传递不同的服务器

所以,假设我的域名是subdomain.domain.com

加载subdomain.domain.com/时,应提供静态文件

加载subdomain.domain.com/example1/req1/req2时,应路由到127.0.0.1:3000/req1/req2

加载subdomain.domain.com/example2/req1/req2时,应该路由到127.0.0.1:8080/req1/req2

但我的配置将subdomain.domain.com/example1/req1/req2路由到127.0.0.1:3000/example1/req1/req2,从而产生错误。nodejs返回无法获取/example1


我应该如何正确地编写这个nginx conf文件?

尝试在位置块中使用rewrite指令

比如:

location /example1/ {
  rewrite     ^/example1/(.*)$ /$1 break;
  proxy_pass  http://xxxxxx;
}

您可以在

查看与重写模块相关的文档,您需要将下面的代码片段添加到nginx.conf中

    server subdomain.domain.com;
        location / {
            rewrite     ^/example1/(.*)$ /$1 break;
            proxy_pass http://127.0.0.1:3000;
        }
    }

nginx-t?nginx-t测试的任何结果都是成功的。如果其中一个答案解决了您的问题,请用左边的复选框将其标记为成功!如果你自己找到了答案,或者是通过一些组合找到了答案,你也可以回答你自己的问题,并在2天左右的时间后做标记!这增加了你的知识,可以帮助你在网站上赢得声誉!