A more general substitution filter module


#1

nginx_substitutions_filter

nginx_substitutions_filter is a filter module which can do both regular
expression and fixed string substitutions on response bodies. This
module is
quite different from the Nginx’s Substitution Module. It scans the
output
chains buffer and matches string line by line, just like Apache’s
mod_substitute.

To install, compile nginx with the following option:

–add-module=/path/to/this/directory

EXAMPLE

subs_filter_types text/html text/css text/xml;
subs_filter http://st(\d*).example.com http://s.example.com ir;
subs_filter http://a.example.com http://s.example.com;

DIRECTIVES

subs_filter_types

syntax: subs_filter_types mime-type [mime-type ...]
default: subs_filter_types text/html
context: http, server, location
description: subs_filter_types is used to specify which content 

types should
be checked for subs_filter. The default is only text/html.

subs_filter

syntax: subs_filter source_str destination_str [gior]
default: none
context: http, server, location
description: subs_filter allows replacing source stringe(regular 

expression
or fixed) in the nginx response with destination string.
Substitution
text may contain variables. More than one substitution rules per
location is supported.
The meaning of the third flags are:
g(default): Replace all the match strings.
i: Perform a case-insensitive match.
o: Just replace the first one.
r: The pattern is treated as a regular expression, default
is fixed
string.

Questions/patches may be directed to Weibin Y., removed_email_address@domain.invalid.


#2

Two bug fixed in some compiler, this is the patch:

Index: ngx_http_subs_filter_module.c

— ngx_http_subs_filter_module.c
+++ ngx_http_subs_filter_module.c
@@ -21,7 +21,7 @@
ngx_str_t match;
#if (NGX_PCRE)
ngx_regex_t *match_regex;

  • ngx_int_t *captures;
  • int *captures;
    ngx_int_t ncaptures;
    #endif

@@ -710,8 +710,8 @@
}
if (pair->captures == NULL || pair->ncaptures == 0) {
pair->ncaptures = OVECCOUNT;

  •            pair->captures =ngx_palloc(r->pool,
    
  •                    OVECCOUNT * sizeof(ngx_int_t));
    
  •            pair->captures = (int *)(ngx_int_t)ngx_palloc(r->pool,
    
  •                    OVECCOUNT * sizeof(int));
           }
    
           while (1) {
    

@@ -726,7 +726,7 @@
break;

             rc = ngx_regex_exec(pair->match_regex, &line,
  •                    pair->captures, pair->ncaptures);
    
  •                    (int *)pair->captures, pair->ncaptures);
               if (rc == NGX_REGEX_NO_MATCHED)
                   break;
               else if(rc < 0) {
    

@@ -1053,7 +1053,7 @@
break;
}

  •        ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
    
  •        ngx_log_debug2(NGX_LOG_DEBUG_HTTP, log, 0,
                   "Left bytes:%d, p:%p ", bytes, p);
           /*There is nothing left in this buffer.*/
           if (cl->buf->last - p <= 0)
    

Weibin Y. wrote:

nginx_substitutions_filter

nginx_substitutions_filter is a filter module which can do both regular
expression and fixed string substitutions on response bodies. This
module is
quite different from the Nginx’s Substitution Module. It scans the
output
chains buffer and matches string line by line, just like Apache’s
mod_substitute.

To install, compile nginx with the following option:

–add-module=/path/to/this/directory

EXAMPLE

subs_filter_types text/html text/css text/xml;
subs_filter http://st(\d*).example.com http://s.example.com ir;
subs_filter http://a.example.com http://s.example.com;

DIRECTIVES

subs_filter_types

syntax: subs_filter_types mime-type [mime-type ...]
default: subs_filter_types text/html
context: http, server, location
description: subs_filter_types is used to specify which content 

types should
be checked for subs_filter. The default is only text/html.

subs_filter

syntax: subs_filter source_str destination_str [gior]
default: none
context: http, server, location
description: subs_filter allows replacing source stringe(regular 

expression
or fixed) in the nginx response with destination string.
Substitution
text may contain variables. More than one substitution rules per
location is supported.
The meaning of the third flags are:
g(default): Replace all the match strings.
i: Perform a case-insensitive match.
o: Just replace the first one.
r: The pattern is treated as a regular expression, default
is fixed
string.

Questions/patches may be directed to Weibin Y., removed_email_address@domain.invalid.