Hi everyone,
I’m looking into setting up nginx as a proxy cache. I was wondering if
someone can elaborate a bit about ways to bypass the cache
in certain requests. For example, I’d like every new user to go to the
backend server once in order for some cookies to be written.
On further requests I’d like to serve the user with a cached copy of the
site.
How can I set a mechanism that bypasses the cache if a cookie with a
certain name does/does not exist?
I’ve dug a bit deeper into the documentation and it seems like
proxy_no_cache and proxy_cache_bypass should somehow do the trick, but I
can get things to work with my exact scenario.
Here’s the flow I’d like to create:
when a new user reaches the site, it is forwarded to the backend and
receives a few cookies back.
A “new user” is identified by a lack of a certain cookie, which
is something I couldn’t figure out how to implement (seems like it only
supports bypassing/no caching if a cookie exists, but not if it
doesn’t).
This request should not be taken from the cache, but the response can be
stored in cache as long as further requests will not be sent the cookies
this request produced.
Returning visitors can be served from the cache, unless they are
logged in (in which case they should be sent to the backend and the
response should not be stored). This is simple enough to do with the
regular bypass/no_cache combination based on a cookie that is created
when a user logs in.
It doesn’t seem like a complicated scenario, but I can’t get it to work
just right.
Any help would be appreciated
Just came back to this as I have hit this wall and it would be very
useful if the roxy no cache and proxy bypass could be triggered based on
the absence of a cookie.
I need to pass first time visitors to the backend and I need to make
sure the content they get is not cached. As they are first time
visitors, I can’t rely on the presence of a cookie to tell this and the
only way I can think off is to check for the absence of a cookie.
I think I am getting closer and hope someone can nudge me across the
line.
My set up is now …
if ($http_cookie !~* "mycookie=0") {
add_header Cookie: "mycookie=1";
}
The idea being that if mycookie is not 0 (which should cover it not
being set), it is set to 1. I then added cookie_MYCOOKIE to the bypass
and no cache parameters so that if it is set to 1, they are triggered.
The backend sets mycookie to 0 if it is 1 so that on subsequent
requests, the resource is served from the cache.
However unfortunately, the “add_header Cookie: “mycookie=1”;” line does
not seem to be called at all.
I throw my hands up in surrender as I just can’t get it to return
the cookie.
I have tried a plain vanilla build with no joy.
v0.8.54
The relevant location code is …
Like Marcus said before. You’re likely to have some implicit location
(if) that it isn’t working.
location /folder/ {
if ($http_cookie !~* “COOKIEX=0”) {
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
have you checked using the debug flag in the error log
that this if (implicit location) is entered?
Read this explanation by Maxim in another thread:
add_header Cookie "COOKIEX=1;
domain=domain.com; path=/folder/";
}
# BEGIN Url Rewrites