xF1 XenForo Forum with Nginx fastcgi_cache full page guest caching

  • Downloading from our site will require you to have a paid membership. Upgrade to a Premium Membership today!

    Dont forget read our Rules! Also anyone caught Sharing this content will be banned. By using this site you are agreeing to our rules so read them. Saying I did not know is simply not an excuse! You have been warned.

Radio

    ven0m

    Administrator
    Staff member
    Administrator
    Moderator
    Platinum
    xenForo 2.x.x
    xenForo 1.x.x
    Contributor
    vBulletin All Access Pass
    The Chest
    Verified
    Ultra Platinum VIP
    Platinum VIP
    Gold VIP
    Silver VIP
    Premium
    Member
    Jul 17, 2005
    20,503
    7,742
    321
    localhost
    Capture returned data and persist it to a disk based cache store for a configurable amount of time.
    Additional Requirements:
    Nginx
    Logged In Cookie
    You must be registered for see images

    Nginx includes a FastCGI module which has directives for caching dynamic content that are served from the PHP backend. Setting this up removes the need for additional page caching solutions like reverse proxies (think Varnish) or application specific plugins. Content can also be excluded from caching based on the request method, URL, cookies, or any other server variable.
    Credits to @eva2000 @hungphutho @MattW and @Floren for their guidance when I started this (y).

    • 1st: Install required , to have persistent cookie for Login Users.


    • 2nd: Add this code into your Nginx Config / nginx.conf, below http { block

    For centminmod based server, above this line:
    Assuming you don't modify the default xenforo cookie prefix which is xf_.

    Code:
    ### Start FastCGI Cache ################
    #map $http_user_agent $mobile_request {
    #    default                                         0;
    #    ~*android|ip(hone|od)|windows\s+(?:ce|phone)    1;
    #    ~*symbian|sonyericsson|samsung|lg|blackberry    1;
    #    ~*mobile                                        1;
    #}
    map $http_cookie $nocachecookie {
         default                   0;
        ~xf_fbUid                  1;
        ~xf_user                   1;
        ~xf_logged_in              1;
        #~xf_style_id              1;
    }
    map $request_uri $nocacheuri {
           default              0;
        ~^/register             1;
        ~^/login                1;
        ~^/validate-field       1;
        ~^/captcha              1;
        ~^/lost-password        1;
        ~^/two-step             1;
    }
    fastcgi_cache_path  /tmp/nginx_fastcgi_cache levels=1:2 keys_zone=fastcgicache:200m inactive=30m;
    ### End FastCGI Cache ################

    3rd: From this , add after this code:

    include fastcgi_params;

    For centminmod based server, add above this line:

    Code:
    ### fastcgi_cache start ###
    fastcgi_cache_methods GET;
    #fastcgi_cache_key       $scheme$request_method$host$request_uri$mobile_request;
    fastcgi_cache_key       $scheme$request_method$host$request_uri;
    fastcgi_cache_use_stale error timeout invalid_header updating http_500;
    fastcgi_ignore_headers  Cache-Control Expires Set-Cookie;
    fastcgi_cache           fastcgicache;
    fastcgi_no_cache        $nocachecookie $nocacheuri;
    fastcgi_cache_bypass    $nocachecookie $nocacheuri;
    fastcgi_cache_valid     200 301 302 303 304 404 15m;
    add_header X-Cache-Status   $upstream_cache_status;
    ### fastcgi_cache end ###

    • 4th: Save and then restart Nginx and PHP-FPM. nprestart for centminmod.


    • 5th: To have xf_user cookie persist on every Login Users.

    Modify template helper_login_form and login_bar_form.
    Replace:

    HTML:
    <label class="rememberPassword"><input type="checkbox" name="remember" value="1" id="ctrl_pageLogin_remember" tabindex="3" /> {xen:phrase stay_logged_in}</label>

    With this:

    HTML:
    <input type="hidden" name="remember" value="1" />


    FAQ:
    • Benefits? Same as Varnish and LiteSpeed Full Page Caching.
    • This implementation works similar with [bd] Cache Cache Pages and [DBTech] DragonByte Optimise Guest Full Page Caching but works way BETTER, so disable it when you have those addon.
    • How to verify if fastcgi_cache is working properly?
    Look at the HTTP headers.
    What you are looking for is something that says "X-Cache-Status HIT"
    HIT means that fastcgi is working.
    MISS or anything else means that the page was not served from cache.