首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >nginx lua redis cookie不设置

nginx lua redis cookie不设置
EN

Stack Overflow用户
提问于 2013-12-11 19:55:01
回答 2查看 3.7K关注 0票数 2

我正在尝试用lua+nginx+redis设置一个曲奇。这就是我的想法:如果cookie不存在,设置cookie,然后保存到redis。

代码语言:javascript
复制
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公司

..。

代码语言:javascript
复制
location /cookie {
    default_type "text/plain";
    lua_code_cache off;
    content_by_lua_file /lua/test.lua;
}

不过,我没有看到曲奇饼。我得到错误63519#0:*408试图在发送响应头后设置ngx.header.HEADER,客户机: 127.0.0.1,服务器: localhost,请求:"GET /cookie HTTP/1.1",主机:"localhost“

我好像搞不懂为什么这不管用?我还以为我可以用纯nginx做曲奇饼。我需要跟踪访问我的页面的用户。有什么想法吗?

谢谢!!

更新

我修改了我的想法,从上游接入点发出redis请求。现在我一直从redis.parser那里得到一个无效的回复。

代码语言:javascript
复制
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公司现在有:

代码语言:javascript
复制
upstream my_redis {
    server 127.0.0.1:6379;
    keepalive 1024 single;
}

代码语言:javascript
复制
    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;
    }
EN

回答 2

Stack Overflow用户

发布于 2013-12-12 16:05:45

也许你忘了展示其他的东西;

我没有开放的环境,但我们的环境是相似的。

下面的代码是我的测试,它运行得很完美。

我是nginx.conf

代码语言:javascript
复制
location /cookie {
    default_type "text/plain";
    lua_code_cache off;
    content_by_lua_file test.lua;
} 

这是lua脚本

代码语言:javascript
复制
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

你会显示更多关于你的代码吗?

票数 0
EN

Stack Overflow用户

发布于 2013-12-13 16:39:04

朋友们,昨天我用自己的环境修改了你的一些代码,程序运行正常。

但是,你说你的代码也很糟糕。

刚才,我还使用了resty.redis。但代码运行正常。

因此,我根据您的帖子使用您的环境和代码,但结果是可以的。

我不能再帮你了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20528680

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档