setting the cookie header with the value of anouther header in an nginx reverse proxy

Yaur asked:

We are working with a piece of hardware(chromecast) that seems to be stripping out cookies, but will happily pass along a custom header (which we signal is acceptable from the back end) one workaround we came up to stuff the cookie data into said custom header and put it back into the cookie header on the proxy. This allows most clients (which respect normal CORS signaling) and the backend to remain unchanged. In my Nginx config I currently have a map defined like:

map cookie $cookie {
    default   $http_cookie;
    ""        $http_my_token;
}

which I’m later using as follows:

server {
  listen 443 ssl;
  #...
  location /myurl {
      proxy_pass http://nodeserver;
      proxy_set_header X-Forwarded-For $remote_addr;
      #...
      proxy_set_header Cookie $cookie;
 }
}

I expect this code to replace the Cookie header with the value supplied in the "My-Token" header, but what I see instead is that the value of both headers is passed through unchanged.

My answer:


Your map specifies that the value you want to check is the literal string cookie. This is not what you said you want, and it’s pretty useless to try to check a literal string since it can only be itself.

The first parameter to map is the value you want to test. Instead of the string cookie you should pass what you actually want to check. It seems you are looking for whether the user agent is sending a cookie. If that’s the case, you should use $http_cookie.

map $http_cookie $cookie {

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.