lua possibilities/limitations

agentzh agentzh at gmail.com
Mon Feb 14 07:08:19 MSK 2011


On Sun, Feb 13, 2011 at 12:02 PM, Richard Kearsley
<Richard.Kearsley at m247.com> wrote:
> Hi Guys,
>
> I need a little help with the lua module. My ultimate aim is to import
> variables into nginx from outside source.
>

Your code below is totally wrong because you do not understand nginx
request phases well enough :)

1. Your content_by_lua always runs after any write-phase directrives
like "set" and "if" even if you put "set $limit_rate $myvar;" after it
in the config file.

2. Your content_by_lua for the /main location does not generate any
response bodies (using ngx.print or ngx.say in Lua). So you see the
empty response body on the client side.

BTW, your "add_header" directive runs in the output filter phase, so
your X-Test header gets the value set by your Lua code, which is
expected. But your X-Limit gets the old value because "set $limit_rate
$myvar" runs *before* $myvar gets the new value in Lua and $limit_rate
is still the old value. (For why "set" runs after your content_by_lua
code, see above).

FWIW, rewrite_by_lua and access_by_lua also run *after* those "set",
"if", "rewrite" directives provided by ngx_rewrite.

>
> Am I going about this in the right way? As I say, my ultimate aim is to set
> a whole bunch of nginx vars from outside...
>

For this purpose, rewrite_by_lua or access_by_lua will almost be
sufficient. But if you want to calculate variable values using
response headers (from ngx_proxy or ngx_fastcgi), then you need to
issue subrequests in content_by_lua before ngx_lua gets the
"output_header_filter_by_lua" directive implemented :)

Cheers,
-agentzh



More information about the nginx mailing list