通过nginx+lua 实现修改反向代理后的响应报文和header
注意使用了子请求ngx.location.capture(), 其中参数always_forward_body = true, 回自动转发父请求中的请求body, 默认为false, 只自动转发put和post的请求体
upstream myapp_backend {
server 10.xxx.xxx.xxx:xxxx;
}
log_format gitv1 '"$remote_addr" "$http_host" "[$time_local]" "$request" '
'"$status" "$body_bytes_sent" "$bytes_sent" "$gzip_ratio" "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" "$upstream_addr" "$upstream_response_time" "$request_time" "$request_body" "$resp_body"';
server {
listen 10082;
server_name resp.xxxx.xx;
access_log logs/cm_resp_access.log gitv1;
error_log logs/cm_resp_error.log;
lua_need_request_body on;
location /subreq/ {
internal;
proxy_pass http://myapp_backend/;
}
location /md5json {
set $resp_body "";
content_by_lua '
--ngx.req.read_body()
local subrequest_uri = "/subreq" .. ngx.var.request_uri
local resp = ngx.location.capture(subrequest_uri,{
method = ngx.HTTP_POST,
always_forward_body = true
})
ngx.var.resp_body = resp.body
ngx.print(resp.body)
';
header_filter_by_lua 'ngx.header["resp_header"] = ngx.md5(ngx.var.resp_body .. "xxx")';
}
}
版权声明:本文为zongyue_wang原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。