Nginx fastcgi_cache hide Set-Cookie when serving from cache

cachefastcginginx

I'm trying to use nginx fastcgi_cache to serve some pages from my site, it worked fine but I found that sessions were being duplicated to all the users because Set-Cookie was cached in the response.

I've tried several solutions, but I don't want to disable cookies in those pages, only to ignore them when serving from cache. Is there any way to do this? I've considered moving to varnish but I have several sites in the same server and I'd like to avoid it.

I've also tried this, but no success

fastcgi_cache_path /etc/nginx/cache/iteramos levels=1:2 keys_zone=ITERAMOS:120m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";

set $supercache_uri $request_uri;
set $no_cache 1;

#set no cache to 0 (do cache) if we are in listing pages
if ($supercache_uri ~ ^/(preguntas|etiquetada|etiquetas)$) {
    set $no_cache 0;
    set $supercache_uri '';
}
if ($supercache_uri = /) {
    set $no_cache 0;
    set $supercache_uri '';
}

if ($supercache_uri ~ ^/?page= ) {
    set $no_cache 0;
    set $supercache_uri '';
}

location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_ignore_headers "Cache-Control" "Expires" "Set-Cookie";
    fastcgi_cache ITERAMOS;
    fastcgi_cache_valid 200 60m;
    #this header adds a hit / bypass / miss header
    fastcgi_cache_use_stale error timeout;
    add_header X-Cache $upstream_cache_status;
    fastcgi_cache_bypass $no_cache;
    fastcgi_no_cache $no_cache;
}

Thanks in advance

Best Answer

Try to use fastcgi_hide_header:

fastcgi_hide_header "Set-Cookie";

This will hide all cookies when the request hits cache. official docs