简体   繁体   中英

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. 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. 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.

But, some links are generated by js, and rewriting javascript responses seems to be very difficult to do correctly.

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?

Or maybe this is not really possible, and I'd need to use a full proxy like 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.

Use of a reverse proxy should be 100% transparent to clients and your application code, with zero code changes. So perhaps it is a design problem where I can clarify requirements for you.

URL DESIGN

As an API example, I might design URLs as follows for an API:

Note that the public URL of the API is actually that of a route within the reverse proxy.

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.

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. There are plenty of examples in Curity guides , which may make you aware of some use cases

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.

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.

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. Many tech stacks will have a property such as BaseUrl that fixes this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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