【问题标题】:Filter request on proxy_pass using lua on nginx在 nginx 上使用 lua 过滤 proxy_pass 上的请求
【发布时间】:2016-03-31 12:59:08
【问题描述】:

我想使用带有 lua 的 nginx 的 proxy_pass 在流媒体服务器之前过滤流媒体 url

我的流媒体服务器是http://localhost:8092

我想在访问http://localhost:8080/streami1?token=mytoken 时转发到http://localhost:8092/stream1。如果您访问http://localhost:8080/streaming1?token=abc,它将显示权限拒绝页面。

这是我在 nginx 配置文件上的代码:

  location ^~ /stream {
            set $flag_false "false";
            set $flag "false";
            set $flag_true 1;
            rewrite_by_lua '
                    local token = ngx.var.arg_token
                    if token == "mytoken" then
                            ngx.var.flag = ngx.var.flag_true
                    end

            ';
            # rewrite_by_lua "ngx.var.flag = ngx.var.flag_true";
            if ($flag = $flag_true) {
                    proxy_pass http://x.x.x.x:8092;
                    break;
            }
            echo "You do not have permission: $flag";
   }

但是,当我使用 url 请求 http://localhost:8080/streaming1?token=mytoken 时,它不会传递给我的流媒体,而是显示“您没有权限:1”。显然,它将标志值更改为 1,但它不会传递给我的流媒体。 我怎么了?。请帮帮我?

【问题讨论】:

    标签: nginx lua


    【解决方案1】:
    1. rewrite_by_lua 指令始终在标准 ngx_http_rewrite_moduleifset 指令)之后运行。您可以改用set_by_lua 指令。
    2. if (condition) {} 语句的 "=" 和 "!=" 运算符将变量与 字符串 进行比较,这意味着 if 条件的 $flag_true 不会被评估为 1

    修改后的配置如下:

        location ^~ /stream {
            set $flag_true 1;
            set_by_lua $flag '
                local token = ngx.var.arg_token
                if token == "mytoken" then
                    return ngx.var.flag_true
                end
                return "false"
            ';
            if ($flag = 1) {
                proxy_pass http://x.x.x.x:8092;
                break;
            }
            echo "You do not have permission: $flag";
        }
    

    【讨论】:

    • 我的问题是什么?这是正确的?。因为 if(condition) {} 语句在检查令牌语句之前执行?
    • @SơnLâm,是的,if(condition){} 语句在检查令牌语句 (rewrite_by_lua) 之前执行。
    猜你喜欢
    • 2021-04-03
    • 2016-11-02
    • 1970-01-01
    • 1970-01-01
    • 2020-03-19
    • 2014-04-09
    • 2011-08-15
    • 2018-10-30
    • 1970-01-01
    相关资源
    最近更新 更多