【问题标题】:Nginx How do i route to reverse proxy by locationNginx如何按位置路由到反向代理
【发布时间】:2021-05-03 00:07:07
【问题描述】:

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

所以,假设我的域是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返回Cannot GET /example1)

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

【问题讨论】:

  • nginx -t 的任何结果?
  • nginx -t 测试成功
  • 如果其中一个答案解决了您的问题,请将其标记为这样,并在其左侧打勾!如果您自己或通过某种组合找到答案,您也可以回答自己的问题并在 2 天左右后标记!这增加了知识体系,可以帮助您在网站上获得声誉!

标签: nginx nginx-reverse-proxy nginx-config


【解决方案1】:

尝试在位置块中使用重写指令。

类似:

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

您可以在http://nginx.org/en/docs/http/ngx_http_rewrite_module.html查看与重写模块相关的文档

【讨论】:

    【解决方案2】:

    您需要将以下代码 sn-p 添加到您的 nginx.conf 中。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-14
      • 1970-01-01
      • 2020-12-18
      • 1970-01-01
      • 2011-06-08
      • 2020-09-29
      • 1970-01-01
      相关资源
      最近更新 更多