openresty - OpenResty:响应
问题描述
我想捕获 http 响应正文并使用 resty.http 将其发送到另一个端点。这是我的配置,我无法在 content_by_lua_block 中获取正文(它为空)。响应正文打印在 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/*;
}
解决方案
您可以使用 读取正文数据ngx.req.get_body_data
。此处提供更多信息:https ://github.com/openresty/lua-nginx-module/#ngxreqget_body_data
推荐阅读
- c++ - 为什么我在函数中创建的对象不会被其他函数修改?(C++)
- python - 检索解包需要8字节的缓冲区错误套接字python
- github - 有什么方法可以使用 Google Colab 在 GitHub 上发送 Pull Requests 和/或 Merge PRs?
- android - Flutter - 错误:此库已禁用空安全功能
- php - laravel 8 中的单元测试控制器
- rendering - 在虚幻引擎 4 中使用自定义组件渲染 obj 网格会呈现视觉伪影
- c# - 用户控件不更新文本框 mvvm WPF C#
- javascript - 预加载器屏幕淡出后播放 embed youtube
- python-3.x - Tensorflow 2.2.0-rc4 AttributeError:模块'tensorflow.compat.v1'在Spyder中没有属性'contrib'?
- c# - 如何增加分数并在控制台中打印?