【问题标题】:Routing request to multiple services based on baseURI基于baseURI将请求路由到多个服务
【发布时间】:2019-04-26 16:37:27
【问题描述】:

我有多个服务在Nginx 下运行。所有这些服务都在不同的端口上。

如何根据初始 base URI 将请求重定向到特定服务?

例如对于 URL https://my-domain/serviceA/v1/schema 到端口 8181/v1/schemahttps://my-domain/serviceB/v1/api/schema 到端口9191/v1/api/schema

到目前为止,我能够使用 location 将请求转发到基于初始路径的特定端口,但无法从 URI 中删除 /serviceA/serviceB

【问题讨论】:

  • proxy_pass directive 的文档中有一个示例。
  • 是的,我试过这样:location /serviceA { proxy_pass 10.5.121.6:8181; .... 其他一些配置... ...... } 但问题在于,如果我像 location /serviceA/v1 这样提供它,它不会根据文档从 URL 中删除“/serviceA/” { proxy_pass 10.5.121.6:8181/v1; .... 其他一些配置... ...... } 然后它将替换 url。

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


【解决方案1】:

proxy_pass 语句可以包含可选的 URI 组件。详情请见this document

例如:

location /serviceA/ {
    proxy_pass http://192.168.0.2:8080/;
    ...
}

URI 组件是proxy_pass 语句中的最后一个/。 URI /serviceA/foo 在传递到上游之前被转换为 /foo。在 location 值上使用尾随 / 以进行正确翻译。

【讨论】:

    猜你喜欢
    • 2023-03-26
    • 1970-01-01
    • 2014-05-06
    • 2019-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-20
    • 1970-01-01
    相关资源
    最近更新 更多