ngx.exec() with named location not working.

726 views
Skip to first unread message

Makailol Charls

unread,
Mar 21, 2014, 9:07:04 AM3/21/14
to openre...@googlegroups.com
Hello,

I have been trying to jump to named location with ngx.exec().

rewrite_by_lua ' ngx.exec("@proxyA"); ';


    location @proxyA {
        proxy_pass http://someIP;
        more_set_headers 'Foo: bar';
    }

Here rewrite_by_lua is in server context. This returns 500 internal server error with below log.
[error] 3672#0: *4 rewrite or internal redirection cycle while redirect to named location "@proxyA"  .

I have noticed that in response I get header "Foo".

Would you suggest what mistake I have been doing here?

I have downloaded and built master copy of lua module in last month.

Thanks,
Makailol

Yichun Zhang (agentzh)

unread,
Mar 21, 2014, 7:31:30 PM3/21/14
to openresty-en
Hello!

On Fri, Mar 21, 2014 at 6:07 AM, Makailol Charls wrote:
> I have been trying to jump to named location with ngx.exec().
> rewrite_by_lua ' ngx.exec("@proxyA"); ';
> location @proxyA {
> proxy_pass http://someIP;
> more_set_headers 'Foo: bar';
> }
>

Note that unlike ngx_rewrite module's "rewrite" directive,
rewrite_by_lua used in the server {} level is just inherited by all
the location {} blocks. So the above configuration is equivalent to

location @proxyA {
rewrite_by_lua ' ngx.exec("@proxyA"); ';
proxy_pass http://someIP;
more_set_headers 'Foo: bar';
}

That is, location @proxyA also gets that rewrite_by_lua and keep
jumping to itself.

> Here rewrite_by_lua is in server context. This returns 500 internal server
> error with below log.
> [error] 3672#0: *4 rewrite or internal redirection cycle while redirect to
> named location "@proxyA" .
>

The error message clearly indicates what is happening here :)

Best regards,
-agentzh

Makailol Charls

unread,
Mar 22, 2014, 3:03:45 AM3/22/14
to openre...@googlegroups.com
Hello,

Thanks for your quick reply.

Would it be possible to use rewrite_by_lua twice?
For example, I have used rewrite_by_lua in server context for some common logic which will be inherited in each location block and now I want to write some additional code for specific location (code to jump on named based locations conditionally). Would this be possible or it will execute only  rewrite_by_lua which is in that location block (and won't execute the common code which is written in server context.) ?

Regards,
Makailol

Yichun Zhang (agentzh)

unread,
Mar 22, 2014, 12:59:49 PM3/22/14
to openresty-en
Hello!

On Sat, Mar 22, 2014 at 12:03 AM, Makailol Charls wrote:
> Would it be possible to use rewrite_by_lua twice?
> For example, I have used rewrite_by_lua in server context for some common
> logic which will be inherited in each location block and now I want to write
> some additional code for specific location (code to jump on named based
> locations conditionally). Would this be possible or it will execute only
> rewrite_by_lua which is in that location block (and won't execute the common
> code which is written in server context.) ?
>

Yes, rewrite_by_lua in the inner scope will override the one in the
outer scope completely :) So in your case, the following should work:

rewrite_by_lua ' ngx.exec("@proxyA"); ';
location @proxyA {
rewrite_by_lua return;
proxy_pass http://someIP;
more_set_headers 'Foo: bar';
}

Eventually we'll make rewrite_by_lua used in the server {} block run
in the "server rewrite" phase and it will not get inherited by
locations, just like the standard "rewrite" directive.

Regards,
-agentzh
Reply all
Reply to author
Forward
0 new messages