openresty / lua-nginx-module

Embed the Power of Lua into NGINX HTTP servers
https://openresty.org/
11.34k stars 2.04k forks source link

ngx.req诸多方法不能使用, 比如 get_headers, get_method等 #2188

Open poohlaha opened 1 year ago

poohlaha commented 1 year ago

电脑

image

安装版本:

nginx version: nginx/1.23.4
lua version: Lua 5.4.5  Copyright (C) 1994-2023 Lua.org, PUC-Rio
luajit version: LuaJIT 2.1.0-beta3 -- Copyright (C) 2005-2022 Mike Pall. https://luajit.org/(使用的是openresty中的版本)
openssl verions: 1.1.1t
pcre version: 1.x

nginx需要的模块(从openresty的 github 中下载, 存放于 /Users/smile/tools/nginx-modules目录下):

lua-nginx-module(master分支)
ngx_devel_kit(master分支)
lua-resty-core(master分支, 需要 `make` 和 `make install` 到`/Users/smile/tools/nginx-1.23.4/restry`目录)
lua-resty-lrucache(master分支, 需要 `make` 和  `make install` 到`/Users/smile/tools/nginx-1.23.4/restry`目录)

ps: 本人使用nginx源码编译的方式集成 lua, 而非 openresty或brew的方式

安装完lualuajitopensslpcre 后编译nginx(nginx下载目录为: /Users/smile/tools/nginx-1.23.4):

./configure --prefix=/usr/local/nginx \
--with-http_ssl_module \
--with-http_stub_status_module \
--add-module=/Users/smile/tools/nginx-modules/lua-nginx-module-master \
--add-module=/Users/smile/tools/nginx-modules/ngx_devel_kit-master
make
make install

安装成功后配置 nginx.conf

http {
   ...

  # 添加 lua_package_path
 lua_package_path '/Users/smile/tools/nginx-1.23.4/restry/lib/lua/?.lua;;';
 lua_package_cpath '/Users/smile/tools/nginx-1.23.4/restry/lib/lua/?.so;;';

...

server {
    location /testLua {
        default_type 'text/plain';
        # content_by_lua 'ngx.say("hello, lua")';
        content_by_lua_block {
            ngx.say('ngx.req.raw_header: ', ngx.req.raw_header(), '<br/>'). -- 这个可以运行, 输出请求头
            ngx.say("ngx.req.http_version: ", ngx.req.http_version(), "<br/>") -- 这个可以运行(结果: ngx.req.http_version: 1.1)
            ngx.say("ngx.req.get_headers: ", ngx.req.get_headers(), "<br/>") -- 此处开始报错
            ngx.say("ngx.req.get_method: ", ngx.req.get_method(), "<br/>")  -- 此处开始报错
            ngx.say("hello, lua")
        }
    }

 ...
}

...

}

其中: get_headersget_method两个方法运行报错, 错误如下:

stack traceback:
coroutine 0:
    [C]: in function 'error'
    ...tools/nginx-1.23.4/restry/lib/lua/resty/core/request.lua:131: in function 'get_headers'
    content_by_lua(/Users/smile/tools/nginx-1.23.4/lua.conf:18):2: in function <content_by_lua(/Users/smile/tools/nginx-1.23.4/lua.conf:18):1>, client: 127.0.0.1, server: localhost, request: "GET /testLua HTTP/1.1", host: "localhost:9999"
2023/05/11 10:53:17 [error] 3358#0: *507 lua entry thread aborted: runtime error: ...tools/nginx-1.23.4/restry/lib/lua/resty/core/request.lua:292: no request found
stack traceback:
coroutine 0:
    [C]: in function 'error'
    ...tools/nginx-1.23.4/restry/lib/lua/resty/core/request.lua:292: in function 'get_method'
    content_by_lua(/Users/smile/tools/nginx-1.23.4/lua.conf:18):2: in function <content_by_lua(/Users/smile/tools/nginx-1.23.4/lua.conf:18):1>, client: 127.0.0.1, server: localhost, request: "GET /testLua HTTP/1.1", host: "localhost:9999"

