Welcome! Log In Create A New Profile

Advanced

Use nginx lua extension with custom lua in C-modules

Posted by cryptooman 
Use nginx lua extension with custom lua in C-modules
March 04, 2016 01:41PM
Hi,

How to use custom Lua modules written in C in Nginx?

1) It is clear how to extend Lua with C: http://www.troubleshooters.com/codecorn/lua/lua_lua_calls_c.htm
Tried test samples -> all work good

2) It is clear how to use Lua modules with nginx written in Lua: https://github.com/openresty/lua-nginx-module#statically-linking-pure-lua-modules
Tried test samples -> all work good

But modules written in C and compiled as ".o" or ".so" object are not working.

1) I configure nginx with --with-ld-opt="-Wl,-rpath,/path/to/luajit-or-lua/lib,/<path>/mylua.o"
Content of "mylua.c"
--------------------------------------------------
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"

typedef int (*lua_CFunction) (lua_State *L);

lua_State* L;

static int test(lua_State *L)
{
int x = 777;
lua_pushnumber(L, x);
return 1;
}

int luaopen_mylua(lua_State *L)
{
lua_register(L, "test", test);
return 0;
}
--------------------------------------------------

2) This module works if use it in lua, like:
lua> require("mylua");
print(test()); -- Prints "777"

3) Nginx is compiling correctly

4) When I use in nginx config:
content_by_lua_file '/opt/nginx/test.lua';

Content of /opt/nginx/test.lua
local foo = require("mylua");
ngx.say(foo.test());

I'm receiving the error:
[error] 9375#0: *1 lua entry thread aborted: runtime error: /opt/nginx/test.lua:1: module 'mylua' not found:
no field package.preload['mylua']

The names of modules and files are 100% correct. Checked 10x times.
I suppose that lua C-module must be compiled with "luajit".

Thanks in advance
Re: Use nginx lua extension with custom lua in C-modules
March 04, 2016 02:28PM
Try here 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: 144
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