Nginx:通过将标头值与文件内容进行比较来授予访问权限; 可能?

aak*_*aki 2 authentication lua nginx

我想通过比较自定义标头值与文件中的条目来限制对某些静态文件的访问.基本上我想在另一个(受限制的)应用程序中生成这样的令牌,让Nginx拒绝所有具有不匹配标头和标头值(令牌)的请求.

到目前为止我所读到的是,使用HttpLuaModule可以扩展Nginx编写Lua代码.但我不知道我的想法是否有任何机会可行.

那么,是否有使用标准nginx模块的简单解决方案(首选)?或者是通过重新安装/编译包含前面提到的模块的nginx安装Lua模块我唯一的机会?

KSD*_*mon 7

您可以通过lua轻松完成此操作.检查lua-nginx-module的文档,如aaki所述.所需的一切 - 只需在获取请求阶段插入逻辑,例如:

location / {
    lua_need_request_body on;

    client_max_body_size 100k;
    client_body_buffer_size 100k;

    access_by_lua '
        -- check the client IP address is in our black list
        if ngx.var.remote_addr == "132.5.72.3" then
            ngx.exit(ngx.HTTP_FORBIDDEN)
        end

        -- check if the request body contains bad words
        if ngx.var.request_body and
                 string.match(ngx.var.request_body, "fsck")
        then
            return ngx.redirect("/terms_of_use.html")
        end

        local f = io.open("/tmp/foo")
        local token = f:read("a")
        local user_token = ngx.req.get_headers()["user-session-token"]

        if not user_token or user_token ~= token then
            ngx.exit(ngx.HTTP_FORBIDDEN)
        end

        -- tests passed
    ';

    # proxy_pass/fastcgi_pass/etc settings/proceed the request and so on
 }
Run Code Online (Sandbox Code Playgroud)