翻看源码(/Users/smile/tools/nginx-1.23.4/restry/lib/lua/resty/core/request.lua),找到相应地方:

请帮忙看一下为什么ngx.req中好多方法不能使用,是不是我的安装有问题还是什么, 对了不要推荐我安装openresty,因为我一开始用的openresty, 结果这文档看的我脑袋疼,半天也没安装成功!谢谢~~

zhuizhuhaomeng commented 1 year ago

please download openresty-1.21.4.2rc1 directly.

Kauren J @.***> 于2023年5月11日周四 11:24写道:

电脑 [image: image] https://user-images.githubusercontent.com/10954901/237565022-5d32d5b6-7791-46ba-81e3-8b10a7e74121.png

安装版本:

nginx version: nginx/1.23.4 lua version: Lua 5.4.5 Copyright (C) 1994-2023 Lua.org, PUC-Rio luajit version: LuaJIT 2.1.0-beta3 -- Copyright (C) 2005-2022 Mike Pall. https://luajit.org/(使用的是openresty中的版本) openssl verions: 1.1.1t pcre version: 1.x

nginx需要的模块(从openresty的 github 中下载):

lua-nginx-module(master分支) ngx_devel_kit(master分支) lua-resty-core(master分支, 需要 makemake install/Users/smile/tools/nginx-1.23.4/restry目录) lua-resty-lrucache(master分支, 需要 makemake install/Users/smile/tools/nginx-1.23.4/restry目录)

ps: 本人使用nginx源码编译的方式集成 lua, 而非 openresty或brew的方式

安装完lua、luajit、openssl、pcre 后编译nginx(nginx下载目录为: /Users/smile/tools/nginx-1.23.4):

./configure --prefix=/usr/local/nginx \ --with-http_ssl_module \ --with-http_stub_status_module \ --add-module=/Users/smile/tools/nginx-modules/lua-nginx-module-master \ --add-module=/Users/smile/tools/nginx-modules/ngx_devel_kit-master

make make install

安装成功后配置 nginx.conf

http { ...

添加

lua_package_path '/Users/smile/tools/nginx-1.23.4/restry/lib/lua/?.lua;;'; lua_package_cpath '/Users/smile/tools/nginx-1.23.4/restry/lib/lua/?.so;;';

...

server { location /testLua { default_type 'text/plain';

content_by_lua 'ngx.say("hello, lua")';

    content_by_lua_block {
        ngx.say('ngx.req.raw_header: ', ngx.req.raw_header(), '<br/>'). -- 这个可以运行
        ngx.say("ngx.var.request_method: ", ngx.var.request_method(), "<br/>") -- 这个可以运行
        ngx.say("ngx.req.get_headers: ", ngx.req.get_headers(), "<br/>") -- 此处开始报错
        ngx.say("ngx.req.get_method: ", ngx.req.get_method(), "<br/>")  -- 此处开始报错
        ngx.say("hello, lua")
    }
}

... }

...

}

其中: get_headers 和 get_method两个方法运行报错, 错误如下:

stack traceback: coroutine 0: [C]: in function 'error' ...tools/nginx-1.23.4/restry/lib/lua/resty/core/request.lua:131: in function 'get_headers' content_by_lua(/Users/smile/tools/nginx-1.23.4/lua.conf:18):2: in function <content_by_lua(/Users/smile/tools/nginx-1.23.4/lua.conf:18):1>, client: 127.0.0.1, server: localhost, request: "GET /testLua HTTP/1.1", host: "localhost:9999" 2023/05/11 10:53:17 [error] 3358#0: *507 lua entry thread aborted: runtime error: ...tools/nginx-1.23.4/restry/lib/lua/resty/core/request.lua:292: no request found

