2014-09-30 2 views
20

저는 Nginx에 익숙하지 않아 도움이되기를 바랍니다.nginx의 쿠키에서 일부 값을 추출하는 방법

nginx의 브라우저 쿠키에서 특정 데이터 (특정 PHP 스크립트로 설정된 필드)를 추출하여 기록 할 수 있습니다. 가능한 경우, nginx 구성을 수정하여이 작업을 수행하려고합니다.

모든 포인터/도움을 크게 주시면 감사하겠습니다.

+1

http://nginx.org/en/docs/http/ngx_http_core_module.html#var_cookie_ –

답변

31

$cookie_COOKIE_NAME_GOES_HERE 변수를 사용하여 쿠키 값에 액세스 할 수 있습니다.

여기

+7

누군가의 시간을 절약하기 위해이 방법은 영숫자와 '_'가있는 쿠키에서만 작동합니다. user.id 또는 user [id]라는 쿠키가 있으면 (예를 들어) nginx 맵 기능과 함께 $ http_cookie를 사용해야합니다. http://nginx.org/en/docs/http/ngx_http_map_module.html#map – LuisClemente

+1

@ LuisClemente - 당신이 어떻게 할 것인가에 대한 저격의 기회? – Guy

10

Nginx Documentation는 Http 만 쿠키를 추출하여 OAuth를 무기명 토큰으로 편안한 API에게 전달하는 예제를 참조하십시오 :

http { 

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

    server { 
    listen    443 ssl; 

    ssl_certificate  /etc/nginx/certs/nginx.crt; 
    ssl_certificate_key /etc/nginx/certs/nginx.key; 

    proxy_set_header  Authorization $auth_header; 

    location/{ 
     proxy_pass   https://rest-api-host.domain.com/; 
    } 

    } 

} 
2

사람이 여러 가지 쿠키 이전의 대답을 사용하는 경우 응답에 올바른 정규식은 다음과 같습니다

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

이상의 일반적인 사용 :

map $http_cookie $auth_header { 
    default ""; 
    "~*yourCookieName=(?<variable>[^;]+)" "the value you wanna set $variable"; 
    } 
관련 문제