lua-resty-http-fast

注意: 该库要求 OpenResty 1.21.4.3 及以上版本

配置二进制安装包仓库

首先我们需要配置二进制安装包的仓库,按照以下命令进行配置。(命令中的 CLIENT_TOKEN 需要替换成订阅邮件中的有效 Token)

curl -o get-xray-priv-lib-repo.sh https://pkg2.openresty.com/scripts/get-xray-priv-lib-repo.sh
sudo bash get-xray-priv-lib-repo.sh -l coro-nginx-module -t CLIENT_TOKEN
sudo bash get-xray-priv-lib-repo.sh -l lua-resty-http-fast -t CLIENT_TOKEN

安装

针对 OpenResty-1.21.4.x

使用 yum 作为包管理器的 CentOS/RockyLinux/Amazon Linux/Alibaba Cloud Linux/Tecent Linux 等操作系统,执行以下命令进行私有库的安装。

sudo yum makecache --disablerepo="*" --enablerepo="lua-resty-http-fast,coro-nginx-module,openresty"
sudo yum install --disablerepo="*" --enablerepo="lua-resty-http-fast,coro-nginx-module,openresty" -y lua-resty-http-fast-1.21.4

使用 dnf 作为包管理器的 Fedora 等操作系统,执行以下命令进行私有库的安装。

sudo dnf makecache --disablerepo="*" --enablerepo="lua-resty-http-fast,coro-nginx-module,openresty"
sudo dnf install --disablerepo="*" --enablerepo="lua-resty-http-fast,coro-nginx-module,openresty" -y lua-resty-http-fast-1.21.4

使用 apt 作为包管理器的 Ubuntu/Debian 等操作系统,执行以下命令进行私有库的安装。

sudo apt-get install -y lua-resty-http-fast-1.21.4

针对 OpenResty 1.25.3.x

使用 yum 作为包管理器的 CentOS/RockyLinux/Amazon Linux/Alibaba Cloud Linux/Tecent Linux 等操作系统,执行以下命令进行私有库的安装。

sudo yum makecache --disablerepo="*" --enablerepo="lua-resty-http-fast,coro-nginx-module,openresty"
sudo yum install --disablerepo="*" --enablerepo="lua-resty-http-fast,coro-nginx-module,openresty"  -y lua-resty-http-fast-1.25.3

使用 dnf 作为包管理器的 Fedora 等操作系统,执行以下命令进行私有库的安装。

sudo dnf makecache --disablerepo="*" --enablerepo="lua-resty-http-fast,coro-nginx-module,openresty"
sudo dnf install --disablerepo="*" --enablerepo="lua-resty-http-fast,coro-nginx-module,openresty" -y lua-resty-http-fast-1.25.3

使用 apt 作为包管理器的 Ubuntu/Debian 等操作系统,执行以下命令进行私有库的安装。

sudo apt-get install -y lua-resty-http-fast-1.25.3

升级 OpenResty

如果此前已经安装过了 openresty,请执行以下更新命令:

sudo yum upgrade -y openresty

使用

Nginx 配置

在使用前需要在配置文件 nginx.conf 中需要添加以下这些配置项,用以加载 lua-resty-http-fast 相关的动态模块。

# The load_module directive must be on top of nginx.conf
load_module "/usr/local/openresty-coro-nginx-module/lib/ngx_http_coro_module.so";
load_module "/usr/local/openresty-coro-nginx-module/lib/ngx_http_coro_libcurl_module.so";

...

http {
    coro_preload /usr/local/openresty/openssl111/lib/libcrypto.so;
    coro_preload /usr/local/openresty/openssl111/lib/libssl.so;
    coro_preload /usr/local/openresty/libcurl/lib/libcurl.so;

    coro_stack_size  65536;
    ...
}

Lua 接口

本库的 Lua 接口与开源的 lua-resty-http 基本保持一致。 如需了解 lua-resty-http-fast 的完整用法,请参阅官方文档

以下是一些使用示例:

简单的 HTTP 请求

# The load_module directive must be on top of nginx.conf
load_module "/usr/local/openresty-coro-nginx-module/lib/ngx_http_coro_module.so";
load_module "/usr/local/openresty-coro-nginx-module/lib/ngx_http_coro_libcurl_module.so";
...

http {
    coro_preload /usr/local/openresty/openssl111/lib/libcrypto.so;
    coro_preload /usr/local/openresty/openssl111/lib/libssl.so;
    coro_preload /usr/local/openresty/libcurl/lib/libcurl.so;
    coro_stack_size  65536;

    server {
        location /a {
            # need to specify the resolver to resolve the hostname
            resolver 8.8.8.8;

            content_by_lua_block {
                local http = require "resty.http.fast"
                local httpc = http.new()
                httpc:connect{
                    scheme = "http",
                    host = "127.0.0.1",
                    port = ngx.var.server_port
                }

                local res, err = httpc:request{
                    path = "/b"
                }

                ngx.status = res.status
                ngx.print(res:read_body())

                httpc:close()
            }
        }

        location = /b {
            echo "OK";
        }
    }
}

使用 mTLS 的 HTTPS 请求

# The load_module directive must be on top of nginx.conf
load_module "/usr/local/openresty-coro-nginx-module/lib/ngx_http_coro_module.so";
load_module "/usr/local/openresty-coro-nginx-module/lib/ngx_http_coro_libcurl_module.so";

...

http {
    coro_preload /usr/local/openresty/openssl111/lib/libcrypto.so;
    coro_preload /usr/local/openresty/openssl111/lib/libssl.so;
    coro_preload /usr/local/openresty/libcurl/lib/libcurl.so;
    coro_stack_size  65536;

    server {
        location /a {
            # need to specify the resolver to resolve the hostname
            resolver 8.8.8.8;

            content_by_lua_block {
                local function read_file(path)
                    local file, err = io.open(path, "rb")
                    if not file then
                        error(file ~= nil, err)
                    end

                    local content = file:read("*a")
                    file:close()
                    return content
                end

                local cert_data, err = read_file("/path/to/mtls_client.crt")
                local key_data, err = read_file("/path/to/mtls_client.key")

                -- This is optional
                local ca_cert_data = read_file("/path/to/test.crt")

                local httpc = assert(require('resty.http.fast').new())

                local ok, err = httpc:connect {
                    scheme = 'https',
                    host = '127.0.0.1',
                    port = $TEST_NGINX_RAND_PORT_2,
                    ssl_client_cert_pem = cert_data,
                    ssl_client_priv_key_pem = key_data,
                    ssl_server_ca_pem = ca_cert_data,
                    ssl_verify = false,
                }

                if ok and not err then
                local res, err = assert(httpc:request {
                    method = 'GET',
                    path = '/',
                    headers = {
                        ['Host'] = 'example.com',
                    },
                })

                ngx.say(res:read_body())
                end

                httpc:close()
            }
        }
    }
}