NGINX配置gzip请求自动解压

为了实现NGINX自动解压gzip请求,需要编译安装Nginx,使其支持lua模块。下面记录一下过程。

安装编译环境

yum groupinstall "Development Tools"

编译安装Nginx

1、下载Nginx源码。并解压
2、下载编译安装PCRE、zlib、LuaJit

$ wget https://ftp.pcre.org/pub/pcre/pcre-8.44.tar.gz
$ tar -zxf pcre-8.44.tar.gz
$ cd pcre-8.44
$ ./configure
$ make
$ sudo make install

$ wget http://zlib.net/zlib-1.2.11.tar.gz
$ tar -zxf zlib-1.2.11.tar.gz
$ cd zlib-1.2.11
$ ./configure
$ make
$ sudo make install

$ wget https://github.com/openresty/luajit2/archive/v2.1-20201229.tar.gz
$ tar -xvzf v2.1-20201229.tar.gz
$ cd luajit2-2.1-20201229/
$ make install

3、下载解压ngx_devel_kit、lua-nginx-module

这里是重点啊!!!很多坑都是这个家伙带来的
一定要用v0.10.9rc7版本,我试了其他几个版本,比如说v0.10.8、v0.10.15等,都是不行的,都会遇到下面说的那些坑,只有v0.10.9rc7不会。

$ wget https://github.com/vision5/ngx_devel_kit/archive/v0.3.1.tar.gz
$ tar -xzvf v0.3.1.tar.gz
$ rm v0.3.1.tar.gz

$ wget https://github.com/openresty/lua-nginx-module/archive/v0.10.9rc7.tar.gz
$ tar -xvzf v0.10.9rc7.tar.gz
$ rm v0.10.9rc7.tar.gz

4、编译安装Nginx

$ cd nginx-1.16.1
$ LUAJIT_LIB=/usr/local/lib LUAJIT_INC=/usr/local/include/luajit-2.1 \
./configure \
--user=nobody                         \
--group=nobody                        \
--with-http_gzip_static_module        \
--with-http_stub_status_module        \
--with-zlib=/tmp/nginx/zlib-1.2.11    \
--with-pcre                           \
--with-file-aio                       \
--with-http_realip_module             \
--without-http_scgi_module            \
--without-http_uwsgi_module           \
--without-http_fastcgi_module ${NGINX_DEBUG:+--debug} \
--with-cc-opt=-O2 --with-ld-opt='-Wl,-rpath,/usr/local/lib' \
--add-module=/tmp/nginx/ngx_devel_kit-0.3.1 \
--add-module=/tmp/nginx/lua-nginx-module-0.10.9rc7
$ make
$ sudo make install

5、下载编译安装lua-zlib

$ wget https://github.com/brimworks/lua-zlib/archive/v1.2.tar.gz
$ tar -xvzf v1.2.tar.gz
$ cd lua-zlib-1.2/
$ cmake -DLUA_INCLUDE_DIR=/usr/local/include/luajit-2.1 -DLUA_LIBRARIES=/usr/local/lib -DUSE_LUAJIT=ON -DUSE_LUA=OFF
$ make
$ cp zlib.so /usr/local/lib/lua/5.1/

配置Nginx

在location里面添加如下内容:

location / {
    proxy_pass http://127.0.0.1:8081;
    rewrite_by_lua_file /opt/linux/inflate_body.lua;
}

其中,inflate_body.lua是一个lua脚本,可以自动解压Content-Encoding: gzip的请求体。脚本内容如下:

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

ngx.ctx.max_chunk_size = tonumber(ngx.var.max_chunk_size) or 262144  -- 256KB
ngx.ctx.max_body_size = tonumber(ngx.var.max_body_size) or 16777216  -- 16MB

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, size)
        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.var.max_chunk_size)

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

启动Nginx

/usr/local/nginx/sbin/nginx

重新加载Nginx配置文件

/usr/local/nginx/sbin/nginx -s reload

python后端测试脚本

# coding:utf-8

import socket

from multiprocessing import Process

def handle_client(client_socket):
    """
    处理客户端请求
    """
    request_data = client_socket.recv(1024)
    print("request data:", request_data.decode())
    # 构造响应数据
    response_start_line = "HTTP/1.1 200 OK\r\n"
    response_headers = "Server: My server\r\n"
    response_body = "<h1>Python HTTP Test</h1>"
    response = response_start_line + response_headers + "\r\n" + response_body

    # 向客户端返回响应数据
    client_socket.send(bytes(response, "utf-8"))

    # 关闭客户端连接
    client_socket.close()


if __name__ == "__main__":
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_socket.bind(("127.0.0.1", 12345))
    server_socket.listen(128)

    while True:
        client_socket, client_address = server_socket.accept()
        print("[%s, %s]用户连接上了" % client_address)
        handle_client_process = Process(target=handle_client, args=(client_socket,))
        handle_client_process.start()
        client_socket.close()

相关链接

Python实现简单HTTP服务器
nginx 中添加 lua 模块,支持lua脚本以及遇到的坑
NGINX: Compiling and Installing from Source
lua-zlib模块安装、使用

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

上古豆腐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值