c# mvc将请求重新路由到不同的服务器

时间:2021-07-20 10:35:31

I have a web application which is a mesh of a few different servers and 1 server is the front-end server which handles all request external incoming requests.

我有一个web应用程序,它是几个不同服务器的网格,一个服务器是处理所有请求的前端服务器。

So some of these request will have to be passed along to different servers and ideally the only thing I want to change is the host and Uri fields of these request. Is there a way to map an entire incoming request to a new outgoing request and just change a few fields?

因此,这些请求中的一些必须传递到不同的服务器上,理想情况下,我只想更改这些请求的主机和Uri字段。是否有一种方法可以将整个传入请求映射到新的传出请求并只更改几个字段?

I tried something like this:

我试过如下方法:

// some controller
public HttpResponseMessage get()
{
    return this.Request.Rewrite("192.168.10.13/api/action");
}


//extension method Rewrite
public static HttpResponseMessage Rewrite(this HttpRequestMessage requestIn, string Uri) {
    HttpClient httpClient = new HttpClient(new HttpClientHandler());
    HttpRequestMessage requestOut = new HttpRequestMessage(requestIn.Method, Uri);
    requestOut.Content = requestIn.Content;

    var headerCollection = requestIn.Headers.ToDictionary(x => x.Key, y => y.Value);
    foreach (var i in headerCollection)
    {
        requestOut.Headers.Add(i.Key, i.Value);
    }

    return httpClient.SendAsync(requestOut).Result;
}

The issue I am having is that this has a whole slew of issues. If the request is a get Content shouldn't be set. THe headers are incorrect since it also copies things like host which shouldn't be touched afterwards etc.

我所面临的问题是,这有很多问题。如果请求是一个get内容,就不应该设置。标题是不正确的,因为它还复制了一些东西,比如主机,以后不应该被碰等等。

Is there an easier way to do something like this?

有没有更简单的方法来做这样的事情?

2 个解决方案

#1


2  

I had to do this in C# code for a Silverlight solution once. It was not pretty.

我必须在c#代码中为Silverlight解决方案做一次。这不是漂亮。

What you're wanting is called reverse proxying and application request routing.

您需要的是反向代理和应用程序请求路由。

First, reverse proxy solutions... they're relatively simple.

首先,反向代理解决方案…他们是相对简单的。

Here's Scott Forsyth and Carlos Aguilar Mares guides for creating a reverse proxy using web.config under IIS. Here's a module some dude named Paul Johnston wrote if you don't like the normal solution. All of these focus on IIS.

下面是Scott Forsyth和Carlos Aguilar Mares网站创建反向代理指南。在IIS配置。这是一个叫做保罗·约翰斯顿的模块,如果你不喜欢它的正常解。所有这些都关注IIS。

Non-IIS reverse proxies are more common for load balancing. Typically they're Apache based or proprietary hardware. They vary from free to expensive as balls. Forgive the slang.

非iis反向代理更常见用于负载平衡。通常它们是基于Apache或私有硬件。它们从免费到昂贵不等。原谅的俚语。

To maintain consistency for the client's perspective you may need more than just a reverse proxy configuration. So before you go down the pure reverse proxy route... there's some considerations.

为了保持客户端透视图的一致性,您可能需要的不仅仅是反向代理配置。所以在你沿着完全反向的代理路径走之前。有一些考虑。

The servers likely need to share Machine Keys to synchronize view state and other stuff, and share the Session Store too.

服务器可能需要共享机器密钥来同步视图状态和其他东西,并共享会话存储。

If that's not consistent enough, you may want to implement session stickiness through Application Request Routing (look for Server Affinity), such that a given session cookie (or IP address, or maybe have it generate a token cookie) maps the user to the same server on every request.

如果不够一致,您可能希望通过应用程序请求路由(查找服务器关联)实现会话粘性,这样给定的会话cookie(或IP地址,或者让它生成令牌cookie)将用户映射到每个请求的同一服务器。

#2


0  

I also wrote a simple but powerful reverse proxy for asp.net / web api. It does exactly what you need.

我还为asp.net / web api编写了一个简单但功能强大的反向代理。它完全满足你的需要。

You can find it here: https://github.com/SharpTools/SharpReverseProxy

您可以在这里找到它:https://github.com/SharpTools/SharpReverseProxy

Just add to your project via nuget and you're good to go. You can even modify on the fly the request, the response, or deny a forwarding due to authentication failure.

只要通过nuget添加到你的项目中就可以了。您甚至可以动态地修改请求、响应或由于身份验证失败而拒绝转发。

Take a look at the source code, it's really easy to implement :)

看看源代码,它真的很容易实现:)

#1


2  

I had to do this in C# code for a Silverlight solution once. It was not pretty.

我必须在c#代码中为Silverlight解决方案做一次。这不是漂亮。

What you're wanting is called reverse proxying and application request routing.

您需要的是反向代理和应用程序请求路由。

First, reverse proxy solutions... they're relatively simple.

首先,反向代理解决方案…他们是相对简单的。

Here's Scott Forsyth and Carlos Aguilar Mares guides for creating a reverse proxy using web.config under IIS. Here's a module some dude named Paul Johnston wrote if you don't like the normal solution. All of these focus on IIS.

下面是Scott Forsyth和Carlos Aguilar Mares网站创建反向代理指南。在IIS配置。这是一个叫做保罗·约翰斯顿的模块,如果你不喜欢它的正常解。所有这些都关注IIS。

Non-IIS reverse proxies are more common for load balancing. Typically they're Apache based or proprietary hardware. They vary from free to expensive as balls. Forgive the slang.

非iis反向代理更常见用于负载平衡。通常它们是基于Apache或私有硬件。它们从免费到昂贵不等。原谅的俚语。

To maintain consistency for the client's perspective you may need more than just a reverse proxy configuration. So before you go down the pure reverse proxy route... there's some considerations.

为了保持客户端透视图的一致性,您可能需要的不仅仅是反向代理配置。所以在你沿着完全反向的代理路径走之前。有一些考虑。

The servers likely need to share Machine Keys to synchronize view state and other stuff, and share the Session Store too.

服务器可能需要共享机器密钥来同步视图状态和其他东西,并共享会话存储。

If that's not consistent enough, you may want to implement session stickiness through Application Request Routing (look for Server Affinity), such that a given session cookie (or IP address, or maybe have it generate a token cookie) maps the user to the same server on every request.

如果不够一致,您可能希望通过应用程序请求路由(查找服务器关联)实现会话粘性,这样给定的会话cookie(或IP地址,或者让它生成令牌cookie)将用户映射到每个请求的同一服务器。

#2


0  

I also wrote a simple but powerful reverse proxy for asp.net / web api. It does exactly what you need.

我还为asp.net / web api编写了一个简单但功能强大的反向代理。它完全满足你的需要。

You can find it here: https://github.com/SharpTools/SharpReverseProxy

您可以在这里找到它:https://github.com/SharpTools/SharpReverseProxy

Just add to your project via nuget and you're good to go. You can even modify on the fly the request, the response, or deny a forwarding due to authentication failure.

只要通过nuget添加到你的项目中就可以了。您甚至可以动态地修改请求、响应或由于身份验证失败而拒绝转发。

Take a look at the source code, it's really easy to implement :)

看看源代码,它真的很容易实现:)