# Hide a request cookie in proxy\_pass

**URL:** <https://www.ruby-forum.com/t/hide-a-request-cookie-in-proxy-pass/231772>\
**Category:** NGINX\
**Created:** [August 29, 2014, 5:55pm UTC](https://www.ruby-forum.com/t/hide-a-request-cookie-in-proxy-pass/231772 "2014-08-29T17:55:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![addis\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c4cdca/32.png) [@addis\_a](https://www.ruby-forum.com/u/addis_a)\
**Post date:** [August 29, 2014, 5:55pm UTC](https://www.ruby-forum.com/t/hide-a-request-cookie-in-proxy-pass/231772/1 "2014-08-29T17:55:41Z")

</div>

Hi,

is it possible to hide one request cookie (but not all, so  
proxy\_set\_header  
Cookie “” is not the way) when proxying to an upstream server?

The use case is:

- website [foo.com](http://foo.com) uses a hosted service on a subdomain, e.g.  
[blog.foo.com](http://blog.foo.com)  
hosted by [Wordpress.com](http://Wordpress.com)

- horror: MSIE will send all [foo.com](http://foo.com) cookies to the subdomain too,  
leaking  
sessions (not just to [Wordpress.com](http://Wordpress.com) but to everyone because [blog.foo.com](http://blog.foo.com)  
does not support HTTPS), and there’s no way to tell it not to

- proposed workaround: serve [blog.foo.com](http://blog.foo.com) yourself, using Nginx,  
HTTPS-only,  
proxying to the hosted service (as [foo.wordpress.com](http://foo.wordpress.com), which does support  
HTTPS), and stripping out the parent-domain request cookies

Is there a way to do this with Nginx? A way to rewrite the Cookie header  
to  
strip out selected cookies?

Or is the only way out of this to avoid the subdomain cookie situation  
altogether, either by running [www.foo.com](http://www.foo.com) instead of [foo.com](http://foo.com), or by  
abandoning the subdomain and using e.g. [foo.com/blog/](http://foo.com/blog/) instead?

Thanks,

Gulli

Posted at Nginx Forum:

> **[Hide a request cookie in proxy\_pass](https://forum.nginx.org/read.php?2,252944,252944#msg-252944)**
>
> Hi, is it possible to hide one request cookie (but not all, so proxy\_set\_header Cookie "" is not the way) when proxying to an upstream server? The use case is: \* website foo.com uses a hosted service on a subdomain, e.g. blog.foo.com hosted...

---

<div class="post-metadata">

**Author:** ![gthb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/cc9497/32.png) [@gthb](https://www.ruby-forum.com/u/gthb)\
**Post date:** [August 29, 2014, 7:27pm UTC](https://www.ruby-forum.com/t/hide-a-request-cookie-in-proxy-pass/231772/2 "2014-08-29T19:27:40Z")

</div>

Hello!

On Fri, Aug 29, 2014 at 11:55:08AM -0400, gthb wrote:

> - horror: MSIE will send all [foo.com](http://foo.com) cookies to the subdomain too, leaking  
> sessions (not just to [Wordpress.com](http://Wordpress.com) but to everyone because [blog.foo.com](http://blog.foo.com)  
> does not support HTTPS), and there’s no way to tell it not to
> 
> - proposed workaround: serve [blog.foo.com](http://blog.foo.com) yourself, using Nginx, HTTPS-only,  
> proxying to the hosted service (as [foo.wordpress.com](http://foo.wordpress.com), which does support  
> HTTPS), and stripping out the parent-domain request cookies
> 
> Is there a way to do this with Nginx? A way to rewrite the Cookie header to  
> strip out selected cookies?

With proxy\_set\_header you can change the header to any value,  
including one with a particular cookie removed. The tricky part  
is to construct new value for the original header. Something like  
this should work:

```
set $new_cookie $http_cookie;

if ($http_cookie ~ "(.*)(?:^|;)\s*secret=[^;]+(.*)") {
    set $new_cookie $1$2;
}

proxy_pset_header Cookie $new_cookie;

```

(Note that the above is completely untested.)

–  
Maxim D.

> **[nginx](http://nginx.org/)**

---

<div class="post-metadata">

**Author:** ![gthb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/cc9497/32.png) [@gthb](https://www.ruby-forum.com/u/gthb)\
**Post date:** [September 2, 2014, 12:17pm UTC](https://www.ruby-forum.com/t/hide-a-request-cookie-in-proxy-pass/231772/3 "2014-09-02T12:17:07Z")

</div>

Yep, works like a charm, thank you! And two consecutive ifs to strip two  
cookies works as well:

```
set $stripped_cookie $http_cookie;
if ($http_cookie ~ "(.*)(?:^|;)\s*sessionid=[^;]+(.*)$") {
    set $stripped_cookie $1$2;
}
if ($stripped_cookie ~ "(.*)(?:^|;)\s*csrftoken=[^;]+(.*)$") {
    set $stripped_cookie $1$2;
}

```

Cheers,

Gulli

Posted at Nginx Forum:

> **[Re: Hide a request cookie in proxy\_pass](https://forum.nginx.org/read.php?2,252944,253012#msg-253012)**
>
> Hi, is it possible to hide one request cookie (but not all, so proxy\_set\_header Cookie "" is not the way) when proxying to an upstream server? The use case is: \* website foo.com uses a hosted service on a subdomain, e.g. blog.foo.com hosted...
