I have some lua code where I play with the value of ngx.var.cookie_abc.
The
variable must exist if some assignment is done on it.
To achieve this I did
set $cookie_abc “$cookie_abc”;
The above line clears the value of $cookie_abc, where in I assumed it to
be
defaulted if the value already exists.
i.e. if request has cookie abc=“test” set, the $cookie_abc will have
value
test. But after executing the above expression the value is set to blank
“”.
Where in it should have been “test”. If the cookie value is not present
in
the request then obviously the value should be blank.
Is there a way around it? If not, can it be fixed.
Note even this won’t work
set $tmp_abc “$cookie_abc”;
set $cookie_abc “$tmp_abc”;
There variable values are referential so even $tmp_abc become blank.
Posted at Nginx Forum:
On Mon, Feb 11, 2013 at 04:49:46AM -0500, amodpandey wrote:
i.e. if request has cookie abc=“test” set, the $cookie_abc will have value
There variable values are referential so even $tmp_abc become blank.
map $cookie_abc $abc {
‘’ default;
default $cookie_abc;
}
will set $abc to the value of $cookie_abc if not empty, or to
“default” if cookie is unset or empty.
http://nginx.org/r/map
Thank you. But this is not what I am looking for. The question is about
defining $cookie_abc and default it to $cookie_abc itself (if the value
exists).
the map will give me the value in $abc which I am not looking for.
BTW set $cookie_abc “$abc”; will still reset the value of $abc to
default
Posted at Nginx Forum:
The variable $cookie_abc will exist only if the client request cookie
has
“abc”. In my case the first request won’t have this cookie set and I am
setting this value for some magic.
Lua module is very this on this feature. If we do not have the variable
defined you should set it before using.
set $cookie_abc “$cookie_abc”
If the above works as expected I am good.
Posted at Nginx Forum:
On Monday 11 February 2013 17:41:05 amodpandey wrote:
Thank you. But this is not what I am looking for. The question is about
defining $cookie_abc and default it to $cookie_abc itself (if the value
exists).
the map will give me the value in $abc which I am not looking for.
BTW set $cookie_abc “$abc”; will still reset the value of $abc to default
All magic variable $cookie_, $http_, $upstream_http_* are always
exist.
By defining “set $cookie_abc” you just hid the original variable, so
don’t
do that.
It more looks like a bug in the lua module.
wbr, Valentin V. Bartenev
On Monday 11 February 2013 22:07:18 amodpandey wrote:
The variable $cookie_abc will exist only if the client request cookie has
“abc”. In my case the first request won’t have this cookie set and I am
setting this value for some magic.
It always exists. For clients without the cookie it has empty value.
Lua module is very this on this feature. If we do not have the variable
defined you should set it before using.
set $cookie_abc “$cookie_abc”
If the above works as expected I am good.
Yes. It works as expected.
wbr, Valentin V. Bartenev
–
http://nginx.org/en/donation.html
It should not and it does not! If the client does not send a cookie with
name “abc” or “def” I do not assume nginx to have any variable with that
name. Am I missing anything?
Posted at Nginx Forum:
You should give a better (code test case) example of what you want to do
If you are using lua then i’m sure there will be a solution
Hello!
On Mon, Feb 11, 2013 at 1:49 AM, amodpandey wrote:
I have some lua code where I play with the value of ngx.var.cookie_abc. The
variable must exist if some assignment is done on it.
To achieve this I did
set $cookie_abc “$cookie_abc”;
The above line clears the value of $cookie_abc, where in I assumed it to be
defaulted if the value already exists.
Are you sure? Which version of ngx_lua and Nginx are you using?
I’ve tried the following minimal example on my side with Nginx 1.2.6 +
ngx_lua 0.7.14 and it works as expected:
location = /t {
content_by_lua '
ngx.say("cookie abc: ", ngx.var.cookie_abc)
';
}
And let’s use curl to access location = /t (assuming the Nginx is
listening on the local port 8080):
$ curl -H 'Cookie: abc=32' localhost:8080/t
cookie abc: 32
$ curl localhost:8080/t
cookie abc: nil
Please note that your request must actually take a Cookie request
header, otherwise the value of $cookie_abc will surely be empty (or
nil, to be more accurate).
Best regards,
-agentzh
Hello!
On Mon, Feb 11, 2013 at 7:21 PM, amodpandey wrote:
Set the value of $cookie_abc to “a”/“b” (some logic) if the cookie value is
not coming in the request else use the value set. I am doing this in
If you want to set a cookie (i.e., adding Set-Cookie response headers
on the HTTP protocol level), then assigning to the Nginx variable
$cookie_XXX will not do what you want. (This has nothing to do with
Lua and this is how the Nginx core works right now.)
To achieve that, you need to add the Set-Cookie response headers
explicitly. For example, in Lua you can do something like this:
ngx.header['Set-Cookie'] = {'a=32; path=/', 'b=4; path=/'}
will yield the HTTP response headers
Set-Cookie: a=32; path=/
Set-Cookie: b=4; path=/
Best regards,
-agentzh
Thank you for your response.
Versions
nginx versions tried nginx/1.2.5 and nginx/1.3.9
LuaJIT 2.0.0
What I want achive?
Set the value of $cookie_abc to “a”/“b” (some logic) if the cookie value
is
not coming in the request else use the value set. I am doing this in
server level
set $cookie_abc “$cookie_abc”;
set $tmp_abc “”;
set_by_lua $tmp_abc ’
common.set_abc_cookie()
';
I am using set_by_lua to make sure the cookie value is set before the
rewrites are evaluated.
Why I am doing this?
I have used $cookie_abc variable in my config and I want to have “a”/“b”
value depending on a logic if the cookie is not passed.
What is not working?
inside common.set_abc_cookie()
ngx.var.cookie_abc = “a” if the cookie is not passed.
This is expected. That is why I am doing
set $cookie_abc “$cookie_abc”;
Posted at Nginx Forum: