lua-nginx-module icon indicating copy to clipboard operation
lua-nginx-module copied to clipboard

在 nginx C module 中,如何判断是否在 openresty 环境下并且向 lua 注册 C 函数

Open zhwaaaaaa opened this issue 3 months ago • 2 comments

请问在 nginx module 中,如何判断 是在 openresty 环境下,并且向 lua 注册 C 函数。 ngx_lua_foo_module:

#include <ngx_config.h>
#include <ngx_core.h>

// ... some C code running in pure nginx


// 如何判断??
#ifdef OPENRESTY

#include <lauxlib.h>
#include "ngx_http_lua_api.h"

static int
ngx_http_lua_foo_module(lua_State * L)
{
    lua_createtable(L, 0, 6);

    lua_pushcfunction(L, ngx_http_lua_foo);
    lua_setfield(L, -2, "foo");

    return 1;
}

#endif

在 编译 CONFIG 文件中 如何判断是在 openresty 环境下“ nginx compile config:

ngx_module_type=EVENT
ngx_module_name=ngx_foo_module
ngx_module_incs=
ngx_module_deps=
ngx_module_srcs=$ngx_addon_dir/ngx_foo_module.c
ngx_module_libs=
ngx_module_order=
ngx_module_link=ADDON

. auto/module

if [ "$OPENRESTY" = ”yes“ ];then # 如何判断 ??
  ngx_module_type=EVENT
  ngx_module_name=ngx_foo_lua_module
  ngx_module_incs=
  ngx_module_deps=
  ngx_module_srcs=$ngx_addon_dir/ngx_foo_lua_module.c
  ngx_module_libs=
  ngx_module_order=
  ngx_module_link=ADDON
fi

zhwaaaaaa avatar Oct 01 '25 05:10 zhwaaaaaa

Please refer to https://github.com/openresty/lua-nginx-module/blob/971cc14eded9067fcbf2e02071c432d84ace7445/config#L176-L191

leslie-tsang avatar Nov 15 '25 02:11 leslie-tsang

if the ffi way meets your requirement,it is best to use it

zhuizhuhaomeng avatar Nov 16 '25 08:11 zhuizhuhaomeng