Nginx fastcgi_cache exclude session cookie

fastcginginx

Need help finding the correct setup for using Nginx fastcgi_cache without caching the PHP session cookie. Here is what I currently have below. Testing this however if I go to a page that has been cached, delete my PHPSESSION cookie in the browser, and refresh, I get a PHPSESSION id from one of the cached Nginx files.

if ($http_cookie = "PHPSESSION")
{
   set $fastcgi_skipcache 1;
}
location ~* \.php {
    include fastcgi_params;
    fastcgi_pass backend;
    fastcgi_index index.php;
    fastcgi_param SERVER_NAME $host;
    fastcgi_param SCRIPT_URL $fastcgi_script_name;
    fastcgi_param SCRIPT_URI $scheme://$http_host$fastcgi_script_name;
    fastcgi_param SCRIPT_NAME "/index.php";
    fastcgi_param PHP_SELF $uri;
    fastcgi_param HTTPS $https if_not_empty;
    fastcgi_param HTTP_FRONT_END_HTTPS HTTPS;
    fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    fastcgi_param REQUEST_URI $uri?$args;
    fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
    fastcgi_intercept_errors on;
    fastcgi_ignore_client_abort off;
    fastcgi_connect_timeout 90;
    fastcgi_send_timeout 180;
    fastcgi_read_timeout 180;
    fastcgi_buffer_size 128k;
    fastcgi_buffers 4 256k;
    fastcgi_busy_buffers_size 256k;
    fastcgi_temp_file_write_size 256k;

    #Caching
    fastcgi_cache ee;
    fastcgi_cache_key "$scheme$host$request_uri";
    fastcgi_cache_valid 200 302 168h;
    fastcgi_cache_valid 404 1m;
    fastcgi_cache_bypass $fastcgi_skipcache;
    fastcgi_no_cache $fastcgi_skipcache;

}

We used to have this working in Nginx with proxy_cache as below, but are moving from Nginx/Apache/Php setup to Nginx/PHP-FPM setup.

    proxy_hide_header Set-Cookie;
    proxy_ignore_headers Expires Cache-Control Set-Cookie;
    proxy_set_header Cookie "";

    proxy_cache ee;
    proxy_cache_key         "$scheme$host$request_uri";
    proxy_cache_valid 200 302 60m;
    proxy_cache_valid 404 1m;

This link seems to be questioning the same thing, but if I use fastcgi_hide_header "Set-Cookie"; we are having problems in our cart. I'm assuming because it cannot see the session.

Let me know if more details are required.
Thanks,

Chris.

Best Answer

I think you're overcomplicating things. Try this:

fastcgi_no_cache $cookie_PHPSESSID;
fastcgi_cache_bypass $cookie_PHPSESSID;

By the way proxy_* directives are for a case when nginx is acting like a proxy, for example in a load-nalancing scheme. I doubt there can be a configuration when nginx can be both fastcgi-backend and a proxy for one location.