简体   繁体   English

yarp 重写硬编码的服务 url

[英]yarp rewriting hardcoded service urls

I don't know, if my question is well formulated, but I try... I'm using yarp as a reverse proxy.我不知道,如果我的问题表述得很好,但我尝试......我使用 yarp 作为反向代理。 Behind the proxy there is a asp.net core service (order-service) on port 5048 eg在代理后面有一个 5048 端口上的 asp.net 核心服务(order-service),例如

So the config looks like the following:所以配置如下所示:

"ReverseProxy": {
  "Routes": {
    "orderService": {
      "ClusterId": "orderServiceProps",
      "Match": {
        "Path": "/order-service/{**Remainder}"
      },
      "Transforms": [
        {
          "PathRemovePrefix": "/order-service"
        }
      ]
    }
  },
  "Clusters": {
    "orderServiceProps": {
      "Destinations": {
        "destination1": {
          "Address": "http://localhost:5048"
        }
      }
    }
  }
}

If I type eg http://localhost:8080/order-service/api/collection the request gets forwarded to http://localhost:5048/api/collection All well so far.如果我输入例如 http://localhost:8080/order-service/api/collection 请求被转发到 http://localhost:5048/api/collection 到目前为止一切顺利。 But the order-service also has a simple html gui, which is showing some simple links to some special endpoints.但是 order-service 也有一个简单的 html gui,它显示了一些到一些特殊端点的简单链接。
在此处输入图像描述

Now if I click on eg the settings link, the service navigates to http://localhost:5048/settings but I want the service navigates to http://localhost:8080/order-service/settings现在,如果我单击例如设置链接,服务导航到 http://localhost:5048/settings 但我希望服务导航到 http://localhost:8080/order-service/settings
I don't know if I can reach such a bahavior.不知道能不能达到这样的境界。
I don't want the user is seeing the 5048 in his browser!我不希望用户在他的浏览器中看到 5048!
And can I do this without let the service know that it is behind a proxy?我可以在不让服务知道它在代理后面的情况下做到这一点吗?

On the order-service you should configure the X-Forwarded middleware like this:在 order-service 上,您应该像这样配置 X-Forwarded 中间件:

// in ConfigureServices
services.Configure<ForwardedHeadersOptions>(options =>
{
    options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
    options.KnownNetworks.Clear();
    options.KnownProxies.Clear();
});

// in Configure
app.UseForwardedHeaders();

Check this for more info: Configure ASP.NET Core to work with proxy servers and load balancers检查此以获取更多信息: 配置 ASP.NET Core 以使用代理服务器和负载平衡器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM