After the first public release of ngx_lua, I’m proud to annouce the v0.1.1 version, which has introduced the access_by_lua and access_by_lua_file directives:
https://github.com/chaoslawful/lua-nginx-module/downloads
Now we can code up our own nginx access-phase handlers directly in pure Lua, with all the capabilities with rewrite_by_lua and content_by_lua, like firing up subrequests to other locations, reading/writing nginx variables, changing response headers, internal redirection, HTTP 301/302 redirection, throwing out 403/500/etc error pages, and etc etc etc.
Here’s a small example that emulates ngx_auth_request’s functionality in pure Lua:
- location /
- {
- access_by_lua ‘
- local res = ngx.location.capture("/auth")
- if res.status == ngx.HTTP_OK then
- return
- end
- if res.status == ngx.HTTP_FORBIDDEN then
- ngx.exit(res.status)
- end
- ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
- ‘;
- # proxy_pass/fastcgi_pass/postgres_pass/…
- }
which is approximately equivalent to
- location /
- {
- auth_request /auth;
- # proxy_pass/fastcgi_pass/postgres_pass/…
- }
except that "auth_request" runs at the beginning of the nginx access phase while "access_by_lua" runs at the end of the access phase.
ngx_lua is an nginx C module that embeds the Lua or LuaJIT interpreter into the nginx core and you can find the latest source code and the complete documentation here
https://github.com/chaoslawful/lua-nginx-module
Enjoy!
如非注明则为本站原创文章,欢迎转载。转载请注明转载自:moon's blog