How to change cookie header in a filter?

Maxim Dounin mdounin at mdounin.ru
Wed Mar 20 08:38:19 UTC 2013


Hello!

On Tue, Mar 19, 2013 at 11:50:43AM -0700, Cool wrote:

> Thanks Maxim, I got what you mean.
> 
> Since I'm using fastCGI so I put something like this:
> 
> fastcgi_param HTTP_COOKIE "$http_cookie; mycookie=$cookie_note";
> 
> (I populated cookie_note in my filter already, this was done for
> logging purpose thus it is just a reuse of existing facility)
> 
> More problems come with this solution:
> 
> 1. it seems fastcgi_param called before my filter so $cookie_note
> always got empty, and

You shouldn't rely on your filter already executed, and should 
instead register a variable handler which does the actual work.  
This way it will work at any time.

> 2. it seems fastcgi_param could not be used in a if directive so I
> end up with change the cookie header even the mycookie is presented
> in user's request, thus

If there are conditions when you should not add a cookie I would 
recommend you implementing a variable with full Cookie header you 
want to pass, e.g.

    fastcgi_param HTTP_COOKIE $my_new_cookie;

This way you may implement arbitrary conditions you want in your 
module.  (You may also construct the variable using if/set/map/etc, but 
doing appropriate tests in your module would be less error prone.)

-- 
Maxim Dounin
http://nginx.org/en/donation.html



More information about the nginx mailing list