lua-io-nginx-module-plus

注意:此庫需要 OpenResty 1.21.4.3 或更高版本。

二進位制安裝程式的資源庫

首先,我們需要使用下面的命令配置二進位制安裝程式的資源庫。命令中的 “CLIENT_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 lua-io-nginx-module-plus -t CLIENT_TOKEN

安裝

適用於 OpenResty-1.21.4.x

對於使用 yum 作為軟體包管理器的 CentOS/RockyLinux/Amazon Linux/Alibaba Cloud Linux/Tecent Linux 作業系統,執行以下命令安裝私有庫。

sudo yum makecache --disablerepo="*" --enablerepo="lua-io-nginx-module-plus"
sudo yum install --disablerepo="*" --enablerepo="lua-io-nginx-module-plus" -y lua-io-nginx-module-plus-1.21.4

對於使用 dnf 作為軟體包管理器的 Fedora 等作業系統,執行以下命令安裝私有庫。

sudo dnf makecache --disablerepo="*" --enablerepo="lua-io-nginx-module-plus"
sudo dnf install --disablerepo="*" --enablerepo="lua-io-nginx-module-plus" -y lua-io-nginx-module-plus-1.21.4

對於使用 apt 作為軟體包管理器的作業系統(如 Ubuntu/Debian),執行以下命令安裝私有庫。

sudo apt-get install -y lua-io-nginx-module-plus-1.21.4

適用於 OpenResty 1.25.3.x

對於使用 yum 作為軟體包管理器的 CentOS/RockyLinux/Amazon Linux/Alibaba Cloud Linux/Tecent Linux 作業系統,執行以下命令安裝私有庫。

sudo yum makecache --disablerepo="*" --enablerepo="lua-io-nginx-module-plus"
sudo yum install --disablerepo="*" --enablerepo="lua-io-nginx-module-plus" -y lua-io-nginx-module-plus-1.25.3

對於使用 dnf 作為軟體包管理器的作業系統(如 Fedora ),執行以下命令安裝私有庫。

sudo dnf makecache --disablerepo="*" --enablerepo="lua-io-nginx-module-plus"
sudo dnf install --disablerepo="*" --enablerepo="lua-io-nginx-module-plus" -y lua-io-nginx-module-plus-1.25.3

對於使用 apt 作為軟體包管理器的作業系統(如 Ubuntu/Debian),執行以下命令安裝私有庫。

sudo apt-get install -y lua-io-nginx-module-plus-1.25.3

升級

適用於 OpenResty-1.21.4.x

對於使用 yum 作為軟體包管理器的 CentOS/RockyLinux/Amazon Linux/Alibaba Cloud Linux/Tecent Linux 作業系統,執行以下命令安裝私有庫。

sudo yum makecache --disablerepo="*" --enablerepo="lua-io-nginx-module-plus"
sudo yum update --disablerepo="*" --enablerepo="lua-io-nginx-module-plus" -y lua-io-nginx-module-plus-1.21.4

對於使用 dnf 作為軟體包管理器的 Fedora 等作業系統,執行以下命令安裝私有庫。

sudo dnf makecache --disablerepo="*" --enablerepo="lua-io-nginx-module-plus"
sudo dnf update --disablerepo="*" --enablerepo="lua-io-nginx-module-plus" -y lua-io-nginx-module-plus-1.21.4

對於使用 apt 作為軟體包管理器的作業系統(如 Ubuntu/Debian),執行以下命令安裝私有庫。

sudo apt-get update
sudo apt-get install -y lua-io-nginx-module-plus-1.21.4

適用於 OpenResty 1.25.3.x

對於使用 yum 作為軟體包管理器的 CentOS/RockyLinux/Amazon Linux/Alibaba Cloud Linux/Tecent Linux 作業系統,執行以下命令安裝私有庫。

sudo yum makecache --disablerepo="*" --enablerepo="lua-io-nginx-module-plus"
sudo yum update --disablerepo="*" --enablerepo="lua-io-nginx-module-plus" -y lua-io-nginx-module-plus-1.25.3

對於使用 dnf 作為軟體包管理器的作業系統(如 Fedora ),執行以下命令安裝私有庫。

sudo dnf makecache --disablerepo="*" --enablerepo="lua-io-nginx-module-plus,coro-nginx-module"
sudo dnf update --disablerepo="*" --enablerepo="lua-io-nginx-module-plus,coro-nginx-module" -y lua-io-nginx-module-plus-1.25.3

對於使用apt作為軟體包管理器的作業系統,如Ubuntu/Debian,執行以下命令安裝私有庫。

sudo apt-get update
sudo apt-get install -y lua-io-nginx-module-plus-1.25.3

適用於 OpenResty 1.27.1.x

對於使用 yum 作為軟體包管理器的 CentOS/RockyLinux/Amazon Linux/Alibaba Cloud Linux/Tecent Linux 作業系統,執行以下命令安裝私有庫。

sudo yum makecache --disablerepo="*" --enablerepo="lua-io-nginx-module-plus"
sudo yum update --disablerepo="*" --enablerepo="lua-io-nginx-module-plus" -y lua-io-nginx-module-plus-1.27.1

對於使用 dnf 作為軟體包管理器的作業系統(如 Fedora ),執行以下命令安裝私有庫。

sudo dnf makecache --disablerepo="*" --enablerepo="lua-io-nginx-module-plus,coro-nginx-module"
sudo dnf update --disablerepo="*" --enablerepo="lua-io-nginx-module-plus,coro-nginx-module" -y lua-io-nginx-module-plus-1.27.1

對於使用 apt作為軟體包管理器的作業系統(如Ubuntu/Debian`),執行以下命令安裝私有庫。

sudo apt-get update
sudo apt-get install -y lua-io-nginx-module-plus-1.27.1

升級 OpenResty

如果先前已安裝了 openresty,請執行以下升級命令:

sudo yum upgrade -y openresty

使用方法

Nginx 配置

在使用 lua-io-nginx-module-plus 之前,需要在配置檔案 nginx.conf 中新增以下配置項,以載入相關動態模組。

# load_module 指令必須位於 nginx.conf 頂部
load_module /usr/local/openresty/nginx/modules/ngx_http_lua_io_module.so;

thread_pool default threads=2 max_queue=10;
...

http {
    location /ngx_io {
        content_by_lua_block {
            local ngx_io = require "ngx.io"

            local file, err = ngx_io.open("/tmp/foo.txt", "w")
            if not file then
                ngx.log(ngx.ERR, "failed to open /tmp/foo.txt: ", err)
                return
            end

            local bytes, err = file:write("hello world\n")
            if err ~= nil then
                ngx.log(ngx.ERR, "failed to write: ", err)
                -- fall through
            end

            local ok, err = file:close()
            if err ~= nil then
                ngx.log(ngx.ERR, "failed to close: ", err)
                return
            end
        }
    }
}