stack traceback: coroutine 0: [C]: in function 'error' ...tools/nginx-1.23.4/restry/lib/lua/resty/core/request.lua:292: in function 'get_method' content_by_lua(/Users/smile/tools/nginx-1.23.4/lua.conf:18):2: in function <content_by_lua(/Users/smile/tools/nginx-1.23.4/lua.conf:18):1>, client: 127.0.0.1, server: localhost, request: "GET /testLua HTTP/1.1", host: "localhost:9999"

翻看源码(/Users/smile/tools/nginx-1.23.4/restry/lib/lua/resty/core/request.lua),找到相应地方:

  • get_headers 错误地方

[image: image] https://user-images.githubusercontent.com/10954901/237564606-b89c68ff-ed8b-44ab-920e-ea33fa142485.png

  • get_methods 错误地方

[image: image] https://user-images.githubusercontent.com/10954901/237564778-e5d7ae13-6ca6-419f-8daa-489b211d2d01.png

请帮忙看一下为什么ngx.req中好多方法不能使用,是不是我的安装有问题还是什么, 对了不要推荐我安装openresty,因为我一开始用的 openresty, 结果这文档看的我脑袋疼,半天也没安装成功!谢谢~~

— Reply to this email directly, view it on GitHub https://github.com/openresty/lua-nginx-module/issues/2188, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA6RHPX4TGQ3VMTOXT3JVS3XFRLVHANCNFSM6AAAAAAX5QIE3Y . You are receiving this because you are subscribed to this thread.Message ID: @.***>

poohlaha commented 1 year ago

I don't want to use openresty, I want to install resty through the nginx plugin

oowl commented 1 year ago

Openresty has many Nginx patches to do some features https://github.com/openresty/openresty/tree/master/patches, it's not a good idea to use lua-nginx-module with nginx to replace openresty. @poohlaha

yangguanghong commented 1 year ago

