try_files & cookie based caching
July 21, 2009 11:00PM
First, I have a working configuration that uses an ugly kluge in order to work. What I am trying to do is use fastcgi_cache unless the user has a specific login in cookie. This uses WPMU but that isn't important. First, the configuration that DOES WORK (partial, all other config options are the same between working and non-working):

location / {

try_files $uri $uri/ /index.php?u=$uri&$args;

}

location ~ \.php$ {
try_files /stop @nocache; # there is no /stop - included due to try_files requirement

fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
include /etc/nginx/fastcgi_params;

fastcgi_cache default;
fastcgi_cache_key $host$uri$args;
fastcgi_cache_valid 200 302 1h;
fastcgi_cache_valid 301 30d;
fastcgi_cache_valid 304 30d;
fastcgi_cache_valid any 1m;
fastcgi_cache_min_uses 1;
fastcgi_cache_use_stale error timeout invalid_header http_500;
fastcgi_ignore_headers Cache-Control Expires;

if ($http_cookie !~* "wordpress_logged_in_") {
fastcgi_pass unix:/tmp/php-fpm.sock;
}

}

location @nocache {

fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
include /etc/nginx/fastcgi_params;

if ($http_cookie ~* "wordpress_logged_in_") {
fastcgi_pass unix:/tmp/php-fpm.sock;
}

}

That WORKS but note the nasty little '/stop' in the try_files? There is no such file/directory. It is there simply because try_files must have at least two arguments. So this will send the visitor to the backend if they are logged in or serve check fastcgi_cache if not.

So why can't I just make a few minor changes and get the same result. Perhaps change the try_files to something like try_files @cache @nocache? If I do, people who are NOT logged in see blank pages. If I turn that around, people who are logged in see blank pages (the opposite group sees what they are supposed to in each case).

So, the NON-WORKING VARIATION looks something like this:

location / {

try_files $uri $uri/ /index.php?u=$uri&$args;

}

location ~ \.php$ {

try_files @cache @nocache;

}

location @cache {

fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
include /etc/nginx/fastcgi_params;

fastcgi_cache default;
fastcgi_cache_key $host$uri$args;
fastcgi_cache_valid 200 302 1h;
fastcgi_cache_valid 301 30d;
fastcgi_cache_valid 304 30d;
fastcgi_cache_valid any 1m;
fastcgi_cache_min_uses 1;
fastcgi_cache_use_stale error timeout invalid_header http_500;
fastcgi_ignore_headers Cache-Control Expires;

if ($http_cookie !~* "wordpress_logged_in_") {
fastcgi_pass unix:/tmp/php-fpm.sock;
}

}

location @nocache {

fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/tmp/php-fpm.sock;

}

Now that seem cleaner and more efficient except that it doesn't work. Blank pages to users who are NOT logged in, normal pages for those who are.

Anyone have any idea why the second configuration is failing?

As you can imagine, this would be a boon to WPMU users who could use the built-in caching of NGINX when they are guests but getting the benefits of fresh pages (and no exploitation) for logged in users.
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 286
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready