Module vars set inside if's are not set

agentzh agentzh at gmail.com
Wed Nov 25 05:34:48 MSK 2009


On Tue, Nov 24, 2009 at 12:38 PM, Mark Maunder <mark at feedjit.com> wrote:
>
> The variable is set fine outside an 'if' statement but when the if evaluates
> to true, the variable is not set.
>

Behind the scene, the if directive's "block" is implemented as a
dynamic location (of type LIF, not LOC). It's very different from the
if statements' blocks one usually finds in an everyday programming
language :)

> I have duplicated this behaviour with the echo module as follows:
> This will print "No":
> location ^~ /echo/ {
>        set $myText "No";
>        if ($remote_addr ~* '000'){
>                set $myText "Yes";
>        }
>        echo $myText;
> }
>

Tests have shown that if the "if" condition succeeds here, nginx will
return a 404 error page because of the lack of content handler in that
anonymous location corresponding to the "if" block :)

I've just released "echo" module v0.22 to allow you to use "echo"
directly in "if" blocks and the following should work as expected:

    location ^~ /if {
        set $res miss;
        if ($arg_val ~* '^a') {
            set $res hit;
            echo $res;
        }
        echo $res;
    }

GET /if?val=abc yields the "hit" output while GET /if?val=bcd yields "miss" :)

It seems that the standard proxy_pass directive also works in if blocks (yay!):

    location ^~ /if {
        set $res miss;
        if ($arg_val ~* '^a') {
            set $res hit;
            proxy_pass $scheme://127.0.0.1:$server_port/foo?res=$res;
        }
        proxy_pass $scheme://127.0.0.1:$server_port/foo?res=$res;
    }
    location /foo {
        echo "res = $arg_res";
    }

You will get the expected behavior :)

Feel free to take a look at more related (passing) test cases that
I've included in the "echo" module's test suite:

  http://github.com/agentzh/echo-nginx-module/blob/master/test/t/if.t

Have fun with "if" and "echo"!

Regards,
-agentzh




More information about the nginx mailing list