-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
Varnish cache issue with 'Sign In' link in header #9156
Comments
Varnish keeps caches separate for clients who have different cookies for the key "X-Magento-Vary". When you aren't signed in at all, the cookie value doesn't exist. When you do sign in, a cookie value will be set based on your customer group (and one or two other factors IIRC). If your VCL file isn't configured properly, it may not be hashing on the X-Magenty-Vary. Here's what I would verify before going any further. Before signing in, examine your cookie to ensure you DO NOT find an X-Magento-Vary key. This can be done in the Chrome developer tools in the Network tab, but is beyond the scope of this response. Next, sign in and make sure you DO have the X-Magento-Vary cookie, and that it has a long hash value assigned to it. If all that is working properly, but you are still seeing improperly cached pages, examine your Varnish VCL file. This is the one you should have generated in the Magento admin section and then configured to be used by Varnish. If you skipped that step, this is DEFINITELY why you have caching problems. The important part of the VCL file is as follows:
The default varnish behavior is not acceptable to be used in Magento as there are many Magento-specific rules that need to be in place. |
Hi @AirmanAJK Thanks for your response. Our varnish vcl file was generated using admin and it does contain below code
Also, I have verified that
But when I go back to the home page by clicking website logo then links are cached and the cookie is not there. Now when I refreshed home page cookie gets created and links have been updated. Next, when I click Sign Out from home page, the customer gets logout but the home page still has 'Sign Out' link and cookie is not there. Another weird case, after flushing 'full page cache' from admin. If I try same steps as above, cookie doesn't get created after customer login as well. It seems the cookies are getting itself. It will be awesome to have your thoughts on this. |
Are you using a Redis to store the session and cache? If so, make sure you are storing them in different Redis databases. Varnish can cache pages incorrectly if a request comes in from the client with an invalid/expired session. This is because Varnish trusts the X-Magento-Vary header sent in the cookie. If the user isn't actually logged in (due to an old or flushed session), the page will still be cached by Varnish as whatever the incoming request claimed it was, even if Magento generates a different kind of page. This could be what you are experiencing. You should also make sure that your pages aren't being cached by the browser. Varnish is supposed to examine the Magento response headers for Cache-Control and unset them for the response that gets to the client/browser. If somehow those are getting to your browser, it will keep stale pages and not even reach out to the server for updates. You can see in the browser dev tools if the page was loaded from cache or server, and you can also examine the response pages to see what cache headers are actually being set. You always want page requests to be non-cached by the browser. Magento's Varnish VCL happens to use the same headers as the browser for caching, so if somehow they aren't getting unset before getting sent off to the client, this could be your issue. If clearing your local browser cache ends up loading the correct page, this is the problem. |
I have checked this in multiple browsers chrome, firefox, safari with cache cleaned but same behavior in all browsers. We do use Redis and below is configuration of it
|
'session' => ^^ Sessions in redis.. I'd recommend using varnish instead of storing page_cache in redis! it is duplicate work cut that page_cache out of the config and drop in the session portion! then use varnish 4 with a properly created varnish.vcl ! |
You said you were using Varnish for full page caching, but have Redis configured for full page caching as well? This looks like a bad setup. Also, I wasn't asking you to make sure your browser cache was cleared, I was asking you to make sure your browser is never ever letting pages into its cache to begin with. I believe your pages are being cached by the client. Make sure your response headers for page requests have a Cache-Control value of "no-store, no-cache, must-revalidate, max-age=0". This doesn't appear to be a Magento issue. It is likely a misconfiguration of Magento/Varnish/Redis. |
We are also having this issue since upgrading to v2.1.5. It was working just fine before that with this code (simplified):
Now, even when signed in the |
@thdoan I faced the same issue. Because the local storage is "added" after our script (Not sure it's a bug or not). Work Around: My idea is to check local storage until it is "added"- (we should override For example:
We also need to use |
@mrkhoa99 thanks for the suggestion. Do you have a workaround that doesn't involve overriding? I'm leaning towards two other alternatives for a quick fix:
I would prefer to use the KO method since Magento uses it themsleves in |
app/code/Vendor/Theme/etc/frontend/di.xml
app/code/Vendor/Theme/Block/Html/Header.php
app/code/Vendor/Theme/view/frontend/templates/html/header.phtml
app/code/Vendor/Theme/view/frontend/web/js/view/customer.js
app/code/Vendor/Theme/etc/module.xml
app/code/Vendor/Theme/registration.php
There are some notes:
|
@ravinders-121, thank you for your report. |
I've faced this issue after enabling varnish. Anyone solved this issue. |
Also facing same issue. Is there any additional information on this and on possible solutions ? |
@Theenluck any solution? @magento-admin any solution with varnish? |
Hello @mimarcel Thanks for your suggestion, but some case section is not loaded on logout and local storage same so i update little bit js code app/code/Vendor/Theme/etc/frontend/di.xml
app/code/Vendor/Theme/Block/Html/Header.php
app/code/Vendor/Theme/view/frontend/templates/html/header.phtml
app/code/Vendor/Theme/view/frontend/web/js/view/customer.js
app/code/Vendor/Theme/etc/module.xml
app/code/Vendor/Theme/registration.php
There are some notes: We will override : Magento\Theme\Block\Html\Header |
Open app/etc/env.php and paste below code. This would allow you to fix everything. 'session' => |
Same issue experience on Magento 2 2.1.9 using Redis Cache. |
We are closing this issue, as we have new one here -> #20571 |
Hi, I have exactly same problem with Magento 2.4.1 and varnish 6 installed. Thanks a lot |
Hi I have faced same issue with Magento 2.4.6 and Varnish version 6. Thanks a lot |
Hi Magento Team,
We found an issue with caching of 'Sign In' link in the header when varnish cache is setup on Magento 2.1.5.
Steps to reproduce are:
1.Go to home page
2.Click 'Sign In' from header
3.Enter login credentials to enter customer account
4.Click website logo to go back to home page
5.Notice that on the home page the header is still showing 'Sign In' instead of showing the customer is logged in and a 'Sign Out' link.
The text was updated successfully, but these errors were encountered: