Note that there are some explanatory texts on larger screens.

plurals
  1. POnginx lua redis cookie not setting
    primarykey
    data
    text
    <p>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. </p> <pre><code>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 </code></pre> <p>and my nginx conf</p> <p>...</p> <pre><code>location /cookie { default_type "text/plain"; lua_code_cache off; content_by_lua_file /lua/test.lua; } </code></pre> <p>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"</p> <p>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? </p> <p>Thanks!!</p> <p><strong>Update</strong></p> <p>I revised my idea to make redis requests from an upstream access point. Now I keep getting an invalid reply from redis.parser. </p> <pre><code>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 </code></pre> <p>and my nginx conf now has:</p> <pre><code>upstream my_redis { server 127.0.0.1:6379; keepalive 1024 single; } </code></pre> <p>and </p> <pre><code> 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; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload