# Ngx\_lua v0.1.1: access\_by\_lua and access\_by\_lua\_file landed

**URL:** <https://www.ruby-forum.com/t/ngx-lua-v0-1-1-access-by-lua-and-access-by-lua-file-landed/201442>\
**Category:** NGINX\
**Created:** [January 7, 2011, 12:45pm UTC](https://www.ruby-forum.com/t/ngx-lua-v0-1-1-access-by-lua-and-access-by-lua-file-landed/201442 "2011-01-07T12:45:22Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [January 7, 2011, 12:45pm UTC](https://www.ruby-forum.com/t/ngx-lua-v0-1-1-access-by-lua-and-access-by-lua-file-landed/201442/1 "2011-01-07T12:45:22Z")

</div>

Hi, folks!

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/tarball/v0.1.1

```

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!  
-agentzh
