Nginx sub_filter rewrites?

This is a perfectly valid way to rewrite links on a proxied page. "The ngx_http_sub_module module is a filter that modifies a response by replacing one specified string by another" (single substitution)

The third party nginx_substitutions_filter is a filter module which can do both regular expression and fixed string substitutions on response bodies

So the third party module can be used to substitute multiple strings, using regex and variables. e.g.:

subs_filter_types text/css text/xml;
subs_filter http(s)?://(www.)?proxied.page.com/     http$1://$http_host/ r;
subs_filter http(s)?://(www.)?proxied2.page.com/    http$1://$http_host/ r;

Ideally, you should ask backend to write correct links. While it is possible to fix some simple cases using sub filter, it is not something generally possible (e.g. if returned data isn't text but e.g. flash code).

You can use variables in a replacement string in sub_filter (but not in string to match in original response), it's explicitly documented:

A replacement string can contain variables.

As for subs filter - it's a 3rd party module which is expected to be more powerful, though may contain more bugs. As long as standard sub filter is enough for you - you probably don't want to use 3rd party subs filter.

Tags:

Nginx