nginx lua redis cookie not setting

2019-08-04 19:16发布

问题:

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

and my nginx conf

...

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

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"

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?

Thanks!!

Update

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

and my nginx conf now has:

upstream my_redis {
    server 127.0.0.1:6379;
    keepalive 1024 single;
}

and

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

回答1:

maybe u forget to display some other things;

i don't have openresty environment; but our environment is similar.

the code below is my test, and it run perfectly

this is nginx.conf

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

this is lua script

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

will u display more about your code?



回答2:

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.

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