atom feed6 messages in ru.sysoev.nginxRe: Module vars set inside if's are n...
FromSent OnAttachments
Mark MaunderNov 23, 2009 8:37 pm 
Igor SysoevNov 24, 2009 12:57 pm 
Mark MaunderNov 24, 2009 2:22 pm 
agentzhNov 24, 2009 6:34 pm 
Mark MaunderNov 24, 2009 6:45 pm 
agentzhNov 24, 2009 6:48 pm 
Subject:Re: Module vars set inside if's are not set
From:agentzh (agen@gmail.com)
Date:Nov 24, 2009 6:34:25 pm
List:ru.sysoev.nginx

On Tue, Nov 24, 2009 at 12:38 PM, Mark Maunder <ma@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:

Have fun with "if" and "echo"!