Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract some value from cookie in nginx

Tags:

nginx

I am new to Nginx and hope to get some help.

I want to extract certain data (certain fields set by my PHP scripts) from browser cookie in nginx so that I can log it. If possible, I want to do this just by modifying nginx configuration.

Any pointer/help would be greatly appreciated.

like image 343
kee Avatar asked Sep 30 '14 19:09

kee


2 Answers

You can access cookie values by using the $cookie_COOKIE_NAME_GOES_HERE variable.

See Nginx Documentation

like image 168
Dayo Avatar answered Sep 27 '22 01:09

Dayo


If anyone is using the previous answer with several different cookies in the response the correct regex is:

map $http_cookie $auth_header {     default "";     "~*OAuth.AccessToken=(?<token>[^;]+)" "Bearer $token";   } 

or more general usage:

map $http_cookie $auth_header {     default "";     "~*yourCookieName=(?<variable>[^;]+)" "the value you wanna set $variable";   } 
like image 34
andresbravog Avatar answered Sep 27 '22 01:09

andresbravog