{"id":7199,"date":"2017-06-07T06:00:48","date_gmt":"2017-06-07T10:00:48","guid":{"rendered":"http:\/\/blog.litespeedtech.com\/?p=7199"},"modified":"2017-06-07T06:00:48","modified_gmt":"2017-06-07T10:00:48","slug":"wpw-conflict-free-cookies-and-tags-on-multi-app-sites","status":"publish","type":"post","link":"https:\/\/lswp.store\/index.php\/2017\/06\/07\/wpw-conflict-free-cookies-and-tags-on-multi-app-sites\/","title":{"rendered":"WpW: Conflict-free Cookies and Tags on Multi-app Sites"},"content":{"rendered":"<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-7537\" src=\"https:\/\/lswp.store\/wp-content\/uploads\/2017\/06\/wpw05-1.jpg\" alt=\"\" width=\"640\" height=\"320\" srcset=\"https:\/\/lswp.store\/wp-content\/uploads\/2017\/06\/wpw05-1.jpg 640w, https:\/\/lswp.store\/wp-content\/uploads\/2017\/06\/wpw05-1-300x150.jpg 300w, https:\/\/lswp.store\/wp-content\/uploads\/2017\/06\/wpw05-1-600x300.jpg 600w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/p>\n<p>Welcome to another installment of <a href=\"http:\/\/blog.litespeedtech.com\/tag\/wordpress-wednesday\">WordPress Wednesday<\/a>!<\/p>\n<p>Today we\u2019re going to discuss the <strong>conflicts that arise on multi-app sites, specifically login vary cookie conflicts, and cache tag conflicts<\/strong>. These issues can pop up when you have multiple-WordPress installations with <a href=\"https:\/\/wordpress.org\/plugins\/litespeed-cache\/\"> the LSCache plugin<\/a> enabled on the same root (as in <code>www.example.com\/<\/code> and <code>www.example.com\/store<\/code>).<\/p>\n<p><em>We\u2019re going to be focusing on WordPress for this article, but you can sometimes see these types of conflicts with other apps that have LSCache enabled (Magento and XenForo, for example). If you want to know more about cookie and tag conflicts involving multiple types of applications, <a href=\"https:\/\/www.litespeedtech.com\/support\/wiki\/doku.php\/litespeed_wiki:cache:common#applies_to_plugins_only\">this wiki<\/a> will point you in the right direction.<\/em><\/p>\n<p>Let\u2019s look at a WordPress blog installed at <code>www.example.com<\/code> and a separate WordPress storefront installed at <code>www.example.com\/store<\/code>. This is our current example, but be aware that the same concepts will apply to any situation where one (or more) WP sites is\u00a0installed in a subdirectory of any other WP site.<\/p>\n<p>Here are two possible problems you can have:<\/p>\n<ol>\n<li><strong>Login Vary Cookie Conflict:<\/strong> A page may be served from cache when it shouldn&#8217;t be.<\/li>\n<li><strong>Purge Tag Conflict:<\/strong> Pages may be purged from cache when they shouldn\u2019t be.<\/li>\n<\/ol>\n<p>Let\u2019s chat about both of these scenarios, what causes them, and how to fix them.<\/p>\n<h2>Mmmmm. Login Vary Cookies.<\/h2>\n<p>Before we explain how to fix this problem, it\u2019s handy to understand how this is all <em>supposed<\/em> to work.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-large wp-image-7255\" src=\"http:\/\/blog.litespeedtech.com\/wp-content\/uploads\/2017\/06\/wpw05a-1024x683.jpg\" alt=\"\" width=\"1024\" srcset=\"https:\/\/lswp.store\/wp-content\/uploads\/2017\/06\/wpw05a-1024x683.jpg 1024w, https:\/\/lswp.store\/wp-content\/uploads\/2017\/06\/wpw05a-300x200.jpg 300w, https:\/\/lswp.store\/wp-content\/uploads\/2017\/06\/wpw05a-768x512.jpg 768w, https:\/\/lswp.store\/wp-content\/uploads\/2017\/06\/wpw05a-1536x1024.jpg 1536w, https:\/\/lswp.store\/wp-content\/uploads\/2017\/06\/wpw05a-2048x1365.jpg 2048w, https:\/\/lswp.store\/wp-content\/uploads\/2017\/06\/wpw05a-1320x880.jpg 1320w, https:\/\/lswp.store\/wp-content\/uploads\/2017\/06\/wpw05a-600x400.jpg 600w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p>As of this writing, LSCache for WordPress will only cache and serve pages for non-logged in users. (Remember this. It\u2019s important.) This behavior is controlled by a vary cookie called <code>_lscache_vary<\/code>. \u00a0Once users have logged in, the cookie is set, and their page requests will <em> always <\/em> be processed by WordPress. The pages they visit will not be cached, nor will cached pages be served to them.<\/p>\n<p>You can see why it\u2019s important, then, for the <code>_lscache_vary<\/code> cookie to be set correctly. <strong>It\u2019s the key to determining which users get pages served from cache, and which users get pages through WordPress.<\/strong><\/p>\n<p>Ok, so let\u2019s say your domain serves Example Blog at <code>www.example.com\/<\/code> and Example Store at <code>www.example.com\/store\/<\/code>, which is a subdirectory of Example Blog. If you log into Example Blog, the <code>_lscache_vary<\/code> cookie will be set to indicate that you are logged in, and your requests will not be cached. They will always be sent to the WordPress backend. This is normal behavior.<\/p>\n<p>You then visit Example Store without logging in. Since you are a non-logged in user, LSCache steps up and caches the page you requested. This is also normal behavior. However, the logged in <code>_lscache_vary<\/code> cookie is still set from your visit to Example Blog, and so this page is cached <em> for logged-in users <\/em> . Uh oh. This causes future logged-in Example Store users to get a \u201ccache hit\u201d on this page and be served the non-logged in version of the page. This is decidedly <em> not <\/em> normal behavior.<\/p>\n<p>Besides the fact that logged-in users are now being served cached pages (which they should not be), they are being served the non-logged-in versions of those pages (which is also wrong).<\/p>\n<h3>Why does this happen?<\/h3>\n<p>Think about it from the browser\u2019s point of view. As far as the browser is concerned, Example Blog (<code>www.example.com\/<\/code>) and Example Store (<code>www.example.com\/store\/<\/code>) are the same website because the store is actually a subdirectory of the blog. When the browser visits either one of those addresses, it will use the cookies for <code>www.example.com<\/code>. \u00a0Even though the store is an entirely separate WordPress application, to the browser it looks simply like a part of the blog.<\/p>\n<h3>How can we fix it?<\/h3>\n<p>We need to tell the browser to use distinct cookies for Example Blog and Example Store. This requires a simple change within the LSCache plugin settings.<\/p>\n<p>By default, the cache will use <code>_lscache_vary<\/code> as the login cookie in any WordPress installation. We can avoid conflicts by renaming some of the cookies. <strong>Any place where we have one WordPress installation in a subdirectory of another WordPress installation, we will need to set unique cookie names.<\/strong><\/p>\n<p>So for our two-app scenario, you could log in to the WP-Admin panel of Example Blog, navigate to <strong>LiteSpeed Cache &gt; Settings &gt; Advanced Settings<\/strong>, and set <strong>Login Cookie<\/strong> to <code>_lscache_vary_blog<\/code> (or whatever you would like it to be).<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-7273\" src=\"http:\/\/blog.litespeedtech.com\/wp-content\/uploads\/2017\/06\/wpw05b.jpg\" alt=\"\" width=\"1001\" srcset=\"https:\/\/lswp.store\/wp-content\/uploads\/2017\/06\/wpw05b.jpg 1001w, https:\/\/lswp.store\/wp-content\/uploads\/2017\/06\/wpw05b-300x117.jpg 300w, https:\/\/lswp.store\/wp-content\/uploads\/2017\/06\/wpw05b-768x299.jpg 768w, https:\/\/lswp.store\/wp-content\/uploads\/2017\/06\/wpw05b-600x234.jpg 600w\" sizes=\"(max-width: 1001px) 100vw, 1001px\" \/><\/p>\n<p>You could then do the same in Example Store\u2019s WP-Admin panel and set <strong>Login Cookie<\/strong> to something different, like <code>_lscache_vary_store<\/code>. \u00a0(Technically, you don\u2019t need to change both cookie names, because the act of changing just one of them has already given you two unique cookie names. For example, if you only change the cookie on Example Store, you\u2019d have <code>_lscache_vary<\/code> and <code>_lscache_vary_store<\/code>.)<\/p>\n<p>If you had more installations running off of this same root, you\u2019d want to repeat this process for all of them.<\/p>\n<p><strong>Achievement unlocked: eliminate login vary cookie conflicts!<\/strong><\/p>\n<h2>Cache Tag Prefix<\/h2>\n<p>&#8212;<\/p>\n<p><strong><em>September 2017 UPDATE: <\/em><\/strong><em>in the latest versions of LSCache, <strong>this is no longer an issue<\/strong>. Cache Tax Prefixes are automatically generated and given a unique string for each installation, thereby eliminating the possibility of conflicts. We&#8217;re keeping the information in this section for historical reference, but this is not an issue you should encounter anymore.\u00a0<\/em><\/p>\n<p>&#8212;<\/p>\n<p>Here is another problem you may run into when you have Example Blog and Example Store running together on <code>www.example.com<\/code>: excessive purging of the cache.<\/p>\n<p>Note: This is <em> not <\/em> relevant for a single multi-site WordPress install. This issue only occurs in two (or more) distinct WordPress installations that happen to use the same cache root.<\/p>\n<p>So here\u2019s the issue: LSCache for WordPress has a default cache tag prefix of <code>b1_<\/code>, and this is the same on every WordPress instance you install. In addition to being a prefix, every page is also cached with the tag <code>b1_<\/code> to denote that it belongs to that blog. The cache tag is used to inform LSCache which pages to purge from cache. So, for instance, in a situation where you might want to purge your entire Example Blog from cache, and you press that button, LSCache interprets the command as \u201cpurge everything with the cache tag \u00a0<code>b1_<\/code>\u201d and out goes your entire Example Blog cache (which you wanted) <em> and <\/em> your Example Store cache as well (which you did not want), because it also has the <code>b1_<\/code> tag.<\/p>\n<p>To solve this, you need to give all of your sites unique custom <strong>Cache Tag Prefix<\/strong>es in the same Advanced settings screen as before.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-7275\" src=\"http:\/\/blog.litespeedtech.com\/wp-content\/uploads\/2017\/06\/wpw05c.jpg\" alt=\"\" width=\"984\" srcset=\"https:\/\/lswp.store\/wp-content\/uploads\/2017\/06\/wpw05c.jpg 984w, https:\/\/lswp.store\/wp-content\/uploads\/2017\/06\/wpw05c-300x111.jpg 300w, https:\/\/lswp.store\/wp-content\/uploads\/2017\/06\/wpw05c-768x283.jpg 768w, https:\/\/lswp.store\/wp-content\/uploads\/2017\/06\/wpw05c-600x221.jpg 600w\" sizes=\"(max-width: 984px) 100vw, 984px\" \/><\/p>\n<p>For Example Blog, you could set it to <code>b<\/code>, and for Example Store, set it to <code>s<\/code>. Those strings would then be added to the beginning of the relevant app\u2019s cache and purge tags. A command to purge Example Blog would then be interpreted as \u201cpurge everything with the cache tag <code>b_b1_<\/code>. \u201d \u00a0Example Store would be left alone, because its purge tags begin with <code>s_b1_<\/code> .<\/p>\n<p><strong>Achievement unlocked: eliminate cache purge conflicts!<\/strong><\/p>\n<h2>In a nutshell&#8230;<\/h2>\n<p>Using <a href=\"https:\/\/wordpress.org\/plugins\/litespeed-cache\/\"> LiteSpeed Cache <\/a> on your WordPress (and other) sites can lead to dramatic improvements in performance, so it pays to spend a minute or two working out little kinks like these! For more information on how to deal with cookie and tag issues as they relate to apps other than WordPress, <a href=\"https:\/\/www.litespeedtech.com\/support\/wiki\/doku.php\/litespeed_wiki:cache:common#applies_to_plugins_only\"> take a look at our wiki articles <\/a> .<\/p>\n<p>&#8212;<\/p>\n<p>Have some of your own ideas for future <a href=\"http:\/\/blog.litespeedtech.com\/tag\/wordpress-wednesday\">WordPress Wednesday<\/a> topics? Leave us a comment!<\/p>\n<p>Don\u2019t forget to meet us back here next week for the next installment. In the meantime, here are a few other things you can do:<\/p>\n<ul>\n<li>Subscribe to the <a href=\"http:\/\/blog.litespeedtech.com\/tag\/wordpress-wednesday\/feed\/\">WordPress Wednesday RSS feed<\/a><\/li>\n<li>Download <a href=\"https:\/\/wordpress.org\/plugins\/litespeed-cache\/\">LiteSpeed Cache for WordPress plugin<\/a><\/li>\n<li>Learn more about the plugin <a href=\"https:\/\/www.litespeedtech.com\/products\/cache-plugins\/wordpress-acceleration\">on our website<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Learn to solve cache tag and vary cookie conflicts that may arise with multiple WordPress installations.<\/p>\n","protected":false},"author":1,"featured_media":8148,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[28],"tags":[53,296,316,317],"class_list":["post-7199","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cache","tag-cache-tags","tag-vary-cookies","tag-wordpress","tag-wordpress-wednesday"],"jetpack_featured_media_url":"https:\/\/lswp.store\/wp-content\/uploads\/2017\/07\/wpw05.jpg","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/lswp.store\/index.php\/wp-json\/wp\/v2\/posts\/7199","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lswp.store\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lswp.store\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lswp.store\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lswp.store\/index.php\/wp-json\/wp\/v2\/comments?post=7199"}],"version-history":[{"count":0,"href":"https:\/\/lswp.store\/index.php\/wp-json\/wp\/v2\/posts\/7199\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lswp.store\/index.php\/wp-json\/wp\/v2\/media\/8148"}],"wp:attachment":[{"href":"https:\/\/lswp.store\/index.php\/wp-json\/wp\/v2\/media?parent=7199"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lswp.store\/index.php\/wp-json\/wp\/v2\/categories?post=7199"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lswp.store\/index.php\/wp-json\/wp\/v2\/tags?post=7199"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}