New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cookies #73
Comments
Doing something like this is... complicated. To start with, the current behaviour of passing a visitor's initial request to the backend is going away in the next release. Instead Varnish will generate the frontend cookie token and serve the response from cache without talking to the backend at all, so this:
won't work in the (very) near future if it relies on that initial pass through.
While it wouldn't be trivial, you could probably use the one of the GeoIP vmods:
and manually setting the Cookie header in |
Thanks for the insightful response. I had no idea that varnish could have plugins! I will have to look into this. Can you please give an example use of |
Sort of, yes. You will need to define the functions but Varnish will append them all together so you don't need to directly copy them, just do the processing that you need to do and don't do a
Then the later |
Hi, I have identified what I need to acheive: A) If mystore cookie does not exist: #1 create cookie for current domain (if not .com) #2 redirect to .com (if not already there) #3 create cookie on .com B) If mystore cookie exists, and url indicates the same store as in the cookie value, then load store C) If mystore cookie exists, and url indicates a different store than is in the cookie value, then redirect to correct store. D) The other thing that i can see needs to happen, is the cookie needs to be passed to the backend for any backend requests. So far, I have achieved B and C. I'm stuck with the rest. Are you able to provide paid support to implement this for us? If so what kind of time frame and costs. Thanks in advance. |
D should happen automatically once the client has the cookie (sent the Set-Cookie header with the cookie), with 0.5.x at least, not sure about lower versions. For A, you would probably want to do something like this: sub set_store_cookie {
# set a flag so we know to send the Set-Cookie header in the response
set req.http.X-MyStoreCookie-Flag = "1";
if (req.http.Cookie) {
set req.http.Cookie = req.http.Cookie + "; mystore=" + req.http.Host;
} else {
set req.http.Cookie = "mystore=" + req.http.Host;
}
}
sub vcl_recv {
if (req.http.Cookie !~ "mystore=") {
call set_store_cookie;
if (req.http.Host !~ ".com$") {
# redirect to .com domain
}
}
}
sub vcl_deliver {
if (req.http.X-MyStoreCookie-Flag) {
set resp.http.Set-Cookie = regsub(req.http.Cookie, ".*(mystore=[^;]+).*", "\1");
}
}
Unfortunately no, I don't do custom development like that. |
Wow thanks heaps. It appears that I have got it working to our specification. I will report back on progress. |
So I thought that I had it all working, but it's still only half working. I guess the easiest thing to do is post my current config. I had to change the regex slightly provided in a previous post it apeared that it was using a ; as a cookie separator, where as I needed extra cookie values so I have changed that to a comma instead.
for reference the site (mentioned below) is hosted at 119.224.138.120 - DNS is not currently pointing there.
|
This is expected, unfortunately Varnish doesn't really handle multiple Set-Cookie headers in a response very well (and browser support for setting multiple cookies in a single Set-Cookie header is spotty). You can only really manipulate one (the last sent I believe) and doing so will remove any others. Sorry, I should have mentioned that before. Off the top of my head, I don't think it will cause any problems and the frontend cookie should get set on the request after the mystore cookie is set. Unless you meant that the frontend cookie is overwriting the mystore cookie? That makes things a little more difficult. You'll need to "trick" the frontend cookie generation logic in sub vcl_deliver {
if (req.http.X-MyStoreCookie-Flag) {
# prevent the frontend cookie set-cookie header
unset req.http.X-Varnish-Faked-Session;
set resp.http.Set-Cookie = regsub(req.http.Cookie, ".*(mystore=[^,]+).*", "\1");
}
}
If the frontend cookie generation is overwriting the mystore cookie then the above change should fix this (I think). |
Hi,
I'm working with a custom but of code in a header file to build a country switcher, that changes between the sites and remembers the decision for future visits. This works outside of varnish, but when we tried to deploy it, cookies were stripped.
I realise that cookies are stripped, however, I need to allow one through.
My setup is:
Any pointers in the right direction would be much appreciated.
The text was updated successfully, but these errors were encountered: