• openresty


    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>
    ")
    
            -- 2 请求的内容
            -- 请求头,路径,请求方法
            ngx.say(ngx.req.raw_header())-- 所有请求头(
    隔开每个)
            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.html
    https://wiki.shileizcc.com/confluence/pages/viewpage.action?pageId=47416041#id-7.HTTP服务-请求头.1
    https://xiaogenban1993.github.io/19.07/nginx_openresty.html
    https://www.wanglibing.com/cn/posts/openresty/
    https://www.jianshu.com/p/a90438b2281d
    https://moonbingbing.gitbooks.io/openresty-best-practices/content/openresty/get_req_body.html
    https://wiki.jikexueyuan.com/project/openresty/openresty/install.html

  • 相关阅读:
    Neural Collaborative Filtering 神经网络协同过滤
    pyspark(一) 常用的转换操作
    HO引擎近况20210315
    LeetCode–前 K 个高频元素
    常用十大算法(七)— 克鲁斯卡尔算法
    常用十大算法(六)— 普里姆算法
    LeetCode–数值的整数次方
    LeetCode–二叉树的层次遍历 II
    常用十大算法(五)— 贪心算法
    LeetCode–二进制中1的个数
  • 原文地址:https://www.cnblogs.com/tomtellyou/p/14202320.html
Copyright © 2020-2023  润新知