我想捕获http响应正文,并使用resty.http将其发送到另一个端点。这是我的配置,我无法在content_by_lua_block内获取主体(它为null)。响应正文打印在access.log中,但在content_by_lua_block内部不存在。任何指针表示赞赏。我是openresty的新手,并赞赏链接。
--- nginx.conf --- start
worker_processes 1;
error_log /tmp/error.log debug;
events {
worker_connections 128;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
lua_package_path "/usr/local/Cellar/openresty/1.13.6.2/lualib/resty/?.lua;;";
lua_need_request_body on;
log_format bodylog '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" $request_time '
'<"$request_body" >"$response_body"';
server {
listen 8081 ;
server_name 127.0.0.1;
set $response_body "";
location /posts/1 {
mirror /mirror;
mirror_request_body on;
proxy_pass http://localhost:3000/posts/1;
access_log /tmp/access.log bodylog;
}
location = /mirror { // mirror traffic for requests
internal;
proxy_pass http://127.0.0.1:8080/analyzeservlet-1.0.0/analyzeservlet;
content_by_lua_block {
if ngx.var.resp_body ~= "" then
local http = require "resty.http"
local httpc = http.new()
local res, err = httpc:request_uri("<uri", {
method = "POST",
body = ngx.var.resp_body,
headers = {
["Content-Type"] = "application/x-www-form-urlencoded",
} })
end
}
}
}
client_body_buffer_size 16k;
client_max_body_size 16k;
body_filter_by_lua_block {
local response_body = string.sub(ngx.arg[1], 1, 1000)
ngx.ctx.buffered = (ngx.ctx.buffered or "") .. response_body
if ngx.arg[2] then
ngx.var.response_body = ngx.ctx.buffered
end
}
include servers/*;
}
答案 0 :(得分:0)
您可以使用ngx.req.get_body_data
读取身体数据。此处提供更多信息:https://github.com/openresty/lua-nginx-module/#ngxreqget_body_data