Hello!
On Sun, May 4, 2014 at 5:13 AM, Gordon Madarm wrote:
> I want to inspect all GET parameters and return a 503 response if any of
> them contain the string "test". I'm using the following code but still get
> 200 responses. What am I missing?
>
[...]
> for key, val in pairs(gargs) do
> if type(val) == "table" then
> ngx.say(key, ": ", table.concat(val, ", "))
> else
> ngx.say(key, ": ", val)
If you check out your nginx error log file, you should see the following error:
[error] 11522#0: *1 lua handler aborted: runtime error: attempt to
set status 503 via ngx.exit after sending out the response status 200
The reason for this is that you call ngx.say() before ngx.exit(503).
The ngx.say() call outputs response body data so it also triggers
sending out the default response header which is 200. After sending
out the 200 response header, you cannot revoke it back (due to the
streaming output nature of nginx) and update it to 503, unless you
have a time machine. See the official documentation for ngx.say() for
more details:
https://github.com/openresty/lua-nginx-module#ngxsay
To quote, "emits arguments concatenated to the HTTP client (as
response body). If response headers have not been sent, this function
will send headers out first and then output body data."
The suggestion is to use ngx.log() or print() for debugging purpose
here. Also, always check out your nginx error logs when something
unexpected happens.
Regards,
-agentzh