Welcome! Log In Create A New Profile

Advanced

How to set response from rewrite_by_lua_file directive

Posted by tripledot 
How to set response from rewrite_by_lua_file directive
November 25, 2014 01:14PM
I have a rewrite_by_lua_file directive where I lookup a key supplied as a header in the original request, in a shared lookup table. If found, I pass as a header the value mapped to the original key. If not, I return a specific status code.

So in my nginx.conf file I have something like this:

location /start
{
rewrite_by_lua_file key_lookup.lua;
proxy_pass http://127.0.0.1:8080/start;
}


And my key_lookup.lua is attached.

So I am trying to use ngx.say to set the response body to a specific JSON content when I return 401 Unauthorized, with no luck (the status code returned is what I expect, but the response body is empty). I have also tried using ngx.print.

Is there another way to achieve this?
Attachments:
open | download - key_lookup.lua (586 bytes)
Re: How to set response from rewrite_by_lua_file directive
November 25, 2014 03:03PM
Downloading seems to take forever. The contents of my key_lookup.lua is below:

local cjson = require("cjson")

local headers = ngx.req.get_headers()
local key = headers["X-KEY-HEADER"]

if key then
local value = ngx.shared.lookuptable[key]
if value then
ngx.req.set_header("X-VALUE-HEADER", tostring(value))
else
ngx.log(ngx.ERR, "Unable to find value for key " .. tostring(key))
ngx.status = ngx.HTTP_UNAUTHORIZED
ngx.say(cjson.encode({error = {code = "401 Unauthorized", cause = "10000"}}))
ngx.exit(ngx.HTTP_UNAUTHORIZED)
end
else
ngx.log(ngx.ERR, "X-KEY-HEADER not specified")
ngx.exit(ngx.HTTP_UNAUTHORIZED)
end
Re: How to set response from rewrite_by_lua_file directive
November 25, 2014 04:34PM
Better place to ask Lua stuff:
https://groups.google.com/forum/#!forum/openresty-en

---
nginx for Windows http://nginx-win.ecsds.eu/
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 154
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