Modify HTML Pages Returned by Nginx Reverse Proxy – How to Guide

nginxreverse-proxy

I have a reverse proxy setup for access to a third party application located inside a intranet from the internet.
Let's say this application is on the URL:

https://internalserver:8080/ (reachable only from the intranet)

and the proxy is on:

https://proxyserver/ (reachable from any place in the world)

The proxy is managed by nginx and is working ok. When the user accesses https://proxyserver/
they get the content of the app at https://internalserver:8080/.

The problem is that the application is writing absolute URLs in the HTML response so, when the user clicks a link to a new page the browser is trying to locate the page with its internal name, e.g.
https://internalserver:8080/somepage
instead of
https://proxyserver/somepage.

I know this is a program bug, but I'm not able to modify the program.

Can I intercept the response, modify the URLs and send it (modified) to the final client with nginx? Or maybe with another tool?

EDIT: I saw this question before, but my case is more specific, the quoted question ask for a generic modification. In that case the fast-cgi ad hoc program is the best solution, what I want is a more specific solution for (I think) a more common scenario. while a fast-cgi program can work I´m looking for a easiest and maybe stronger and proved into the real world, solution for this scenario.

Best Answer

Here is an official Nginx Video on YouTube which demonstrates Inline Content Rewriting.

https://youtu.be/7Y7ORypoHhE?t=20m22s

Indeed with sub_filter

http://nginx.org/en/docs/http/ngx_http_sub_module.html

In your case, you're looking at something like:

location / {
sub_filter_once off;
sub_filter_types text/html;
sub_filter "https://internalserver:8080" "https://proxyserver";
}
Related Topic