Node.js Nginx gunzip将请求发送到后端

Node.js Nginx gunzip将请求发送到后端,node.js,nginx,reverse-proxy,gunzip,Node.js,Nginx,Reverse Proxy,Gunzip,我有大量HTTP POST请求被发送到nginx服务器,然后将负载平衡到一组反向代理node.js/express.js后端服务器。为了节省一些网络消耗,正在使用GZIP和头内容编码发送有效负载:GZIP 我正在努力实现这样的目标: [客户端][Nginx反向代理][后端] |[gzip管道负载]|[raw payload]| |--------------------> | ----------------------------->| || | |[原始响应]|[原始响应]| |我知道这是一

我有大量HTTP POST请求被发送到nginx服务器,然后将负载平衡到一组反向代理node.js/express.js后端服务器。为了节省一些网络消耗,正在使用GZIP和头内容编码发送有效负载:GZIP

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

[客户端][Nginx反向代理][后端]
|[gzip管道负载]|[raw payload]|
|--------------------> | ----------------------------->| || |
|[原始响应]|[原始响应]|
|我知道这是一个较老的问题,但我希望对发送压缩请求的IOS客户端也这样做,在检查完您的问题后,我发现了一个描述的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;
    }

你找到这个问题的答案了吗?你最终经历了什么过程?@bibstha现在我正在后端进行gzip解码。如果我找不到使用Nginx的替代方案,我可能会在Nginx之后开发一个简单的反向代理,或者编写一个LUA模块来处理这些情况。这个模块使用哪个LUA和LUA zlib版本?谢谢!又一次没有解释的否决票;我讨厌这种没有帮助的行为。无论如何,我想一个问题是
ngx.req.get_headers()[“Content Encoding”]
应该使用
ngx.resp.
-- 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;
    }