3

我想从 nginx 与 redis 通信,以便将已对图像发出的请求存储在列表中,尤其是在未在另一台服务器上代理的图像上。

我安装了 OpenResty,以便使用redis2_queryredis2_pass命令。

这是我的 nginx 配置:

location ~* \.(jpg|jpeg|gif|png)$ {
    try_files $uri @imagenotfound;

    redis2_query lpush founds $uri;
    redis2_pass 127.0.0.1:6379;

}

location @imagenotfound {

    proxy_pass http://imgdomain.com/$uri;
    proxy_set_header Host imgdomain.com;
    proxy_set_header Server imgdomain.com;

    redis2_query lpush notfounds $uri;
    redis2_pass 127.0.0.1:6379;

}

我提出的每个请求都返回一个整数,并且据我所知,redis2_pass返回查询的结果。无论如何不返回此结果而只执行查询?

如果我删除redis2_queryand redis2_pass,图像将正确显示。

在此先感谢您的帮助!

4

1 回答 1

2

一个似乎可行的解决方案是将 Lua 脚本与 access_by_lua 和 resty.redis 模块一起使用:

location ~* \.(jpg|jpeg|gif|png)$ {
    try_files $uri @imagenotfound;

    access_by_lua '
                    local redis = require "resty.redis"
                    local red = redis:new()
                    red:set_timeout(1000)
                    local ok, err = red:connect("127.0.0.1", 6379)
                    if not ok then
                        ngx.say("failed to connect: ", err)
                        return
                    end
                    ok, err = red:lpush("founds", ngx.var.uri)
                    if not ok then
                        ngx.say("failed to set founds: ", err)
                        return
                    end
            ';


}

location @imagenotfound {

    proxy_pass http://imgdomain.com/$uri;
    proxy_set_header Host imgdomain.com;
    proxy_set_header Server imgdomain.com;

     access_by_lua '
                    local redis = require "resty.redis"
                    local red = redis:new()
                    red:set_timeout(1000)
                    local ok, err = red:connect("127.0.0.1", 6379)
                    if not ok then
                        ngx.say("failed to connect: ", err)
                        return
                    end
                    ok, err = red:lpush("notfounds", ngx.var.uri)
                    if not ok then
                        ngx.say("failed to set notfounds: ", err)
                        return
                    end
            ';


}

如果有人有 Lua 技能并且可以告诉我这是否是正确的方法,我会很高兴得到他的反馈!

无论如何,感谢您在评论中的帮助。

于 2015-03-03T17:00:01.383 回答