Welcome! Log In Create A New Profile

Advanced

Re: conditional cache

Maxim Dounin
September 01, 2010 07:28PM
Hello!

On Wed, Sep 01, 2010 at 03:00:30PM -0400, paul2791 wrote:

> I've been trying to set up two separate proxy caches, one for logged in
> users, one for guests. The one for guests had a very long expiry as
> guest pages don't change much, User pages are very dynamic and expiry is
> very short.
>
> Using cookies i can tell if a page request is by a user or guest, but I
> cannot see any easy way to alter the proxy as all the proxy_cache
> commands can't be put inside an if.
>
> I've managed the following workaround, which works fine but it seems
> strange i have to use this method when there must be an easier and more
> straightforward way that i am just not seeing
>
> set $memcached_key "pages:$uri:$cache_key"; ### dummy key, will never
> exist as i'm not using memcache for nginx
> if ($cache_key = "guest"){error_page 404 = @guests;} #set 404 for quest
> if ($cache_key != "guest"){error_page 404 = @users;} #set 404 for users
> memcached_pass 127.0.01:11220; ## this server is empty, has no keys will
> ALWAYS return 404

It's good idea to avoid using anything in "if" except "return" and
"rewrite ... last". See http://wiki.nginx.org/IfIsEvil.

Also it's probably better use 418 status code here ;). I would
recommend something like this:

location / {
error_page 418 = @guests;

if (...detect guests...) {
return 418;
}

proxy_cache users;
...
}


location @guests {
proxy_cache guests;
...
}

> location @users {
> proxy_cache users;
> proxy_cache_key "$request_uri$cache_key";
> proxy_cache_valid 502 504 1m;
> proxy_cache_valid any 5m;
> proxy_pass http://127.0.0.1:10098;
> }
>
> location @guests {
> proxy_cache guests;
> proxy_cache_key "$request_uri$cache_key";
> proxy_cache_valid 502 504 1m;
> proxy_cache_valid any 60m;
> proxy_pass http://127.0.0.1:10098;
> }

Alternatively you may want to just set correct Cache-Control /
Expires / X-Accel-Expires headers in backend's response and avoid
explicitly setting validity via proxy_cache_valid.

Maxim Dounin

_______________________________________________
nginx mailing list
nginx@nginx.org
http://nginx.org/mailman/listinfo/nginx
Subject Author Posted

conditional cache

paul2791 September 01, 2010 03:00PM

Re: conditional cache

Maxim Dounin September 01, 2010 07:28PM

Re: conditional cache

paul2791 September 02, 2010 11:02AM

Re: conditional cache

paul2791 September 02, 2010 11:15AM

Re: conditional cache

Maxim Dounin September 02, 2010 11:56AM

Re: conditional cache

Luca De Marinis September 03, 2010 06:34AM



Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 77
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready