M
M
Matvey Safronov2016-09-27 15:13:10
Nginx
Matvey Safronov, 2016-09-27 15:13:10

How to make nginx run lua scripts via luaJIT?

I want to put the jwt validation logic on the shoulders of nginx:
https://github.com/auth0/nginx-jwt
The server has the following configuration:

server {

        listen 80;
        server_name test;

        location / {
                access_by_lua '
                local jwt = require("nginx-jwt")
                jwt.auth()
                ';
                proxy_set_header X-Forwarded-For $remote_addr;
                proxy_set_header Host $http_host;
                proxy_pass http://127.0.0.1:1338;
        }

}

I specified the location of lua scripts in nginx.conf:
lua_package_path "/opt/test/?.lua;;";
Reload nginx, access via http:
$ curl test
2016/09/27 15:01:57 [error] 25541#0: *399 lua entry thread aborted: runtime error: /opt/test/resty/evp.lua:4: module 'ffi' not found:

I haven't coded in Lua. It turned out that there are two Lua runtimes: lua proper and luajit. It turned out that the ffi module is some kind of thing with the ability to execute C code, and is executed only through luajit. In my case, scripts are executed using lua, and it ends with such an error (checked in REPL).
The question is how to make nginx run these scripts with luaJIT ? Already got it all out. Delivered OpenResty distribution kit, collected, installed - there is no effect. What to do?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
Boris Nagaev, 2016-10-07
@impeee

You need to install LuaJIT and rebuild nginx with LuaJIT instead of Lua.
In Debian, the following patch was applied for this ( bug ):

diff -ruN nginx-1.6.0.orig/debian/modules/nginx-lua/config nginx-1.6.0/debian/modules/nginx-lua/config
--- nginx-1.6.0.orig/debian/modules/nginx-lua/config	2014-04-24 13:23:46.000000000 -0300
+++ nginx-1.6.0/debian/modules/nginx-lua/config	2014-07-24 03:48:10.816586362 -0300
@@ -131,9 +131,9 @@
             ngx_feature="Lua library in /usr/"
             ngx_feature_path="/usr/include/lua5.1"
             if [ $NGX_RPATH = YES ]; then
-                ngx_feature_libs="-R/usr/lib -L/usr/lib -lm -llua5.1"
+                ngx_feature_libs="-R/usr/lib -L/usr/lib -lm -lluajit-5.1"
             else
-                ngx_feature_libs="-L/usr/lib -lm -llua5.1"
+                ngx_feature_libs="-L/usr/lib -lm -lluajit-5.1"
             fi
             . auto/feature
         fi

```

A
anikavoi, 2020-03-21
@anikavoi

You don't need to rebuild anything. Everything has already been collected for us.
https://openresty.org/en/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question