9

with no evil plans I am trying to mirror a site under other domain, while changing a few strings on the fly.

I set up nginx on a new host to serve as a RP to the master site. This allows to set one rule of replacement:

sub_filter      Originalstring  'new string';
sub_filter_once off;

However, I'd like to have several rules running, which sub_filter allows only one per location.

What can be a solution here, if any?

Sergiks
  • 249
  • 3
  • 5
  • 11

4 Answers4

7

Unlike the previous one, the current version of nginx allows to create multiple sub filter rules under one location directive. Like so

location / {
   resolver   8.8.8.8;
   proxy_pass http://original-domain.com;
   proxy_set_header Accept-Encoding "";
   proxy_set_header Host original-domain.com;

   subs_filter_types text/css text/xml text/css;
   subs_filter a.original-domain.com b.mydomain.com;
   subs_filter http://$host https://$host;
}

You can add up to 255 filters. See http://wiki.nginx.org/HttpSubsModule

Make sure you have subs_filter installed with nginx by running this command nginx -V. Look for --with-http_sub_module in the output.

Raees Iqbal
  • 171
  • 1
  • 4
  • Are you using the built in ngx_http_sub_module (http://nginx.org/en/docs/http/ngx_http_sub_module.html) or the 3rd party plugin http://wiki.nginx.org/HttpSubsModule? – Sascha Mar 31 '15 at 13:49
  • I'm using the built-in module. You can check your nginx's error.log file to figure out more about the error in your code. – Raees Iqbal Apr 06 '15 at 13:50
  • `subs_filter` does not support compressed responses, while `sub_filter` does, if coupled with `gunzip on;` – Valerio Aug 29 '15 at 22:37
  • 1
    @mrgamer when you add `proxy_set_header Accept-Encoding "";`, the response from original server should be without any encoding. So, `subs_filter` should work everywhere when this rule added. – Raees Iqbal Aug 31 '15 at 09:53
  • `"should"` being the most important word here. Sometimes old/proprietary systems behave in non-standard ways. Anyway in NGINX 1.9.4 you can use multiple `sub_filter`, so there is not any reason left to use that submodule – Valerio Aug 31 '15 at 10:21
2

Check HttpSubsModule module instead. Excerpt from documentation:

Several substitution rules per location can be specified

AlexD
  • 8,747
  • 2
  • 29
  • 38
  • 1
    Thanks, AlexD, I am aware of the HttpSubsModule, which is not installed on the shared hosting, I am trying to implement the feature at. Looking for a possible hack or a workaround for the available HttpSubModule. – Sergiks Jun 20 '11 at 09:52
2

now you can upgrade nginx to 1.9.4

*) Feature: multiple "sub_filter" directives can be used simultaneously.

see full change log here: http://nginx.org/en/CHANGES

Zandy
  • 36
  • 1
0

I guess you may setup several locations or server_names with one sub_filter per earch and proxy the request through them.

Alexander Azarov
  • 3,550
  • 21
  • 19