openresty

本文主要介绍了OpenResty的安装,包括在Ubuntu 18.04环境下安装依赖库、下载安装源码包和验证等步骤,还提及了Ubuntu上使用apt安装OpenResty的方法。此外,介绍了lua API、获取请求参数,以及根据body体和Header进行分发代理的不同条件代理方式,并给出相关链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

openresty安装

  1. 安装依赖
    在ubuntu18.04 环境下,openresty的依赖库有:PCRE、OpenSSL、zlib,接下来按如下命令进行安装:
apt-get update
apt-get install libpcre3-dev
apt-get install openssl
apt-get install libssl-dev
apt-get install ruby
apt-get install zlib1g
apt-get install zlib1g.dev

2.下载安装源码包:

wget https://openresty.org/download/openresty-1.13.6.2.tar.gz
tar xzvf openresty-1.13.6.2.tar.gz      
cd openresty-1.13.6.2/
./configure
make
sudo make install

3.验证

cd /usr/local/openresty/
./openresty -v

ubuntu上apt安装 openresty

apt-get update
apt-get install libpcre3-dev
apt-get install openssl
apt-get install libssl-dev
apt-get install ruby
apt-get install zlib1g
apt-get install zlib1g.dev
apt-get install openresty

wget -qO - https://openresty.org/package/pubkey.gpg | sudo apt-key add -
apt-get -y install software-properties-common
add-apt-repository -y "deb http://openresty.org/package/ubuntu $(lsb_release -sc) main"

apt-get install openresty
cd /usr/local/openresty/nginx/conf/
openresty -v
#nginx version: openresty/1.15.8.1

lua API介绍

ngx.var : nginx变量,如果要赋值如ngx.var.b = 2,此变量必须提前声明;另外对于nginx location中使用正则捕获的捕获组可以使用ngx.var[捕获组数字]获取;

ngx.req.get_headers:获取请求头,默认只获取前100,如果想要获取所以可以调用

ngx.req.get_headers(0):获取带中划线的请求头时请使用如headers.user_agent这种方式;如果一个请求头有多个值,则返回的是table;

ngx.req.get_uri_args:获取url请求参数,其用法和get_headers类似;

ngx.req.get_post_args:获取post请求内容体,其用法和get_headers类似,但是必须提前调用ngx.req.read_body()来读取body体(也可以选择在nginx配置文件使用)lua_need_request_body on;开启读取body体,但是官方不推荐);

ngx.req.raw_header:未解析的请求头字符串;

ngx.req.get_body_data:为解析的请求body体内容字符串。

获取请求参数

content_by_lua_block {

        -- 1 say/print方法与echo插件效果相同(say换行,print不换行)
        ngx.say('<h2>test2</h2>')
        ngx.print("<h3>test2</h3>\n")

        -- 2 请求的内容
        -- 请求头,路径,请求方法
        ngx.say(ngx.req.raw_header())-- 所有请求头(\n隔开每个)
        ngx.say(ngx.req.get_headers()['Host'])-- 单个请求头,方法返回值是lua table类型
        ngx.say(ngx.var.uri) --路径借助var,下面会讲 
        ngx.say(ngx.req.get_method()) --方法

        -- 查询字符串
        -- 下面这段是规范的代码处理了异常和数组形式的参数
        local args,err= ngx.req.get_uri_args()
        for key, val in pairs(args) do
            if type(val) == "table" then
                ngx.say(key, ": ", table.concat(val, ", "))
            else
                ngx.say(key, ": ", val)
            end
        end
        -- 简略形式
        ngx.say(ngx.req.get_uri_args()['a'])
        

        -- 请求体
        ngx.req.read_body()  -- explicitly read the req body
        -- 表单形式
        ngx.say(ngx.req.get_post_args()['age'])
        -- 文本形式(json)
        local data = ngx.req.get_body_data()
        if data then
            ngx.say("body data:")
            ngx.print(data)
            return
        end
        

        -- 3 ngx.var调用lua外作用范围的变量
        ngx.say(ngx.var.myvar)         --100
        ngx.say(ngx.var.http_host)     --http_xxx即头部,192.168.0.12:82
        ngx.say(ngx.var.query_string)  --a=10&b=20
        ngx.say(ngx.var.arg_a)         --10
        ngx.say(ngx.var.request_method)--GET
    }

不同条件的代理

(1.)根据body体进行分发代理

location / {
    set $proxy "";
    rewrite_by_lua '
        ngx.req.read_body()
        local body = ngx.var.request_body
        if (body) then
            local match = ngx.re.match(body, "STRING TO FIND")
            if match then
                ngx.var.proxy = "www.ServerA.com"
            else
                ngx.var.proxy = "www.ServerB.com"
            end
        else
            ngx.status = ngx.HTTP_NOT_FOUND
            ngx.exit(ngx.status)
        end
    ';
    proxy_pass http://$proxy$uri;
}

(2.)根据Header进行分发代理

#sample backend
set $backend_host "http://httpbin.org";

location ~*/api/employees {
        rewrite_by_lua '
             --reading request headers
             local req_headers = ngx.req.get_headers()
             local target_uri  = ""
             -- checking an a header value to determine target uri
             if req_headers["x-special-header"] then
                target_uri = req_headers["x-special-header"]
             else 
                -- default path if not header found
                target_uri = "/get"
             end 
            ngx.log(ngx.NOTICE, string.format("resolved target_uri: %s", target_uri))
            --rewriting uri according to header (all original req args and headers are preserved)
            ngx.req.set_uri(target_uri)
        ';
        proxy_pass $backend_host;
}

相关链接

https://openresty.org/cn/installation.htmlhttps://wiki.shileizcc.com/confluence/pages/viewpage.action?pageId=47416041#id-7.HTTP服务-请求头.1https://xiaogenban1993.github.io/19.07/nginx_openresty.htmlhttps://www.wanglibing.com/cn/posts/openresty/https://www.jianshu.com/p/a90438b2281dhttps://moonbingbing.gitbooks.io/openresty-best-practices/content/openresty/get_req_body.htmlhttps://wiki.jikexueyuan.com/project/openresty/openresty/install.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值