gpt4 book ai didi

node.js - Nginx gunzip POST 请求到后端

转载 作者:搜寻专家 更新时间:2023-10-31 23:28:04 25 4
gpt4 key购买 nike

我有大量的 HTTP POST 请求被发送到 nginx 服务器,然后负载平衡到一组反向代理的 node.js/express.js 后端服务器。为了节省一些网络消耗,使用 GZIP 和 header Content-Encoding: gzip 发送有效负载。

我正在努力实现这样的目标:

[Client] [Nginx Reverse Proxy] [BackEnd]
| [gziped payload] | [raw payload] |<br/>
|--------------------> | ----------------------------->|
| | |<br/>
| [Raw Response] | [Raw response] |
| <------------------ | <-----------------------------|
| | |

出于效率原因,我想在 Nginx 上运行 Gunzip,但我一直无法这样做。这是 http nginx.conf 文件:

http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;

server {
listen 80;
server_name localhost;
gunzip on;
location / {
proxy_pass http://localhost:8080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
}

这是一个请求示例:

echo "{"id": 0,"mypayload":"This is an example"}" | gzip -c - | curl -v -i -X POST -H 'Content-Encoding: gzip' --data-binary '@-' http://localhost/test

我希望 nginx 解压缩有效负载的内容并将原始内容传送到后端服务器,但内容仍以压缩形式传送。

我见过很多人在做相反的事情(Nginx gziping 响应,甚至对没有 Accept-Encoding: gzip header 的客户端的 gunzip 响应)却找不到它成功地将 payload gunziping 到后端。

有什么线索吗?

最佳答案

我知道这是一个较旧的问题,但我希望对发送压缩请求的 IOS 客户端执行相同的操作,在查看您的问题后,我发现了一个描述为 here 的 lua 解决方案。并想将其张贴在这里以供将来引用。

lua 模块看起来像:

-- Debian packages nginx-extras, lua-zlib required

ngx.ctx.max_chunk_size = tonumber(ngx.var.max_chunk_size)
ngx.ctx.max_body_size = tonumber(ngx.var.max_body_size)

function create_error_response (code, description)
local message = string.format('{"status":400,"statusReason":"Bad Request","code":%d,"exception":"","description":"%s","message":"HTTP 400 Bad Request"}', code, description)
ngx.status = ngx.HTTP_BAD_REQUEST
ngx.header.content_type = "application/json"
ngx.say(message)
ngx.exit(ngx.HTTP_OK)
end


function inflate_chunk (stream, chunk)
return stream(chunk)
end


function inflate_body (data)
local stream = require("zlib").inflate()
local buffer = ""
local chunk = ""

for index = 0, data:len(), ngx.ctx.max_chunk_size do
chunk = string.sub(data, index, index + ngx.ctx.max_chunk_size - 1)
local status, output, eof, bytes_in, bytes_out = pcall(stream, chunk)

if not status then
-- corrupted chunk
ngx.log(ngx.ERR, output)
create_error_response(4001, "Corrupted GZIP body")
end

if bytes_in == 0 and bytes_out == 0 then
-- body is not gzip compressed
create_error_response(4002, "Invalid GZIP body")
end

buffer = buffer .. output

if bytes_out > ngx.ctx.max_body_size then
-- uncompressed body too large
create_error_response(4003, "Uncompressed body too large")
end
end

return buffer
end


local content_encoding = ngx.req.get_headers()["Content-Encoding"]
if content_encoding == "gzip" then
ngx.req.read_body()
local data = ngx.req.get_body_data()

if data ~= '' then
local new_data = inflate_body(data)

ngx.req.clear_header("Content-Encoding")
ngx.req.clear_header("Content-Length")
ngx.req.set_body_data(new_data)
end
end

然后你可以像这样使用它:

location / {
proxy_pass http://127.0.0.1:8080;
proxy_pass_request_headers on;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;

client_max_body_size 512k; # Max request body size of 512 KB
client_body_buffer_size 512k;

set $max_chunk_size = 10240; # Chunks of 10 KB
set $max_body_size = 524288; # Max inflated body size of 512 KB

rewrite_by_lua_file inflate_body.lua;
}

关于node.js - Nginx gunzip POST 请求到后端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29766154/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com