Welcome! Log In Create A New Profile

Advanced

Re: Module vars set inside if's are not set

agentzh
November 24, 2009 09:40PM
On Tue, Nov 24, 2009 at 12:38 PM, Mark Maunder <mark@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

_______________________________________________
nginx mailing list
nginx@nginx.org
http://nginx.org/mailman/listinfo/nginx
Subject Author Posted

Module vars set inside if's are not set

Mark Maunder November 23, 2009 11:46PM

Re: Module vars set inside if's are not set

Igor Sysoev November 24, 2009 04:06PM

Re: Module vars set inside if's are not set

Mark Maunder November 24, 2009 05:28PM

Re: Module vars set inside if's are not set

agentzh November 24, 2009 09:40PM

Re: Module vars set inside if's are not set

Mark Maunder November 24, 2009 09:54PM

Re: Module vars set inside if's are not set

agentzh November 24, 2009 09:56PM



Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 306
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready