转发 HTTP 请求并直接服务器返回

发布于 2024-08-21 20:45:40 字数 902 浏览 5 评论 0原文

我的服务器分布在多个数据中心,每个数据中心存储不同的文件。我希望用户能够通过单个域访问所有服务器上的文件,并让各个服务器将文件直接返回给用户。

下面是一个简单的例子:
1)用户浏览器请求 http://www.example.com/files/file1.zip< /a>
2) 请求根据 example.com 的 DNS A 记录发送至服务器 A。
3) 服务器A分析请求并计算出/files/file1.zip存储在服务器B上。
4) 服务器A将请求转发给服务器B。
5) 服务器 B 直接将 file1.zip 返回给用户,而不经过服务器 A。

注意:步骤 4 和 5 必须对用户透明,并且不能涉及向用户发送重定向,因为这会违反单个域的要求。

根据我的研究,我想要实现的目标称为“直接服务器返回”,这是负载平衡的常见设置。有时也称为半反向代理。

对于步骤 4,听起来我需要进行 MAC 地址转换,然后将请求传递回网络,对于服务器网络外部的服务器,将需要隧道。

对于步骤 5,我只需根据负载平衡设置中的真实服务器配置服务器 B。也就是说,服务器 B 应该在环回接口上拥有服务器 A 的 IP 地址,并且它不应该应答对该 IP 地址的任何 ARP 请求。

我的问题是如何真正实现步骤4?

我发现了大量的硬件和软件可以在第 4 层实现简单的负载平衡,但这些解决方案存在缺陷,无法处理我需要的自定义路由。看来我需要推出自己的解决方案。

理想情况下,我想在 Web 服务器级别(即 PHP 或 C# / ASP.net)进行路由/转发。不过,我愿意在较低级别(例如 Apache 或 IIS)或更低级别(即在所有内容之前使用自定义代理服务)执行此操作。

I have servers spread across several data centers, each storing different files. I want users to be able to access the files on all servers through a single domain and have the individual servers return the files directly to the users.

The following shows a simple example:
1) The user's browser requests http://www.example.com/files/file1.zip
2) Request goes to server A, based on the DNS A record for example.com.
3) Server A analyzes the request and works out that /files/file1.zip is stored on server B.
4) Server A forwards the request to server B.
5) Server B returns file1.zip directly to the user without going through server A.

Note: steps 4 and 5 must be transparent to the user and cannot involve sending a redirect to the user as that would violate the requirement of a single domain.

From my research, what I want to achieve is called "Direct Server Return" and it is a common setup for load balancing. It is also sometimes called a half reverse proxy.

For step 4, it sounds like I need to do MAC Address Translation and then pass the request back onto the network and for servers outside the network of server A tunneling will be required.

For step 5, I simply need to configure server B, as per the real servers in a load balancing setup. Namely, server B should have server A's IP address on the loopback interface and it should not answer any ARP requests for that IP address.

My problem is how to actually achieve step 4?

I have found plenty of hardware and software that can do this for simple load balancing at layer 4, but these solutions fall short and cannot handle the kind of custom routing I require. It seems like I will need to roll my own solution.

Ideally, I would like to do the routing / forwarding at the web server level, i.e. in PHP or C# / ASP.net. However, I am open to doing it at a lower level such as Apache or IIS, or at an even lower level, i.e. a custom proxy service in front of everything.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

莫相离 2024-08-28 20:45:40

请原谅我的无知,但为什么不设置服务器 A 通过 NFS 或 SMB 挂载位于其他服务器上的文件,具体取决于您是否使用 UNIX 变体,还是使用 Windows?

似乎您想要做的事情过于复杂,而这些事情可能非常简单。此外,使用网络安装的文件将允许您在将来需要时将这些文件安装到其他计算机上。此时,您可以在服务器 A(以及服务器 x、y 和 z,它们也都从服务器 B 挂载文件)前面放置一个负载均衡器。

当然,这并不能解决返回时绕过服务器 A 的问题,从技术上讲,服务器 A 将返回文件而不是服务器 B,但如果将负载均衡器放在 A 前面,那么 A 无论如何都会成为 B,所以从技术上讲,B 仍然会返回文件,因为负载均衡器将使用直接服务器返回(这是很长一段时间以来的标准功能)。

如果我确实错过了什么,请详细说明。

编辑:是的,我知道这是近三年前发布的。那好吧。

Forgive my ignorance, but why not setup Server A to mount the files that are located on the other servers either via NFS or SMB, depending on whether you're using a unix variant, or whether you're using Windows?

Seems like what you're trying to do is overly complicate something that could be very simple. In addition, using network-mounted files will allow you to mount those files on additional machines in the future when you need them. At that point, then you could put a load balancer in front of server A (and servers x, y, and z, which also all mount files from server B).

Granted this would not solve the problem of bypassing server A on the return, technically server A would be returning the file instead of server B, but if a load balancer were to be put in front of A, then A would become B anyways, so technically B would still be returning the file, because the load balancer would use direct server return (its a standard feature for a long time now).

If I did miss something, please do elaborate.

Edit: Yes I realize this was posted nearly 3 years ago. Oh well.

从来不烧饼 2024-08-28 20:45:40

为什么不发送状态代码 307 临时重定向的 HTTP 响应?

此时客户端将重新请求正确的指定服务器。

我知道您想要一个域,但您可以拥有两个单独的子域和一个公共域。

例如:

example.com 有 IP1、IP2、IP3。

example1.example.com 有 IP1
example2.example.com 有 IP2
example3.example.com 有 IP3

如果请求到达的服务器无法自行处理,它将转发用户向正确的特定服务器发出另一个请求。顺便说一句,HTTP 浏览器将透明地遵循此重定向。

Why not send an HTTP response of status code 307 Temporary Redirect?

At that point the client will re-request to the correct specified server.

I know you want a single domain, but you could have both individual subdomains plus a single common domain.

For example:

example.com has IP1, IP2, IP3.

example1.example.com has IP1
example2.example.com has IP2
example3.example.com has IP3

If the request comes to a server that it can't handle itself, it will forward the user to make another request to the correct specific server. An HTTP browser will follow this redirect transparently by the way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文