Decompressing request using GZIP with Nginx

If you're using a Lua module with Nginx, then decompressing GZIPped data requires some extra handling.

If you’re getting compressed data in the request, and need to pass on the decompressed data, you can use the following code snippet to decompress the request in flight.

-- Handle request
local zlib = require 'zlib'

myStr = nil
if ngx.req.get_headers()['Content-Encoding'] == 'gzip' then

    ngx.req.read_body()
    local myStr = zlib.inflate()(ngx.var.request_body, 'finish')

    ngx.req.clear_header('Content-Encoding')
    ngx.req.clear_header('Content-Length')
    ngx.req.set_body_data(myStr)
end

To use the above, you will need the lua-zlib library which you can install using luarocks install lua-zlib.

← previous post
Compressing output using GZIP with PHP 5.3

next post →
Logging AWS spot instance termination

22TechnologyView source