Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx sub_filter rewrites?

Tags:

nginx

I am trying to rewrite body links of a proxied page using something like this:

    sub_filter http://proxied.page.come http://local.page.com;
    sub_filter_once off;

Is this the way to go at all ? What is the difference between the sub_filter module and the substitutions_filter

Also can variable be used in the sub_filter delcaration ?

like image 812
silkAdmin Avatar asked Sep 17 '12 16:09

silkAdmin


2 Answers

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;
like image 134
brianf Avatar answered Sep 24 '22 07:09

brianf


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.

like image 32
Maxim Dounin Avatar answered Sep 20 '22 07:09

Maxim Dounin