openrestry动态路由

作者: 大鹏一怒乘风起 | 来源:发表于2021-09-13 10:36 被阅读0次
    mkdir /data/nginx/conf.d/ssl /data/nginx/conf.d/lua -p
    #lua脚本文件
    cd /data/nginx/conf.d/lua/
    cat >split.lua<<EOF
    function split(str, pat)
    local t = {}
    local fpat = "(.-)" .. pat
    local last_end = 1
    local s, e, cap =str:find(fpat,1)
    while s do
    if s ~= 1 or cap ~= "" then
    table.insert(t,cap)
    end
    last_end = e+1
    s, e, cap = str:find(fpat, last_end)
    end
    if last_end <= #str then
    cap = str:sub(last_end)
    table.insert(t, cap)
    end
    return t
    end
    function split_path(str)
    return split(str,'[\\/]+')
    end
    EOF
    
    #nginx 配置文件
    cd /data/nginx/conf.d/
    cat > require.conf <<EOF
    init_worker_by_lua_file "/etc/nginx/conf.d/lua/split.lua";
    server {
            listen 80;
            server_name *.com;
            index index.html index.htm index.php;
            location = /redis {
                internal;
                redis2_raw_queries $args $echo_request_body;
                redis2_pass 10.1.1.123:6379;
            }
            location / {
                set $target '';            
                access_by_lua '
            local parameters = split_path(ngx.var.uri)
            local action = parameters[1]
            local action1 = parameters[2]
            local action2 = ngx.var.host
            function LsReadis(key)
              local parser = require "redis.parser"
              local reqs = {
                 {"auth", "xxxx"},
                 {"get", key}
                    }  
      local raw_reqs = {}
     for i, req in ipairs(reqs) do
    table.insert(raw_reqs, parser.build_query(req))
      end
       local res = ngx.location.capture("/redis?" .. #reqs,
         { body = table.concat(raw_reqs, "") })
      if res.status ~= 200 or not res.body then
         ngx.log(ngx.ERR, "failed to query redis")
         ngx.exit(500)
       end
       replies = parser.parse_replies(res.body, #reqs)
         return replies[2][1]
            end
            if ngx.var.uri == "/" then
        if LsReadis("rdc-domain:"..action2) == nil then
        ngx.exit(501)
        else
           server=LsReadis("rdc-domain:"..action2)
        end
            end
           if server == "" then
              server = "www.xxx.com"
            end
           ngx.var.target = server
           ';
           resolver 8.8.8.8;
           proxy_pass http://$target;
           proxy_set_header Host $host;
           proxy_set_header  X-Real-IP        $remote_addr;
           proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
        }
    }
    EOF
    
    
    docker run -d -p 80:80 -p 443:443 --restart=always -v `pwd`/conf.d:/etc/nginx/conf.d --name openresty openresty/openresty
    

    相关文章

      网友评论

        本文标题:openrestry动态路由

        本文链接:https://www.haomeiwen.com/subject/reaxgltx.html