# Filter out headers for fastcgi cache

**URL:** <https://www.ruby-forum.com/t/filter-out-headers-for-fastcgi-cache/216650>\
**Category:** NGINX\
**Created:** [February 10, 2012, 12:39am UTC](https://www.ruby-forum.com/t/filter-out-headers-for-fastcgi-cache/216650 "2012-02-10T00:39:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [February 10, 2012, 12:39am UTC](https://www.ruby-forum.com/t/filter-out-headers-for-fastcgi-cache/216650/1 "2012-02-10T00:39:40Z")

</div>

Greetings,

I would like to propose that an additional feature for nginx be  
considered related to fastcgi caching: the ability to specify headers  
that will not be stored in cache, but will be sent to the client when  
the cached version is created (sent to the client responsible for  
creating the cached copy). If some solution already exists providing  
this functionality, my apologies, I was not able to track one down -  
currently assuming one does not exist.

Here is one scenario where such an option would be useful (I am sure  
there are others):

A typical scenario where fastcgi caching can be employed for performance  
benefits is when the default version of a page is loaded. By “default”,  
I mean the client has no prior session data which might result in unique  
session specific request elements. In the case of PHP, the presence of  
session data is typically determined by checking for the presence of a  
“PHPSESSID” cookie. So if this cookie does not exist, then it can be  
assumed there is no session - an optimal time to create a cached version  
of the page in many scenarios. However, many PHP apps/sites/etc. also  
create a session in the event one does not exist (a behavior I assume is  
not specific to PHP) - meaning the the response typically contains a  
Set-Cookie: PHPSESSID… header. Nginx’s default behavior is not to  
cache any page with a Set-Cookie header, and that makes sense as a  
default - but lets assume for this example that fastcgi\_ignore\_headers  
Set-Cookie; is in effect and the cached version of the default version  
of the page gets created. The problem here is that the cached version  
created also has the Set-Cookie header cached as well - which causes  
problems for obvious reasons (hands out the same session ID/cookie for  
all users). Ideally, we could specify to cache everything except the  
specified headers - Set-Cookie in this example.

Am I the only one who would find this option useful or are there others?

If this would be found to be useful by others and there is consensus  
that such an addition is called for by those with the resources to  
implement it, I am not sure if it makes sense to strip the headers prior  
storage in cache or when reading from cache - probably whichever is  
easier.

Thoughts?  
Michael

---

<div class="post-metadata">

**Author:** ![Michael\_McCallister](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/13edae/32.png) [@Michael\_McCallister](https://www.ruby-forum.com/u/Michael_McCallister)\
**Post date:** [February 10, 2012, 12:47am UTC](https://www.ruby-forum.com/t/filter-out-headers-for-fastcgi-cache/216650/2 "2012-02-10T00:47:06Z")

</div>

On 9 Fev 2012 23h37 WET, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Here is one scenario where such an option would be useful (I am sure  
> many scenarios. However, many PHP apps/sites/etc. also create a  
> Ideally, we could specify to cache everything except the specified
> 
> Thoughts?  
> Michael

> **[Module ngx\_http\_fastcgi\_module](http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html#fastcgi_pass_header)**
>
> The ngx\_http\_fastcgi\_module module allows passing requests to a FastCGI server.

Also, you can capture the session ID and use it in  
fastcgi\_cache\_key. Like this, for example:

## Set a cache\_uid variable for authenticated users.

map $http\_cookie $cache\_uid {  
default nil; # hommage to Lisp 🙂  
~SESS[[:alnum:]]+=(?\<session\_id\>[[:alnum:]]+) $session\_id;  
}

fastcgi\_cache\_key $cache\_uid@$host$request\_uri;

— appa

---

<div class="post-metadata">

**Author:** ![Michael\_McCallister](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/13edae/32.png) [@Michael\_McCallister](https://www.ruby-forum.com/u/Michael_McCallister)\
**Post date:** [February 10, 2012, 6:22am UTC](https://www.ruby-forum.com/t/filter-out-headers-for-fastcgi-cache/216650/3 "2012-02-10T06:22:43Z")

</div>

Antnio P. P. Almeida wrote, On 02/09/2012 04:45 PM:

> > track one down - currently assuming one does not exist.  
> > if this cookie does not exist, then it can be assumed there is no  
> > Set-Cookie header cached as well - which causes problems for obvious  
> > prior storage in cache or when reading from cache - probably  
> > map $http\_cookie $cache\_uid {  
> > default nil; # hommage to Lisp 🙂  
> > ~SESS[[:alnum:]]+=(?\<session\_id\>[[:alnum:]]+) $session\_id;  
> > }
> 
> fastcgi\_cache\_key $cache\_uid@$host$request\_uri;
> 
> — appa

How does fastcgi\_pass\_header help in this instance? From the docs, that  
directive “explicitly allows to pass named headers to the client.” In  
the example, I need to drop the Set-Cookie header from the cached copy.

As far as setting up cache files for each session, that is less useful  
for me (but a cool feature). The main benefit in caching for me is for  
users that are not logged in (or doing anything that modifies site  
content) since they represent around 75% of page loads and there is no  
user specific variance in the pages themselves. I would suspect many  
other sites follow a similar usage pattern.

---

<div class="post-metadata">

**Author:** ![Michael\_McCallister](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/13edae/32.png) [@Michael\_McCallister](https://www.ruby-forum.com/u/Michael_McCallister)\
**Post date:** [February 14, 2012, 7:48pm UTC](https://www.ruby-forum.com/t/filter-out-headers-for-fastcgi-cache/216650/4 "2012-02-14T19:48:45Z")

</div>

Max wrote, On 02/10/2012 03:54 PM:

> # when passing cached content back to clients
> 
> proxy\_hide\_header Set-Cookie;
> 
> You could also include session cookies in the fastcgi\_cache\_key  
> to make sure new users get the default cached content, while  
> everyone else gets their session-specific cached content:
> 
> fastcgi\_cache\_key  
> “$cookie\_PHPSESSID$scheme$request\_method$host$server\_port$request\_uri”;
> 
> Max

Thanks Max!

---

<div class="post-metadata">

**Author:** ![Michael\_McCallister](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/13edae/32.png) [@Michael\_McCallister](https://www.ruby-forum.com/u/Michael_McCallister)\
**Post date:** [February 15, 2012, 1:57am UTC](https://www.ruby-forum.com/t/filter-out-headers-for-fastcgi-cache/216650/5 "2012-02-15T01:57:21Z")

</div>

14 февраля 2012, 22:48 от Michael McCallister  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > # Strip the Set-Cookie header from cached content
> > 
> > Max
> 
> Thanks Max!

You’re welcome! Just run s/proxy\_/fastcgi\_/ on the above to get  
the fastcgi directives. Those directives are the same in all of  
the (fastcgi|proxy|scgi|uwsgi) modules:

(fastcgi|proxy|scgi|uwsgi)\_ignore\_headers

> **[GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki...](https://github.com/nginxinc/nginx-wiki)**
>
> ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com

(fastcgi|proxy|scgi|uwsgi)\_hide\_header

> **[GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki...](https://github.com/nginxinc/nginx-wiki)**
>
> ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com

Max

---

<div class="post-metadata">

**Author:** ![Michael\_McCallister](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/13edae/32.png) [@Michael\_McCallister](https://www.ruby-forum.com/u/Michael_McCallister)\
**Post date:** [February 10, 2012, 11:55pm UTC](https://www.ruby-forum.com/t/filter-out-headers-for-fastcgi-cache/216650/6 "2012-02-10T23:55:22Z")

</div>

10 февраля 2012, 03:39 от Michael McCallister  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Here is one scenario where such an option would be useful (I am sure  
> create a session in the event one does not exist (a behavior I assume is
> 
> Am I the only one who would find this option useful or are there others?
> 
> If this would be found to be useful by others and there is consensus  
> that such an addition is called for by those with the resources to  
> implement it, I am not sure if it makes sense to strip the headers prior  
> storage in cache or when reading from cache - probably whichever is easier.
> 
> Thoughts?

> **[GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki...](https://github.com/nginxinc/nginx-wiki)**
>
> ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com

The proxy\_hide\_header directive does exactly what you described,  
the cookies get stored in the cache, but they are not passed back  
to the client, so this is all you’d need:

# Cache responses containing the Set-Cookie header as well

fastcgi\_ignore\_headers Set-Cookie;

# Strip the Set-Cookie header from cached content

# when passing cached content back to clients

proxy\_hide\_header Set-Cookie;

You could also include session cookies in the fastcgi\_cache\_key  
to make sure new users get the default cached content, while  
everyone else gets their session-specific cached content:

fastcgi\_cache\_key  
“$cookie\_PHPSESSID$scheme$request\_method$host$server\_port$request\_uri”;

Max
