简体   繁体   English

如何使用 http-proxy-middleware / node-http-proxy 作为反向代理?

[英]How to use http-proxy-middleware / node-http-proxy as a reverse proxy?

I'm investigating the use of http-proxy-middleware / node-http-proxy as a reverse proxy.我正在研究使用 http-proxy-middleware / node-http-proxy 作为反向代理。 Does anyone know if this is really possible?有谁知道这是否真的可能?

I've already setup http-proxy-middleware so that I can proxy a request through it (the results are displayed in an iframe), and I'm also able to modify the request headers and html results.我已经设置了 http-proxy-middleware 以便我可以通过它代理请求(结果显示在 iframe 中),并且我还能够修改请求标头和 html 结果。 Specifically, I'm setting the host/origin headers and rewriting the result to change embedded links so that they go through the proxy as well.具体来说,我正在设置主机/源标头并重写结果以更改嵌入式链接,以便它们也通过代理 go。

But, some links are generated by js, and rewriting javascript responses seems to be very difficult to do correctly.但是,有些链接是js生成的,重写javascript响应好像很难做对。

Is there a way to do this without rewriting links?有没有办法在不重写链接的情况下做到这一点? Ie, is there any method to configure the iframe to automatically send all requests through the proxy?即,是否有任何方法可以配置 iframe 自动通过代理发送所有请求?

Or maybe this is not really possible, and I'd need to use a full proxy like Squid?或者这实际上不可能,我需要使用像 Squid 这样的完整代理吗?

Thanks!谢谢!

This does seem to be possible, to a limited extent.在有限的范围内,这似乎是可能的。 http-proxy-middleware can be configured to edit response headers and to also rewrite the response body, so that links can be rewritten to use the proxy URL. XmlHttpRequest and fetch() requests can also be intercepted to rewrite the requests to use the proxy URL. http-proxy-middleware 可以配置为编辑响应头,也可以重写响应主体,这样链接就可以重写为使用代理 URL。XmlHttpRequest 和 fetch() 请求也可以被拦截以重写请求以使用代理URL。

Use of a reverse proxy should be 100% transparent to clients and your application code, with zero code changes.反向代理的使用应该对客户端和您的应用程序代码 100% 透明,代码更改为零。 So perhaps it is a design problem where I can clarify requirements for you.所以也许这是一个设计问题,我可以为您澄清要求。

URL DESIGN URL 设计

As an API example, I might design URLs as follows for an API:作为 API 的示例,我可以为 API 设计如下 URL:

Note that the public URL of the API is actually that of a route within the reverse proxy.请注意,API 的公共 URL 实际上是反向代理中的路由。

An inte.net client would only ever use the public URL. If the internal API ever returns URLs to inte.net clients, it needs to be configured to use the public URL. inte.net 客户端只会使用公共 URL。如果内部 API 曾经向 inte.net 客户端返回 URL,则需要将其配置为使用公共 URL。

REVERSE PROXIES反向代理

The most mature options are probably the nginx based ones, which provide both declarative routing and also the ability to write any logic you like via plugins.最成熟的选项可能是基于 nginx 的选项,它提供声明式路由以及通过插件编写您喜欢的任何逻辑的能力。 There are plenty of examples in Curity guides , which may make you aware of some use cases Curity 指南中有大量示例,可能会让您了解一些用例

A mainstream option is to use the proxy-pass directive to route to an internal URL. The same pattern should work for the node RP you mention, though for simple tasks no custom logic should be needed.主流选项是使用proxy-pass指令路由到内部 URL。相同的模式应该适用于您提到的节点 RP,但对于简单的任务不需要自定义逻辑。

Header configuration is a common thing to do in the RP, eg to ensure that the component receives the original client's IP address, rather than that of the RP, but that is often optional. Header 配置在 RP 中很常见,例如确保组件接收原始客户端的 IP 地址,而不是 RP 的地址,但这通常是可选的。

MISBEHAVING BACKEND COMPONENT行为不端的后端组件

Perhaps this is the root of the problem - if a website returns the internal URL, eg in redirects or image URLs, then it is wrong.也许这就是问题的根源——如果网站返回内部 URL,例如在重定向或图像 URL 中,那么它是错误的。 Many tech stacks will have a property such as BaseUrl that fixes this.许多技术栈都会有一个属性,例如BaseUrl来解决这个问题。

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

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