Question

I've implemented FastCGI caching on our site, and have seen great speed improvements. However the FastCGI cache key does not seem to be unique enough. If I login, my name appears in the header. However the next person to login still sees my name in the header, assuming the cache is still valid.

Is there a way to make the cache key unique on a per-user basis? Ideally using a unique identifier from the user's Cookies or a PHP Session? I tried implemented the answer below, but Nginx failed to restart.

Log in value from Set-Cookie header in nginx

Note my cache key looks like this:

fastcgi_cache_key "$scheme$request_method$host$request_uri";

Update: My thought is if I can parse the HTTP headers sent to Nginx, then I can grab the PHP SESSION ID and use that. However I cannot find an example of how to do this anywhere. Right now I have something like this, which doesn't work.

http_cookie ~* PHPSESSID=([0-9a-z]+) {
    set $ses_id $1;
}
Was it helpful?

Solution

I was able to solve the above problem using the Nginx ngx_http_userid_module. The hardest part was actually finding the module, implementing the solution was quite trivial.

I used their example configuration:

userid         on;
userid_name    uid;
userid_domain  example.com;
userid_path    /;
userid_expires 365d;
userid_p3p     'policyref="/w3c/p3p.xml", CP="CUR ADM OUR NOR STA NID"';

And then added the userid to my fastCGI cache key:

fastcgi_cache_key "$scheme$request_method$host$request_uri$uid_got";

Hopefully this answer helps someone discover this useful module quicker than I did.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top