Wordpress/Nginx partial page caching for logged in users?

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!

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

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 fastcgi_cache_key to “$scheme$request_method$host$request_uri$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.

Can you share the function that sets the cookie upon login?

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel