openresty / openresty/lua-nginx-module

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

Open
#2,188 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C
Stars
11.8k
Forks
2.1k
Avg merge
6h 1m
Merged PRs (30d)
6

Description

电脑
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, 结果这文档看的我脑袋疼,半天也没安装成功!谢谢~~

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reported content_by_lua_block in nginx.conf and the corresponding get_headers and get_method paths in resty/lib/lua/resty/core/request.lua. Reproduce the request with the listed nginx, LuaJIT, lua-nginx-module, and lua-resty-core versions, then verify whether the build and request context match the module requirements. Done means identifying the installation or usage mismatch, or confirming a reproducible module defect.

Written by the indexing model from the issue text.

Assessment

Tech stack
lua, nginx
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.