nginx lua redis cookie设置失败。
2013-12-12 18:54:14
收藏:0
阅读:96
评论:2
我正在尝试使用lua+nginx+redis来设置cookie。我的思路是,如果cookie不存在,则设置cookie,然后保存到redis。
local redis = require "resty.redis"
local red = redis:new()
local md5 = require "md5"
local ip = ngx.var.remote_addr
local secs = ngx.time()
local uid_key = ip .. secs
local uid = md5.sumhexa(uid_key)
local cookie = ngx.var.cookie_uid
local red_cookie = red:hget("cookie:"..uid, uid)
local ok, err = red:connect("127.0.0.1", 6379)
if not ok then
ngx.say("failed to connect to Redis: ", err)
return
end
local args = ngx.req.get_headers()
local date_time = ngx.http_time(secs)
if cookie == nil or cookie ~= red_cookie then
ngx.header['Set-Cookie'] = "path=/; uid=" .. uid
local res, err = red:hmset("cookie:".. uid,
"uid", uid,
"date_time", date_time,
"user-agent", args["user-agent"]
)
if not res then
ngx.say("failed to set cookie: ", err)
end
end
而我的nginx conf是
...
location /cookie {
default_type "text/plain";
lua_code_cache off;
content_by_lua_file /lua/test.lua;
}
然而,我没有看到cookie被设置。我得到的错误是[error] 63519#0: * 408 after sending out response headers,客户端: 127.0.0.1,服务器: localhost,请求:“GET /cookie HTTP/1.1”,主机:“localhost”。
我似乎无法想出为什么这样不起作用?我也认为我可以纯粹地使用nginx设置cookie。我需要跟踪访问我的页面的用户。有什么想法吗?
谢谢!!
更新
我修改了我的想法,从上游访问点进行redis请求。现在我始终从redis.parser收到无效答复。
local redis = require "resty.redis"
local md5 = require "md5"
local parser = require "redis.parser"
local ip = ngx.var.remote_addr
local secs = ngx.time()
local uid_key = ip .. secs
local uid = md5.sumhexa(uid_key)
local args = ngx.req.get_headers()
local date_time = ngx.http_time(secs)
local test_cookie = ngx.location.capture("/redis_check_cookie", {args = {cookie_uid="cookie:"..uid}});
if test_cookie.status ~= 200 or not test_cookie.body then
ngx.log(ngx.ERR, "failed to query redis")
ngx.exit(500)
end
local reqs = {
{"hmset", "cookie:"..uid, "path=/"}
}
local raw_reqs = {}
for i, req in ipairs(reqs) do
table.insert(raw_reqs, parser.build_query(req))
end
local res = ngx.location.capture("/redis_set_cookie?" .. #reqs,
{ body = table.concat(raw_reqs, "")
})
local replies = parser.parse_replies(res.body, #reqs)
for i, reply in ipairs(replies) do
ngx.say(reply[1])
end
现在我的nginx conf包含:
upstream my_redis {
server 127.0.0.1:6379;
keepalive 1024 single;
}
和
location /redis_check_cookie {
internal;
set_unescape_uri $cookie_uid $arg_cookie_uid;
redis2_query hexists $cookie_uid uid;
redis2_pass my_redis;
}
location /redis_set_cookie {
internal;
redis2_raw_queries $args $echo_request_body;
redis2_pass my_redis;
}
点赞
用户2203643
将下面翻译成中文并且保留原本的 Markdown 格式
朋友们,昨天我使用了我的环境并更改了你们的一些代码,程序运行良好。
但是,你们说你们的代码也不好。
刚刚,我也使用了 resty.redis。但是代码运行良好。
因此,我按照你们的帖子使用了你们的环境和代码,但结果良好。
我无法再提供任何帮助了。
2013-12-13 16:39:04
评论区的留言会收到邮件通知哦~
推荐文章
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?
- addEventListener 返回 nil Lua
- Lua中获取用户配置主目录的跨平台方法
也许您忘记显示其他内容了;
我没有Openresty环境,但我们的环境类似。
下面的代码是我的测试,并且它可以完美运行
这是nginx.conf
location /cookie { default_type "text/plain"; lua_code_cache off; content_by_lua_file test.lua; }这是Lua脚本
local redis = require "redis" local red = redis.connect('192.168.1.51',6379) local ip = ngx.var.remote_addr local secs = ngx.time() local uid_key = ip .. secs local uid = (uid_key) local cookie = ngx.var.cookie_uid local red_cookie = red:hget("cookie:"..uid, uid) local args = ngx.req.get_headers() local date_time = ngx.http_time(secs) if cookie == nil or cookie ~= red_cookie then ngx.header['Set-Cookie'] = "path=/; uid=" .. uid local res, err = red:hmset("cookie:".. uid, "uid", uid, "date_time", date_time, "user-agent", args["user-agent"]) if not res then ngx.say("failed to set cookie: ", err) end end您能显示更多有关您的代码的信息吗?