Wordpress/Nginx partial page caching for logged in users?

March 21, 2018 682 views
WordPress Nginx Caching Ubuntu 16.04

I have a WordPress site (2 load balanced with a separate shared db server actually), and I have a growing number of users that login to get access to certain content areas of the site. Since the content (although not the access) is static, I would like to cache these pages for logged in users but I have the following problem:

Each of these pages contain links to internal resources that are controlled depending on the membership rights the user has. So for example, on a page of links some of the links will go to the content (if one has rights) and some will go to a "please subscribe for access" page.

I am researching the best way to cache these pages, here are some ideas:

  1. Set cookies based on rights, and cache the output of each page result that matches every combo of cookies

  2. Set cookie for rights and read that value in javascript and update the links (but this seems too hackable on the client)

Any other suggestions? Is there some better system out there for caching only parts of a page or fragment? Or something that caches every possibility but only serves the appropriate cached version based on user rights?

Thanks in advance for any suggestions or pointers!

1 Answer

I answered my own question (or at least have one answer). Here is what I am doing in case it helps anyone else:

  1. On login, I set a session cookie whose value is a combination of memberships the user has.
  2. I changed my fastcgicachekey to "$scheme$requestmethod$host$requesturi$cookie_memberships";

Now, those pages are cached, and will show to the appropriate user based on their membership combination. So I get the benefit of fast cached pages while still respecting user rights.

Have another answer? Share your knowledge.