lua-resty-http-fast

Note: OpenResty 1.21.4.3 or higher is required for this library.

The repository of the binary installer

First, we need to configure the repository of the binary installer using the commands below. (The CLIENT_TOKEN in the commands needs to be replaced with a valid Token from the subscription email)

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

Installation

For OpenResty-1.21.4.x

For CentOS/RockyLinux/Amazon Linux/Alibaba Cloud Linux/Tecent Linux operating systems using yum as the package manager, execute the following command to install the private libraries.

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

For operating systems such as Fedora that use dnf as package manager, execute the following command to install the private library.

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

For operating systems such as Ubuntu/Debian that use apt as the package manager, run the following command to install the private libraries.

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

For OpenResty 1.25.3.x

For CentOS/RockyLinux/Amazon Linux/Alibaba Cloud Linux/Tecent Linux operating systems using yum as the package manager, execute the following command to install the private libraries.

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

For operating systems such as Fedora that use dnf as package manager, execute the following command to install the private library.

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

For operating systems such as Ubuntu/Debian that use apt as the package manager, run the following command to install the private libraries.

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

Upgrade

For OpenResty-1.21.4.x

For CentOS/RockyLinux/Amazon Linux/Alibaba Cloud Linux/Tecent Linux operating systems using yum as the package manager, execute the following command to install the private libraries.

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

For operating systems such as Fedora that use dnf as package manager, execute the following command to install the private library.

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

For operating systems such as Ubuntu/Debian that use apt as the package manager, run the following command to install the private libraries.

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

For OpenResty 1.25.3.x

For CentOS/RockyLinux/Amazon Linux/Alibaba Cloud Linux/Tecent Linux operating systems using yum as the package manager, execute the following command to install the private libraries.

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

For operating systems such as Fedora that use dnf as package manager, execute the following command to install the private library.

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

For operating systems such as Ubuntu/Debian that use apt as the package manager, run the following command to install the private libraries.

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

Upgrade OpenResty

If openresty has been previously installed, please execute the following upgrade command:

sudo yum upgrade -y openresty

Usage

Nginx configuration

Before you can use lua-resty-http-fast, you need to add the following configuration items to the configuration file nginx.conf to load the related dynamic modules.

# 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_package_path "/usr/local/openresty/site/lualib/?.ljbc;;";
    ...
}

Lua interfaces

The Lua interfaces of this library are basically consistent with the open source library lua-resty-http. For full usage of lua-resty-http-fast, see the official documentation.

Here are some examples of usage:

Simple HTTP request

# 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_package_path "/usr/local/openresty/site/lualib/?.ljbc;;";

    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";
        }
    }
}

HTTPS request using mTLS

# 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_package_path "/usr/local/openresty/site/lualib/?.ljbc;;";

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