nginx problem with logged in users code

dzamanakos

Well-Known Member
Feb 15, 2014
75
6
58
Larisa / Greece
cPanel Access Level
Root Administrator
Hi, after installing nginx as reverse proxy i noticed that some of website functions doesn't work anymore.

In a website i have this piece of code :

PHP:
//// SHOP PAGES ONLY FOR LOGGED IN USERS
function wdo_user_redirect() {
    if (! is_user_logged_in() && (is_woocommerce() || is_cart() || is_checkout())) {
        wp_redirect('/my-account/');
        exit;
    }
}
add_action('template_redirect', 'wdo_user_redirect');
that redirects visitors to the login page if they aren't logged in, so only logged in users can browse shop pages.

I've also tried the same functionality with a plugin WooCommerce for Logged-in Users that does the same thing (i believe it uses the same code).

The problem is that users are always redirected to the login page even if they are logged in.

I believe that nginx is caching the redirect and users are always redirected to the login page.

When i clear the account's nginx cache via the "clear cache" button in user's cpanel it is working for a while, but soon it keeps redirecting users.
If i disable nginx cache for that user via WHM the code is working as expected.

Is there a way to fix this problem without disabling nginx cache for every user using such functionality?
I believe that at the end many users will have the nginx cache disabled and we will loose the benefits of using nginx.

best,
 
Last edited:

cPRex

Jurassic Moderator
Staff member
Oct 19, 2014
10,360
1,632
363
cPanel Access Level
Root Administrator
Hey there! We have seen some odd caching behavior with various tools using Nginx, usually on login pages or other password-protected areas.. Can you let me know if this is default code included with Woocommerce? If so, it would be best to open a ticket so our team could reproduce this on your machine, and then we can get a case created with our developers to address this.
 

dzamanakos

Well-Known Member
Feb 15, 2014
75
6
58
Larisa / Greece
cPanel Access Level
Root Administrator
Hi, i opened ticket 94421311.

I also found another problem with wordpress + nginx caching which is important too, you cant install wordpress and do the 1st login without clearing the nginx cache.

I'll wait the ticket replies as it's a settings problem.
Engintron, which is a plugin for WHM, that's using nginx as reverse proxy and is maintened by a developer doesnt have these problems, as i was using it for years before switching to cpanel nginx.
best,
 

dzamanakos

Well-Known Member
Feb 15, 2014
75
6
58
Larisa / Greece
cPanel Access Level
Root Administrator
Hi, support couldnt reproduce the caching behavior, i'll wait for other people feedback too.
I believe that the problem is that support may be in a case that the system hasn't cached anything yet and it's difficult to reproduce.

best,
 
  • Like
Reactions: cPRex

dzamanakos

Well-Known Member
Feb 15, 2014
75
6
58
Larisa / Greece
cPanel Access Level
Root Administrator
Hi, after checking the problems with L3 support, the only solution is to make changes per account and not server wide.

Wordpress installation with the classic way (not through wordpress toolkit) will be cached and need the user to clear the cache via cpanel's nginx clear cache button in order to login.

Redirect control can be fixed via ?nocache=true in the url, that needs custom work from a website developer (a user just installs a plugin and expects to work).

That's not suitable as i cant control hundreds of users and monitor which plugins they are using, I would prefer a solution that works out of the box.

Engintron for example that i used for some years didnt have these problems and it's based on nginx, so i believe it's a server config issue.

For now i'll return to engintron and / or litespeed.

best,
 
  • Like
Reactions: cPRex