karljohns0n / nginx-more

Development repository for nginx-more package
MIT License
120 stars 29 forks source link

Avoiding serving from cache when cookie is not set (GeoIP/cookie) #18

Closed erus00 closed 4 years ago

erus00 commented 4 years ago

Hi Karl,

We have several websites for different geographical locations (AU, US, UK, NZ). When a first time visitor comes the Geolocation of the client (GeoLoc) is compared with the website's location (WebLoc).

The issue that I was having is that since I installed nginx-more new visitors were served pages from the cache with a popup suggesting a redirection that was not appropriate for their GeoLoc. php-fpm was not reached.

I am using : /etc/nginx/conf.d/custom/fpm-wordpress-cache-users.conf

After some research I came up with the following modification for "fpm-wordpress-cache-users.conf " in an attempt to bypass the cache for clients who did not receive a cookie saying that they accepted to stay on the website (=new visitors):

Solution 1 - This seems to work:

if ($http_cookie !~* "wow-modal-id-1=yes|wow-modal-id-2=yes|wow-modal-id-3=yes|wow-modal-id-4=yes") {
        set $skip_cache 1;
}

Solution 2 - I was also considering the following but did not try it as Solution 1 works:

if ($cookie_wow-modal-id-1 != "yes") {
        set $skip_cache 1;
}
if ($cookie_wow-modal-id-2 != "yes") {
        set $skip_cache 1;
}
if ($cookie_wow-modal-id-3 != "yes") {
        set $skip_cache 1;
}
if ($cookie_wow-modal-id-4 != "yes") {
        set $skip_cache 1;
}

Solution 1 seems to work perfectly in case the visitor comes to the wrong website as the cache is BYPASS until one of the wow-modal-id-* is activated by the user closing the popup and choosing to stay on the "wrong" website.

New issue: The issue is now that if the visitor comes to the correct website for their GeoLoc then "modal-window" is not triggered so wow-modal-id-* cookie is not established so all the pages are BYPASS which is not desireable.

To correct this behavior I now created a new cookie (Z-Visted=yes) that is given when we established that the visitor is on the correct website then I modified Solution 1 to include it:

if ($http_cookie !~* "Z-Visited=yes|wow-modal-id-1=yes|wow-modal-id-2=yes|wow-modal-id-3=yes|wow-modal-id-4=yes") {
        set $skip_cache 1;
}

Questions: 1- I was wondering if you already created a solution to address these kind of issues in Nginx-More and if you had a more elegant way to do it? 2- I was wondering what happen to the .conf files like "fpm-wordpress-cache-users.conf" in case of update of nginx-more? => Should I put all the changes in a custom .conf file to avoid to be overwritten?

Thank you very much Karl.

karljohns0n commented 4 years ago

Hi,

1- I was wondering if you already created a solution to address these kind of issues in Nginx-More and if you had a more elegant way to do it?

I didn't run into this issue so far so I can't help with this. Feel free to share your solution here so other users can benefit of it or you could ask on nginx mailing list.

2- I was wondering what happen to the .conf files like "fpm-wordpress-cache-users.conf" in case of update of nginx-more? => Should I put all the changes in a custom .conf file to avoid to be overwritten?

Nginx-more updates wont overwrite changes in configuration files such as fpm-wordpress-cache-users.conf. If you modify it and there's changes in a new update, yum will create fpm-wordpress-cache-users.conf.rpmnew so you will be able to diff the files and apply changes if you want. You can also create your own custom conf, it's up to you!

Karl

erus00 commented 4 years ago

Thanks Karl