Welcome! Log In Create A New Profile

Advanced

Re: Can I cache a page conditionally based on cookie value?

Maxim Dounin
May 15, 2013 05:20AM
Hello!

On Tue, May 14, 2013 at 09:23:08PM -0400, patng323 wrote:

> Hi,
>
> Using NginX as a reverse proxy, I want to cache the response ONLY if the
> user has NOT logged in, which can be checked by testing the existence of a
> cookie.
>
> First I tried to use "if", but NginX complained that I cannot put a
> proxy_cache_valid inside an "if":
>
> location / {
> proxy_pass http://www.xyz.com;
> if ($cookie_login = '') {
> proxy_cache my-cache;
> proxy_cache_valid 200 301 302 60m;
> }
> }
>
>
> Then I tried to set the cache-duration to 0s by using a variable, but then
> NginX complained the time value is incorrect.
> set $cacheDuration 60m;
> if ($cookie_login != '') {
> set $cacheDuration 0s;
> }
>
> proxy_cache my-cache;
> proxy_cache_valid 200 301 302 $cacheDuration
>
>
> So is there any way to cache conditionally based on the existence of a
> cookie?

Something like

proxy_no_cache $cookie_login;

should do the trick. See http://nginx.org/r/proxy_no_cache for
details.

--
Maxim Dounin
http://nginx.org/en/donation.html

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

Can I cache a page conditionally based on cookie value?

patng323 May 14, 2013 09:23PM

Re: Can I cache a page conditionally based on cookie value?

Maxim Dounin May 15, 2013 05:20AM

Re: Can I cache a page conditionally based on cookie value?

patng323 May 15, 2013 06:15AM



Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 314
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