title: How to read cookie in nginx
body: <p>I am very novice in UI programming and in nginx. Sorry if the question is dumb. Also I tried almost all the answers present in stackoverflow questions similar to this but nothing helped me to solve this issue</p>
  
  <p><strong>What I need to do :</strong></p>
  
  <p>Check whether a cookie named "AUTH_COOKIE" present. If its present proxy_pass to a site1. Else proxy_pass site2. For this I need to read the cookie.</p>
  
  <p><strong>What I tried :</strong></p>
  
  <p>I tried to read the cookie as $cookie_AUTH_COOKIE, but it doesn't provide me any result. I tried reading using $http_cookie. No use with that as well</p>
  
  <p>I tried following the gist present over here <a href="https://gist.github.com/rnorth/2031652" rel="nofollow noreferrer">https://gist.github.com/rnorth/2031652</a> and <a href="https://gist.github.com/NickSto/6920790" rel="nofollow noreferrer">https://gist.github.com/NickSto/6920790</a>. But no use.</p>
  
  <p>So now I just tried to read the cookie value and return it using <a href="http://wiki.nginx.org/HttpEchoModule" rel="nofollow noreferrer">echo</a>. Response does not contain value for my cookie.</p>
  
  <p>Here is nginx config that I have did using echo</p>
  
  <pre><code>server {
      listen       3001;
      server_name  &lt;my_host&gt;;
  
      # Load configuration files for the default server block.
      # include /etc/nginx/default.d/*.conf;
  
      # set $auth_cookie "check";
      if ($http_cookie ~* "AUTH_COOKIE=([a-z0-9]+)(?:;|$)") {
          set $auth_cookie $1;
      }
      location / {
          resolver 8.8.8.8;
          echo "This is a test from $host with $auth_cookie. Cookie $cookie_AUTH_COOKIE. Referer $http_referer";
      }
  }
  </code></pre>
  
  <p>When I invoke this via <a href="https://chrome.google.com/webstore/detail/advanced-rest-client/hgmloofddffdnphfgcellkdfbfbjeloo" rel="nofollow noreferrer">advanced rest client</a> , I get the response as </p>
  
  <p><code>This is a test from localhost with . Cookie . Referer</code></p>
  
  <p>You can see that $auth_cookie, $cookie_AUTH_COOKIE and $http_referer are not returned.</p>
  
  <p>Here is the screen shots my rest client
  <a href="https://i.stack.imgur.com/NGn7d.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/NGn7d.png" alt="Request"></a>
  <a href="https://i.stack.imgur.com/KaidV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KaidV.png" alt="Respnse"></a></p>
  
  <p>What am I missing here. Do I need to import any module to read cookie? </p>