ngx_lua location capture issue

agentzh agentzh at gmail.com
Wed Oct 19 02:40:01 UTC 2011


On Wed, Oct 19, 2011 at 1:59 AM, Nginx User <nginx at nginxuser.net> wrote:
> Jumped the gun there. There's a sting in the tail.
>
> # GET /test_page.php
>       location ~ .+\.php$ {
>               location ~ ^/test_page\.php$ {
>                       access_by_lua '
>                               local res = ngx.location.capture("/phpids")
>                               if res.status == ngx.HTTP_OK then
>                                       ngx.exec("@proxy")
>                               end
>                               if res.status == ngx.HTTP_FORBIDDEN then
>                                       ngx.exit(res.status)
>                               end
>                               ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
>                       ';
>               }
>
>               rewrite_by_lua 'ngx.exec("@proxy");';
>       }
>
> Will result in the rewrite by lua line being executed instead of the
> access by lua within the sub location.  Changing the access by lua in
> the sub location to rewrite by lua executes the script.
>

The inner location (i.e., location "^/test_page\.php$") automatically
inherits the rewrite_by_lua directive defined in the outer location
(i.e., location ".+\.php$"). So your rewrite_by_lua code runs before
your access_by_lua code in the inner location. And further, your
rewrite_by_lua code does an internal redirection and thus your
access_by_lua never has a chance to run.

Apparently you do not want the inner location to inherit the outer
rewrite_by_lua setting, so just add the following line to your inner
location:

    rewrite_by_lua return;

This will override the rewrite_by_lua directive in the outer scope.

> In any case, my original config is the preferred to get workingas I
> need the request params to be passed along the redirection chain.
>

See my last reply :)

> Better wait until agentzh wakes up in his timezone.
>

10:41 AM here already ;)

Best,
-agentzh



More information about the nginx mailing list