Nginx lua 接收 GET/POST 请求

教程 shanhuhai 8935℃ 0评论

有时候写一些简单的api 接口如果不想再起个php-fpm ,完全可以通过nginx加lua在nginx 直接实现。

当然前提是你必须给nginx安装 lua-nginx-module 模块。

下面是nginx 的配置及lua 代码:

lua_need_request_body on;
location = /api {
    default_type text/plain;
    content_by_lua_block {
        ngx.req.read_body()
        // 获取Post 数据
        local post = ngx.req.get_post_args()
        // 获取Querystring 数据
        local get = ngx.req.get_uri_args() 
        ngx.say(post.name)
        ngx.say(post.gender)
        ngx.say(get.type)
    }

}

content_by_lua_block 中放lua 代码

打开命令行模拟请求测试一下

curl  -XPOST 'http://test-lua.loc/mm?type=good' -d "name=shanhuhai" -d "gender=1"

转载请注明:大后端 » Nginx lua 接收 GET/POST 请求

付费咨询
喜欢 (4)or分享 (0)
发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址