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()
            }
        }
    }
}