在插件开发时,发现kong自带插件不处理压缩内容。
需要自己定制开发。
一,使用zlib,新增处理脚本
myCompression.lua
local zlib = require "zlib"
-- see https://stackoverflow.com/questions/45216069/lua-how-to-gzip-a-string-gzip-not-zlib-in-memory
-- and https://github.com/brimworks/lua-zlib
local _M = {}
-- input: string
-- output: string compressed with gzip
function _M.compress(uncompressed)
local level = 9
local windowSize = 15+16
local stream = zlib.deflate(level, windowSize)
return stream(uncompressed, "finish")
end
-- input: string compressed with gzip
-- output: string
function _M.decompress(compressed)
local stream = zlib.inflate()
return stream(compressed)
end
return _M
二,使用该上述方法,对hearder进行判断即可
if ngx.header["content-encoding"] == "gzip" then
decompress(chunks)
then
dosomething...
//记得处理完成后再压缩回去
if ngx.header["content-encoding"] == "gzip" then
compress(chunks)
then
三,zlib环境安装
zlib使用,需要安装相应环境
在dockerfile中可以用以下代码
RUN /bin/sh -c /bin/sh -c set -ex \
&& sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories \
&& apk add --no-cache libuuid \
&& apk add --no-cache --virtual .build-deps gcc musl-dev zlib-dev \
&& luarocks install lua-zlib \
&& apk del .build-deps # buildkit
以上。