please download openresty-1.21.4.2rc1 directly. Kauren J @.> 于2023年5月11日周四 11:24写道: 电脑 [image: image] https://user-images.githubusercontent.com/10954901/237565022-5d32d5b6-7791-46ba-81e3-8b10a7e74121.png 安装版本: nginx version: nginx/1.23.4 lua version: Lua 5.4.5 Copyright (C) 1994-2023 Lua.org, PUC-Rio luajit version: LuaJIT 2.1.0-beta3 -- Copyright (C) 2005-2022 Mike Pall. https://luajit.org/(使用的是openresty中的版本) openssl verions: 1.1.1t pcre version: 1.x nginx需要的模块(从openresty的 github 中下载): lua-nginx-module(master分支) ngx_devel_kit(master分支) lua-resty-core(master分支, 需要 makemake install/Users/smile/tools/nginx-1.23.4/restry目录) lua-resty-lrucache(master分支, 需要 makemake install/Users/smile/tools/nginx-1.23.4/restry目录) ps: 本人使用nginx源码编译的方式集成 lua, 而非 openresty或brew的方式。 安装完lua、luajit、openssl、pcre 后编译nginx(nginx下载目录为: /Users/smile/tools/nginx-1.23.4): ./configure --prefix=/usr/local/nginx \ --with-http_ssl_module \ --with-http_stub_status_module \ --add-module=/Users/smile/tools/nginx-modules/lua-nginx-module-master \ --add-module=/Users/smile/tools/nginx-modules/ngx_devel_kit-master make make install 安装成功后配置 nginx.conf http { ... # 添加 lua_package_path '/Users/smile/tools/nginx-1.23.4/restry/lib/lua/?.lua;;'; lua_package_cpath '/Users/smile/tools/nginx-1.23.4/restry/lib/lua/?.so;;'; ... server { location /testLua { default_type 'text/plain'; # content_by_lua 'ngx.say("hello, lua")'; content_by_lua_block { ngx.say('ngx.req.raw_header: ', ngx.req.raw_header(), '
'). -- 这个可以运行 ngx.say("ngx.var.request_method: ", ngx.var.request_method(), "
") -- 这个可以运行 ngx.say("ngx.req.get_headers: ", ngx.req.get_headers(), "
") -- 此处开始报错 ngx.say("ngx.req.get_method: ", ngx.req.get_method(), "
") -- 此处开始报错 ngx.say("hello, lua") } } ... } ... } 其中: get_headers 和 get_method两个方法运行报错, 错误如下: stack traceback: coroutine 0: [C]: in function 'error' ...tools/nginx-1.23.4/restry/lib/lua/resty/core/request.lua:131: in function 'get_headers' content_by_lua(/Users/smile/tools/nginx-1.23.4/lua.conf:18):2: in function <content_by_lua(/Users/smile/tools/nginx-1.23.4/lua.conf:18):1>, client: 127.0.0.1, server: localhost, request: "GET /testLua HTTP/1.1", host: "localhost:9999" 2023/05/11 10:53:17 [error] 3358#0:
507 lua entry thread aborted: runtime error: ...tools/nginx-1.23.4/restry/lib/lua/resty/core/request.lua:292: no request found stack traceback: coroutine 0: [C]: in function 'error' ...tools/nginx-1.23.4/restry/lib/lua/resty/core/request.lua:292: in function 'get_method' content_by_lua(/Users/smile/tools/nginx-1.23.4/lua.conf:18):2: in function <content_by_lua(/Users/smile/tools/nginx-1.23.4/lua.conf:18):1>, client: 127.0.0.1, server: localhost, request: "GET /testLua HTTP/1.1", host: "localhost:9999" 翻看源码(/Users/smile/tools/nginx-1.23.4/restry/lib/lua/resty/core/request.lua),找到相应地方: - get_headers 错误地方 [image: image] https://user-images.githubusercontent.com/10954901/237564606-b89c68ff-ed8b-44ab-920e-ea33fa142485.png - get_methods 错误地方 [image: image] https://user-images.githubusercontent.com/10954901/237564778-e5d7ae13-6ca6-419f-8daa-489b211d2d01.png 请帮忙看一下为什么ngx.req中好多方法不能使用,是不是我的安装有问题还是什么, 对了不要推荐我安装openresty,因为我一开始用的 openresty, 结果这文档看的我脑袋疼,半天也没安装成功!谢谢~~ — Reply to this email directly, view it on GitHub <#2188>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA6RHPX4TGQ3VMTOXT3JVS3XFRLVHANCNFSM6AAAAAAX5QIE3Y . You are receiving this because you are subscribed to this thread.Message ID: **@
.***>

nginx-1.21.4 use LuaJIT-2.1-20230410、ngx_lua-0.10.25、lua-resty-lrucache-0.13、lua-resty-core-0.1.27 in openresty-1.21.4.2.tar.gz to complile and install, Launch prompt nginx: [alert] detected a LuaJIT version which is not OpenResty's; many optimizations will be disabled and performance will be compromised (see https://github.com/openresty/luajit2 for OpenResty's LuaJIT or, even better, consider using the OpenResty releases from https://openresty.org/en/download.html)

2023/08/23 13:18:55 [error] 26973#26973: *138 lua entry thread aborted: runtime error: /usr/local/LuaLIB23/lib/lua/resty/core/request.lua:131: no request found stack traceback: coroutine 0: [C]: in function 'error' /usr/local/LuaLIB23/lib/lua/resty/core/request.lua:131: in function 'get_headers' /usr/local/nginx/conf/waf/waf.lua:1: in function </usr/local/ nginx/conf/waf/waf.lua:1>, client: 120.245.61.4, server: www.xxx.com, request: "GET /doc/index/327239452843021338?dataType=basic&type=5&typeCode=318 HTTP/1.1", host: "www.xxx.com", referrer: "https://www.xxx.com/doc/detail?orgName=%E6%9D%AD%E5%B7%9E%E5%90%8C%E6%A2%A6%E6%8A%95%E8%B5%84%E7%AE%A1%E7%90%86%E5%90%88%E4%BC%99%E4%BC%81%E4%B8%9A%28%E6%9C%89%E9%99%90%E5%90%88%E4%BC%99%29"