# URGENT: cookies don't work with nginx (proxying to apache!)

**URL:** <https://www.ruby-forum.com/t/urgent-cookies-dont-work-with-nginx-proxying-to-apache/207892>\
**Category:** NGINX\
**Created:** [June 5, 2011, 6:19pm UTC](https://www.ruby-forum.com/t/urgent-cookies-dont-work-with-nginx-proxying-to-apache/207892 "2011-06-05T18:19:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [June 5, 2011, 6:19pm UTC](https://www.ruby-forum.com/t/urgent-cookies-dont-work-with-nginx-proxying-to-apache/207892/1 "2011-06-05T18:19:44Z")

</div>

Hi. nginx is proxying to apache. Everything works.

Except cookies. My php pages seem just not to be able to set cookies!

Is there any specific directive that I should watch out for?

Could proxy\_cache stuff be interfering with it maybe?

My nginx.conf file is here:

> **[user nobody; worker\_processes 4; worker\_rlimit\_nofile 40960; pid...](http://controlc.com/18331)**
>
> user nobody; worker\_processes 4; worker\_rlimit\_nofile 40960; pid /var/run/nginx.pid; - 18331

My proxy.inc file is here:

> **[proxy.inc file - a499c](http://controlc.com/a499c)**
>
> proxy\_connect\_timeout 150s; proxy\_send\_timeout 150s; proxy\_read\_timeout - a499c

Silly question: I am passing the header “Set Cookie”. Is there a header  
for “Get Cookie”?

Posted at Nginx Forum:

> **[URGENT: cookies don't work with nginx (proxying to apache!)](https://forum.nginx.org/read.php?2,204130,204130#msg-204130)**
>
> Hi. nginx is proxying to apache. Everything works. Except cookies. My php pages seem just not to be able to set cookies! Is there any specific directive that I should watch out for? Could proxy\_cache stuff be interfering with it maybe?

---

<div class="post-metadata">

**Author:** ![pk899](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c68b51/32.png) [@pk899](https://www.ruby-forum.com/u/pk899)\
**Post date:** [June 5, 2011, 7:05pm UTC](https://www.ruby-forum.com/t/urgent-cookies-dont-work-with-nginx-proxying-to-apache/207892/2 "2011-06-05T19:05:31Z")

</div>

## pk899 Wrote:

> maybe?
> 
> My nginx.conf file is here:  
> [http://tinypaste.com/18331](http://tinypaste.com/18331)
> 
> My proxy.inc file is here:  
> [http://tinypaste.com/a499c](http://tinypaste.com/a499c)
> 
> Silly question: I am passing the header “Set  
> Cookie”. Is there a header for “Get Cookie”?

I ran some tests, and it seems one of the issues is that if cookies are  
being set in a page, then the "header(location: xyz)’ command does not  
work.

In apache, we got over this by setting output\_buffering = on.

What’s the equivalent on nginx?

Thx

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,204130,204140#msg-204140](http://forum.nginx.org/read.php?2,204130,204140#msg-204140)

---

<div class="post-metadata">

**Author:** ![pk899](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c68b51/32.png) [@pk899](https://www.ruby-forum.com/u/pk899)\
**Post date:** [June 5, 2011, 7:32pm UTC](https://www.ruby-forum.com/t/urgent-cookies-dont-work-with-nginx-proxying-to-apache/207892/3 "2011-06-05T19:32:06Z")

</div>

On Sun, 2011-06-05 at 13:04 -0400, pk899 wrote:

> I ran some tests, and it seems one of the issues is that if cookies are  
> being set in a page, then the "header(location: xyz)’ command does not  
> work.
> 
> In apache, we got over this by setting output\_buffering = on.
> 
> What’s the equivalent on nginx?

That’s a PHP setting:

> **[PHP: Runtime Configuration - Manual](https://www.php.net/manual/en/outcontrol.configuration.php)**
>
> Runtime Configuration

Cliff

---

<div class="post-metadata">

**Author:** ![pk899](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c68b51/32.png) [@pk899](https://www.ruby-forum.com/u/pk899)\
**Post date:** [June 5, 2011, 8:20pm UTC](https://www.ruby-forum.com/t/urgent-cookies-dont-work-with-nginx-proxying-to-apache/207892/4 "2011-06-05T20:20:15Z")

</div>

Cliff, thanks for this, but wouldn’t that redirect the page before  
setting the cookie?

Posted at Nginx Forum:

> **[Re: URGENT: cookies don't work with nginx (proxying to apache!)](https://forum.nginx.org/read.php?2,204130,204158#msg-204158)**
>
> Hi. nginx is proxying to apache. Everything works. Except cookies. My php pages seem just not to be able to set cookies! Is there any specific directive that I should watch out for? Could proxy\_cache stuff be interfering with it maybe?

---

<div class="post-metadata">

**Author:** ![pk899](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c68b51/32.png) [@pk899](https://www.ruby-forum.com/u/pk899)\
**Post date:** [June 5, 2011, 8:00pm UTC](https://www.ruby-forum.com/t/urgent-cookies-dont-work-with-nginx-proxying-to-apache/207892/5 "2011-06-05T20:00:29Z")

</div>

On Sun, 2011-06-05 at 13:04 -0400, pk899 wrote:

> I ran some tests, and it seems one of the issues is that if cookies are  
> being set in a page, then the "header(location: xyz)’ command does not  
> work.

Try setting the location header before setting the cookie:

header(‘Location: …’);  
setcookie(‘mycookie’, …);

Cliff

---

<div class="post-metadata">

**Author:** ![pk899](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c68b51/32.png) [@pk899](https://www.ruby-forum.com/u/pk899)\
**Post date:** [June 5, 2011, 9:28pm UTC](https://www.ruby-forum.com/t/urgent-cookies-dont-work-with-nginx-proxying-to-apache/207892/6 "2011-06-05T21:28:13Z")

</div>

On Sun, 2011-06-05 at 14:19 -0400, pk899 wrote:

> Cliff, thanks for this, but wouldn’t that redirect the page before  
> setting the cookie?

No, it just sets a header. I don’t believe header order should matter  
in any case, so I expect this is some PHP issue with outputting headers.

Cliff
