Hello!
On Mon, May 12, 2014 at 6:07 PM, ximazi wrote:
> i saw your solution of catching all request by lua. i add access_by_lua_file both in http and location, but only the location one is functional. Is it possible to make both available, thanks
>
Well, access_by_lua used in inner scopes *override* access_by_lua
configurations in outer scopes. This is by design. For your use case,
you'd better combine Lua logic on the Lua land (by means of pure Lua
module files, for example). Below is an example demonstrates the idea:
-- file global_access.lua
local _M = {}
function _M.check()
-- global access checks go here...
if not some_condition then
return ngx.exit(403) -- reject the current request, for example
end
end
return _M
The global_access.lua module file holds your global access rules. And
then for a typical location in your nginx.conf:
-- file location_A_access.lua
local global_access = require "global_access"
local global_check = global_access.check
local _M = {}
function _M.check()
global_check()
-- my checks specific to location A go here...
end
return _M
And finally in your location A, you can have
location A {
access_by_lua 'require("location_A_access").check()';
}
And in an outer scope, you can have
access_by_lua 'require("global_access").check()';
You may also need to configure the lua_package_path directive to add
the location of your global_access.lua and location_A_access.lua to
Lua's search paths (for require()).
Combining Lua logic on the Lua land is way more efficient than
combining multiple access_by_lua directives on the nginx.conf level.
BTW, please use the openresty-en mailing list for future questions
like this. I'm cc'ing the list for future references.
Regards,
-agentzh