nginx lua redis cookie未设置 [英] nginx lua redis cookie not setting

查看:212
本文介绍了nginx lua redis cookie未设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用lua + nginx + redis设置cookie.这是我的想法:如果cookie不存在,则设置cookie,然后保存到redis.

I am trying to set a cookie with lua+nginx+redis. This is my idea: set cookie if cookie doesn't exist and then save to 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

and my nginx conf

...

location /cookie {
    default_type "text/plain";
    lua_code_cache off;
    content_by_lua_file /lua/test.lua;
}

但是我没有看到cookie集.我收到[错误] 63519#0:* 408尝试发送响应头后设置ngx.header.HEADER,客户端:127.0.0.1,服务器:localhost,请求:"GET/cookie HTTP/1.1",主机:"localhost "

I am not seeing the cookie set however. I get [error] 63519#0: *408 attempt to set ngx.header.HEADER after sending out response headers, client: 127.0.0.1, server: localhost, request: "GET /cookie HTTP/1.1", host: "localhost"

我似乎无法弄清楚为什么这行不通?我还以为我可以只用nginx设置cookie.我需要跟踪访问我页面的用户.有什么想法吗?

I can't seem to figure out why this wouldn't work? I also thought I could set cookies with purely nginx. I need to track users who visit my page. Any thoughts?

谢谢!

更新

我修改了想法,以从上游访问点发出Redis请求.现在,我不断收到来自redis.parser的无效回复.

I revised my idea to make redis requests from an upstream access point. Now I keep getting an invalid reply from 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现在具有:

and my nginx conf now has:

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;
    }

推荐答案

朋友,昨天我使用自己的环境并更改了一些代码,程序运行正常.

friends, yesterday i use my own environment and change some of your code, the program run ok.

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

But, u say your code is also bad.

现在,我还使用resty.redis.但是代码可以正常运行.

Just now, i also use the resty.redis. But the code runs ok.

所以,我根据您的帖子使用您的环境和您的代码,但结果还可以.

So, i use your environment and your code according to your post, but the result is ok.

我无法再为您提供帮助.

I can't provide u help any more.

这篇关于nginx lua redis cookie未设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