lua-nginx-module icon indicating copy to clipboard operation
lua-nginx-module copied to clipboard

rewriting existing cookie | getting old cookies value

Open sunnykrGupta opened this issue 11 years ago • 2 comments

local cookie1 = 'Foo=abc; path=/';
local cookie2 = 'PID=123123hj9-12312mn3b1n2-123213213; path=/';
local cookie3 = 'uzma_a=uzma123213213; path=/';;

ngx.header['Set-Cookie'] = {cookie1, cookie2, cookie3};

---------------- these above cookies has been set on browser

local headers = ngx.req.get_headers();     -- print ngx headers
---- i see these cookies coming in future reqests.

----- Now i want to change these cookies before proxy pass to remote server
------ after checking cookies existence [ like below ]

local cval = ngx.var.cookie_PID;
---------- checking if exist then assign new value
local cookie2 = "";
if cval == 'nil' then
    ngx.say("nope!! no pid---");
else
    cookie2 = 'PID='  .. ngx.time() ..  '; path=/';
    ngx.say("yes pid is there and new PID generated---");
end

-- setting cookie varibles

local cookie1 = 'Foo=abc; path=/';
local cookie3 = 'uzma_a=uzma123213213; path=/';

ngx.header['Set-Cookie'] = {cookie1, cookie2, cookie3,};

--- but cookies is not set after above modification 
--- i see old cookies coming to remote server why is that ? is there anything i am not doing correct?

-- it is throwing

2015/08/31 18:16:55 [error] 5631#0: *258 attempt to set ngx.header.HEADER after sending out response headers, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1:8080"

sunnykrGupta avatar Aug 31 '15 09:08 sunnykrGupta

Now it's solved. I was printing by ngx.say API that itself is response back to browser that is why it was not settting my changed cookie .. Now i commented those ngx.say API, it is working like a charm.

sunnykrGupta avatar Aug 31 '15 12:08 sunnykrGupta

@sunnykrGupta Glad you figured it out :) Yeah, care must be taken in the streaming output model.

agentzh avatar Sep 01 '15 04:09 agentzh