XenForo Forum with Nginx fastcgi_cache full page guest caching

Status
Not open for further replies.
I think it is possible while add a custom mobile/desktop string (from user agent or special mobile cookie) to fastcgi_cache_key value.

I'm trying that
Code:
map $http_user_agent $device_type {
    default 'pc';
    ~(iPhone|Android|IEMobile|Balckberry) 'mobile';
}

And so edit that:
fastcgi_cache_key $scheme$request_method$host$request_uri$mobile_request;

So, it should be cache seperately. But i don't know the xenforo's default mobile user agents while checking. If we put right mobile user-agents fully coming from xenforo, then it will be okay i think.
 
Anyone have any idea for caching multiple user agents with this way ? I didn't fix that problem still. However, i think one way is add a string (desktop/mobile check string) to fastcgi_cache_key value.

And if i found a solution like add custom cookie to visitors who coming from xenforo mobile check = 1 so i can add a parameter to fastcgi_cache_key value so xenforo mobile / desktop may caching differently on nginx fastcgi cache module ... @RoldanLT
I don't have any more idea about that.
That's why I suggest earlier to have your mobile style on sub domain like m.domain.com.
 
Should be easy to add a cookie with jquery inside your mobile theme to archive this:

Code:
<script>var expires = new Date(new Date().getTime() + 7 * 86400000);
$.setCookie('nameofcookie', 'valueofcookie', expires);</script>

then you can add this cookie to your map configuration.
 
Should be easy to add a cookie with jquery inside your mobile theme to archive this:

Code:
<script>var expires = new Date(new Date().getTime() + 7 * 86400000);
$.setCookie('nameofcookie', 'valueofcookie', expires);</script>

then you can add this cookie to your map configuration.
Hello, then how can i add this cookie to fastcgi_cache_key variable ? I must tell the nginx if mobile cookie = 1 then cache it separately based on fastcgi_cache_key.

I see that, fastcgi_cache_key parameters allows only a variable and nginx must assign that after checking the cookie. I think it is not a map directive.
 
Yes, you can add the value to 1 and add it to your map:

Code:
### FastCGI Cache ################
map $http_cookie $nocachecookie {
     default                   0;
    ~xf_fbUid                  1;
    ~xf_user                   1;
    ~xf_logged_in              1;
    ~nameofcookie              1;
}

I haven't test this, but should work.
Test carefully with some browser cookie plugin before so you're sure the cookie is there with the value you want.
 
That way will prevent the cache of mobile version. Because it is nocachecookie variable.

But, i want to cache the mobile style too separately from desktop.

So we must add a string on the fastcgi_cache_key so they will be cache separately on disk.
 
Sorry, I didn't understand :(
Ignore what I said before and add it to your cache key:

Code:
fastcgi_cache_key               $scheme$request_method$host$request_uri$cookie_nameofcookie;

This way you'll have two pages in the cache, one for mobile and another to the mobile theme.
Test and report please :)
 
  • Like
Reactions: rdn
I added the jquery cookie code in page_container template (in mobile style) also i see the source code on mobile style but if i look the browser cookies, i can't see the cookie. Cookie didn't set.
Code:
<script>var expires = new Date(new Date().getTime() + 7 * 86400000);
$.setCookie('mobilevisitor', '1', expires);</script>
Something maybe wrong on code.
 
Sorry, I'm using a jquery cookie plugin.
If you don't want to use this plugin you can use a simple function that does the same.

I was thinking about this approach and you will be in trouble when a mobile crawler that don't accept cookies caches a page ... your next visitors will see the mobile theme, even they use a desktop browser.

You should try to match your browser identification in your xf installation with a map like the one @RoldanLT has here and then add this variable to your fastcgi_cache_key

How do identify your mobile users and what user agents do you match?

Just grab those and use either RoldanLT suggestions or use a map to archive the same.
 
Sorry, I'm using a jquery cookie plugin.
If you don't want to use this plugin you can use a simple function that does the same.

I was thinking about this approach and you will be in trouble when a mobile crawler that don't accept cookies caches a page ... your next visitors will see the mobile theme, even they use a desktop browser.

You should try to match your browser identification in your xf installation with a map like the one @RoldanLT has here and then add this variable to your fastcgi_cache_key

How do identify your mobile users and what user agents do you match?

Just grab those and use either RoldanLT suggestions or use a map to archive the same.
@RoldanLT directives doesn't work for mobile user agent check now.
If we set the all mobile user agents to any string, then we can add that easily to fastcgi_cache_key value, so we can cache them separately (mobile/desktop)

But i didn't run correctly that way.

In my tests, that is works (also i add the $device_type to end of the fastcgi_cache_key parameter.
Code:
map $http_user_agent $device_type {
    default 'pc';
    ~(iphone|Android) 'mobile';
}

That is just for iphone and android user agents. What about the all mobile user agents. If i paste the RoldanLT's user agent list, nginx config gives error.
 
I did it finally. Now mobile and desktop versions are caching separately :) There are no problem for now. I will keep to what happen.

Btw @RoldanLT thank you for your help, i' m using your detailed configuration (not minimal) and what is the cache timeout for that ?
Code:
fastcgi_cache_path              /tmp/nginx_fastcgi_cache levels=1:2 keys_zone=fastcgicache:200m inactive=30m;
This mean max 200mb caching and 30min flush time? I think i should increase the cache size because my forum saving every requests mobile/desktop seperately now.

Code:
fastcgi_cache_valid     200 202 302 404 403 5m;
fastcgi_cache_valid     301 1h;
fastcgi_cache_valid     any 1m;
And that mean if url 301 then cache flush time is 1 hours ? I don't understand the difference.

Also, how purging the cache ? What happen if send a post to a thread ? Cache is purging for this page?

Thanks.
 
Btw @RoldanLT thank you for your help, i' m using your detailed configuration (not minimal) and what is the cache timeout for that ?
Complete info on this page :D
http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html
This mean max 200mb caching and 30min flush time? I think i should increase the cache size because my forum saving every requests mobile/desktop seperately now.
In addition, all active keys and information about data are stored in a shared memory zone, whose name andsize are configured by the keys_zone parameter. One megabyte zone can store about 8 thousand keys.
And that mean if url 301 then cache flush time is 1 hours ?
Yes.
Also, how purging the cache ? What happen if send a post to a thread ? Cache is purging for this page?
I don't have any implementation for this since cache page only last 5 minutes, not a big deal.
 
You think, Is 5min cache very advantage for performance? Of course, it depends how many guests coming to same thread in last 5 min. If only 1 guest , then cache can't important about performance i think. Am i right ? It only gives an advantage if 2 and more guests come to same thread in last 5 min.

I think this cache method will be perfect if we cache page to visitors until that page has modified. If modified, than flush. It will be performance booster then :)
(Except index.php because it changing every minute already from last posts, it may has about 5min cache is good already )
 
I think this cache method will be perfect if we cache page to visitors until that page has modified. If modified, than flush. It will be performance booster then :)
I want something like that also, but don't know how to.
 
I increased the http 200 status cache time to 10min now for seem the performance, but how can i set only forum/index.php cache time to like 3min otherwise, do you know any parameter for that? Because, thread pages may caching up top 10min but index.php should be lowest.

Maybe any extra parameter for fastcgi_cache_valid area ? :)
 
Status
Not open for further replies.
Top Bottom