在Nginx中为Redis添加一个键/值
我想从nginx与redis进行通信,以便在列表中存储对图像提出的请求,特别是在另一台服务器上代理的未找到的图像上 我安装了OpenResty,以便使用在Nginx中为Redis添加一个键/值,nginx,lua,redis,openresty,Nginx,Lua,Redis,Openresty,我想从nginx与redis进行通信,以便在列表中存储对图像提出的请求,特别是在另一台服务器上代理的未找到的图像上 我安装了OpenResty,以便使用redis2\u query和redis2\u pass命令 这是我的nginx配置: location ~* \.(jpg|jpeg|gif|png)$ { try_files $uri @imagenotfound; redis2_query lpush founds $uri; redis2_pass 127.0.
redis2\u query
和redis2\u 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\u pass
返回查询结果。是否仍然不返回此结果而只执行查询
如果我删除redis2\u查询
和redis2\u传递
,图像将正确显示
提前感谢您的帮助 一个似乎有效的解决方案是使用Lua脚本,通过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技能,并且可以告诉我这是否是正确的方法,我将很高兴得到他的反馈
无论如何,感谢您在评论中的帮助。为什么这是一个问题?是因为您在HTTP响应中得到了这些信息吗?与这个nginx模块完全不兼容,所以我只是想了解这个问题,看看我是否能提供帮助。因为我希望nginx返回图像,而不是redis查询结果的整数……我不是最好的帮助者,所以这是一个随机的结果。但是用
$uri
完成location
方法会有帮助吗?我也是这个模块的新手!据我所知,该模块允许将数据作为缓存存储在redis中,并使用redis_pass检索数据。我只需要在redis中存储数据。你能举例说明你的建议吗?谢谢你的帮助。如果你把redis指令放在第一位会怎么样?