nginx lua redis cookie 未设置

标签 nginx lua redis

我正在尝试使用 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 配置

...

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

但是我没有看到 cookie 集。我得到 [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。我需要跟踪访问我页面的用户。有什么想法吗?

谢谢!!

更新

我修改了我的想法,从上游访问点发出 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;
    }

最佳答案

也许你忘了显示一些其他的东西;

我没有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

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

关于nginx lua redis cookie 未设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20528680/

相关文章:

node.js - Node js通过url在redis中插入一条数据

ruby-on-rails - 请升级到 Ruby 1.9.3 或更新版本以继续

php - 启用mysql允许php访问使用ubuntu服务器作为web服务器

lua - luarocks 管理是否对项目有 "./node_modules"等效项?

lua - 如何使用lua构造读写管道?

regex - gui的Lua模式

PHP 警告 : PHP Startup: Unable to load dynamic library 'redis.so'

linux - 如何在 Pressmatic 上用 PHP 启用 SOAP 客户端

html - Web应用程序上的登录页面?

用于 IODocs 的 Heroku 上的 node.js 和 Redis