Skip to content

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

@poohlaha

Description

@poohlaha

电脑
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),找到相应地方:

  • get_headers 错误地方
image
  • get_methods 错误地方
image

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions