Welcome! Log In Create A New Profile

Advanced

memcache + cookie check + fastcgi_pass

Posted by natebeaty 
memcache + cookie check + fastcgi_pass
November 20, 2011 07:03PM
I have a feeling this is simple, but I'm having no luck figuring it out on my own.

I am storing full pages in memcache on the PHP side, and then checking for the memcache page in nginx and serving that up if found. Additionally, I'd like to skip the memcache check if the request is not GET and/or the user has an authentication cookie set. Any help is much appreciated. My (simplified) config:

server {
listen 80;
server foo.com;
.. various configs ..

location / {
rewrite ^/user/?$ /user.php last;
.. many more rewrites ..
}

location ~ \.php$ {
if ($request_method != GET) {
# what to put here to send to @cache_miss?
break;
}
if ($http_cookie ~* "cookie_authenticated" ) {
# what to put here to send to @cache_miss?
break;
}
set $memcached_key "foo:$request_uri";
memcached_pass 127.0.0.1:11211;
default_type text/html;
error_page 404 405 502 = @cache_miss;
}

location @cache_miss {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;
}
}

I've tried all sorts of variations with proxy_pass to upstreams, fastcgi_pass, etc. I'd like to just pass it to the named directive, but I'm not sure how to do that. (Or what the proper alternative would be.)

Thanks in advance.
Re: memcache + cookie check + fastcgi_pass
November 21, 2011 01:19PM
Seems I have it working with the setup below. Is this the most elegant way to do this?

location ~ \.php$ {
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;

if ($args ~ flush=y) {
fastcgi_pass 127.0.0.1:9000;
break;
}

if ($request_method != GET) {
fastcgi_pass 127.0.0.1:9000;
break;
}

if ($http_cookie ~* "cookie_authenticated" ) {
fastcgi_pass 127.0.0.1:9000;
break;
}

set $memcached_key "foo:$request_uri";
memcached_pass 127.0.0.1:11211;

default_type text/html;
error_page 404 405 502 = @cache_miss;
}

location @cache_miss {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;
}
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 101
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 500 on July 15, 2024
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready