openresty / replace-filter-nginx-module

Streaming regular expression replacement in response bodies
260 stars 68 forks source link

Issue faced with location if and replace_filter #10

Closed makailol closed 10 years ago

makailol commented 10 years ago

Hi,

If we use replace_filter directive inside the location if , it ignores the rules defined in location context.

For example, with below configuration , only replace_filter rule which is inside if will be executed.


location = /echo-replace {
    set $rf 1;
    default_type 'text/html';
    echo "Line1
"; echo "Line2"; replace_filter 'Line1' 'Replaced1' gi; if ($rf = 1) { replace_filter 'Line2' 'Replaced2' gi; } }

Does it point to a bug or is it expected behavior ?

Thanks, Makailol

agentzh commented 10 years ago

@makailol It is the expected behavior. When merging the configurations of two different scopes, the configuration of the inner scope will override the configurations of the outer scopes for the inner scope. The "merge" does not work incrementally.

makailol commented 10 years ago

Thanks for explanation .

I was thinking that in case of location and location if, configuration can be merged at least in case of replace_filter because I was trying to define once common rule and another conditional .

agentzh commented 10 years ago

@makailol Then you'll need a way to achieve the current behavior when you want to override the rules in the outer scopes.

For your use case, you can just put common rules into a special .conf file, say, common.conf, and just include the file in the scopes you want it.

makailol commented 10 years ago

Yes. That is an option.

For now there are only two common rules and only two conditions so I have just included it in both conditions.

I have about 8-10 replace_filter rules (could not merge as all are different), could it slow the response?

Thanks for prompt response and good suggestions as always :)

agentzh commented 10 years ago

@makailol The more rules you add, the more computation will be involved. So it will surely add performance penalty. We'll start optimizing the underlying libsregex library soon (the focus for libsregex has been on features and correctness and we'll focus on absolute performance soon).

makailol commented 10 years ago

So far I have enjoyed using replace_filter when it requires to change the response content real time. So I will be glad to see new features and improvements in the sregex library.

As you said the rules can add performance penalty but could you suggest if is there any other better option to change the response real time?

agentzh commented 10 years ago

@makailol Everything does regex search and replacement adds a performance penalty, just more or less. I started working on this because there was and still is no true equivalent for this functionality out there.

makailol commented 10 years ago

Yes there is no equivalent modules for this kind of functionality.

mod_Pagespeed and cloudflare optimize the website and they usually change the html response by replacing some tag's attributes with different name/value etc. Does this also cause the performance penalty?

Thanks for sharing your experience and knowledge with me.

agentzh commented 10 years ago

@makailol I think I've made it clear enough in my previous comment. In case you miss anything, please read it again. I think we're already far off topic in this ticket. I'm closing this.