rewrite_by_lua_block {
        local uri = ngx.var.uri

        -- 实现将/a/index.php?g=Api&m=Apply&a=add
        -- 保持url不变的情况下将请求转发到后端的
        -- /b/backend/web/index.php?r=api/apply/add
        local query_arg_g = ngx.req.get_uri_args()["g"]
        local query_arg_m = ngx.req.get_uri_args()["m"]
        local query_arg_a = ngx.req.get_uri_args()["a"]

        ngx.log(ngx.ERR, "req uri ", uri)
        ngx.log(ngx.ERR, "query_arg_g ", query_arg_g)
        ngx.log(ngx.ERR, "query_arg_m ", query_arg_m)
        ngx.log(ngx.ERR, "query_arg_a ", query_arg_a)

        -- rewrite ^ /lua_rewrite_3; 等价于  ngx.req.set_uri("/lua_rewrite_3", false);
        -- rewrite ^ /lua_rewrite_3 break; 等价于  ngx.req.set_uri("/lua_rewrite_3", false); 加 if/else判断/break/return
        -- rewrite ^ /lua_rewrite_4 last; 等价于  ngx.req.set_uri("/lua_rewrite_4", true);
        -- ngx.req.set_uri_args:重写请求参数,可以是字符串(a=1&b=2)也可以是table;
        if string.find(uri, "^/a/index.php") == 1 and query_arg_g == "Api" and query_arg_m == "Apply" and query_arg_a == "add" then
            ngx.log(ngx.ERR, "should rewrite to yii apply api")
            local backend_uri = "/b/backend/web/index.php"
            ngx.req.set_uri(backend_uri, false)
            local query_args = ngx.req.get_uri_args()
            query_args["r"] = "api/apply/add"
            ngx.req.set_uri_args(query_args)
            return
        end
    }


分类: 运维

0 条评论

发表评论

电子邮件地址不会被公开。 必填项已用*标注