33

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.

1

2 Answers 2

29

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";
}
1
  • Does it has any performance penalty?
    – Krunal
    Mar 11, 2020 at 10:11
8

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

The ngx_http_sub_module module is a filter that modifies a response by replacing one specified string by another.

This module is not built by default, it should be enabled with the --with-http_sub_module configuration parameter.

Example Configuration

sub_filter      </head>
    '</head><script language="javascript" src="$script"></script>';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.