Nginx内部调用另一个请求并返回该请求的数据

我正在使用 Nginx 和 Lua 解析我的 REST API ( https://openresty-reference.readthedocs.io/en/latest/Lua_Nginx_API/) 。我正在尝试在我的 REST 调用中内部调用另一个 API,这是一个不同的服务器,并返回该响应。但它始终得到空响应。以下是我的配置,我正在请求 /student,任何帮助都将不胜感激

location /student {
    content_by_lua '
    local str = "test"
    local res = ngx.location.capture("/detail",{method = ngx.HTTP_POST, body = str})
    ngx.say(str)
    ';
}
location /detail {
    set $target '104.28.17.1/post';
    set $pass 'Basic Y2l28=';
    lua_need_request_body on;
    content_by_lua '
    proxy_set_header          Host            $host;
    proxy_set_header          X-Real-IP       $remote_addr;
    proxy_set_header          X-Forwarded-For $proxy_add_x_forwarded_for;
    #access_by_lua '
    proxy_set_header Authorization $pass;
    proxy_set_header Content-Type 'application/x-www-form-urlencoded';
    proxy_set_header Accept '*/*';
    proxy_set_header Connection 'keep-alive';
    proxy_pass http://$target;
}
点赞
用户9090571
用户9090571

尝试将你的位置/student更改为

location /student {
    content_by_lua '
    local str = "test"
    local res = ngx.location.capture("/detail",{method = ngx.HTTP_POST, body = str})
    ngx.status = 200
    ngx.say(str)
    ngx.exit(ngx.HTTP_OK)
    ';
}
2020-06-20 19:01:04