# How to add cookie when proxy\_pass via programming?

**URL:** <https://www.ruby-forum.com/t/how-to-add-cookie-when-proxy-pass-via-programming/207509>\
**Category:** NGINX\
**Created:** [May 27, 2011, 6:50am UTC](https://www.ruby-forum.com/t/how-to-add-cookie-when-proxy-pass-via-programming/207509 "2011-05-27T06:50:07Z")\
**Posts on this page:** 4\
**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:** [May 27, 2011, 6:50am UTC](https://www.ruby-forum.com/t/how-to-add-cookie-when-proxy-pass-via-programming/207509/1 "2011-05-27T06:50:07Z")

</div>

Hey, I want to add custom cookie when proxy pass to the upstream in my  
custom module. I want to implement like what this config does:

proxy\_pass [http://servers](http://servers);  
proxy\_add\_header Cookie “$http\_cookie;KEY=VALUE”;

But sinc the VALUE is dynamically generated so I need to add this cookie  
via programming.

How to do that?

Thanks very much.

Posted at Nginx Forum:

> **[How to add cookie when proxy\_pass via programming?](https://forum.nginx.org/read.php?2,201413,201413#msg-201413)**
>
> Hey, I want to add custom cookie when proxy pass to the upstream in my custom module. I want to implement like what this config does: proxy\_pass http://servers; proxy\_add\_header Cookie "$http\_cookie;KEY=VALUE"; But sinc the VALUE is...

---

<div class="post-metadata">

**Author:** ![speedfirst](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e19adc/32.png) [@speedfirst](https://www.ruby-forum.com/u/speedfirst)\
**Post date:** [May 27, 2011, 10:01am UTC](https://www.ruby-forum.com/t/how-to-add-cookie-when-proxy-pass-via-programming/207509/2 "2011-05-27T10:01:34Z")

</div>

On Fri, May 27, 2011 at 12:49:30AM -0400, speedfirst wrote:

> Hey, I want to add custom cookie when proxy pass to the upstream in my  
> custom module. I want to implement like what this config does:
> 
> proxy\_pass [http://servers](http://servers);  
> proxy\_add\_header Cookie “$http\_cookie;KEY=VALUE”;
> 
> But sinc the VALUE is dynamically generated so I need to add this cookie  
> via programming.
> 
> How to do that?

Create a module that defines a veriable and use it in  
proxy\_set\_header Cookie “$http\_cookie; key=$var”;

–  
Igor S.

---

<div class="post-metadata">

**Author:** ![speedfirst](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e19adc/32.png) [@speedfirst](https://www.ruby-forum.com/u/speedfirst)\
**Post date:** [May 27, 2011, 11:32am UTC](https://www.ruby-forum.com/t/how-to-add-cookie-when-proxy-pass-via-programming/207509/3 "2011-05-27T11:32:34Z")

</div>

Thanks. And I want to know the time when “proxy\_set\_header” is executed,  
because the my cookie value is related to the chosen upstream server. So  
will proxy\_set\_header happen before upstream peer initialization or  
after that?

Posted at Nginx Forum:

> **[Re: How to add cookie when proxy\_pass via programming?](https://forum.nginx.org/read.php?2,201413,201474#msg-201474)**
>
> Hey, I want to add custom cookie when proxy pass to the upstream in my custom module. I want to implement like what this config does: proxy\_pass http://servers; proxy\_add\_header Cookie "$http\_cookie;KEY=VALUE"; But sinc the VALUE is...

---

<div class="post-metadata">

**Author:** ![speedfirst](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e19adc/32.png) [@speedfirst](https://www.ruby-forum.com/u/speedfirst)\
**Post date:** [May 27, 2011, 11:44am UTC](https://www.ruby-forum.com/t/how-to-add-cookie-when-proxy-pass-via-programming/207509/4 "2011-05-27T11:44:57Z")

</div>

On Fri, May 27, 2011 at 05:32:11AM -0400, speedfirst wrote:

> Thanks. And I want to know the time when “proxy\_set\_header” is executed,  
> because the my cookie value is related to the chosen upstream server. So  
> will proxy\_set\_header happen before upstream peer initialization or  
> after that?

Before, and this value is used for all upstreams.

–  
Igor S.
