Welcome to Centmin Mod Community
Become a Member

Wordpress How To Switch From Cache Enabler To Redis Nginx Level Wordpress Caching?

Discussion in 'Blogs & CMS usage' started by eva2000, Jun 7, 2022.

Thread Status:
Not open for further replies.
  1. eva2000

    eva2000 Administrator Staff Member

    48,894
    11,189
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +17,423
    Local Time:
    7:17 AM
    Nginx 1.21.x
    MariaDB 10.x
    Centmin Mod's centmin.sh menu option 22 Wordpress auto installer as several preset options for guest full HTML page caching. Read the below instructions if you had chosen Cache Enabler based guest full HTML page caching already but want to switch to Redis Nginx level caching.

    Step 1. Remove and disable Cache Enabler caching as outlined at https://community.centminmod.com/threads/disable-or-remove-keycdn-cache-enabler.15657/

    Step 2. Setup a dummy Redis Nginx level caching Wordpress instance via centmin.sh menu option 22 that you will delete afterwards. Make sure to use an Nginx vhost domain name different from your live Wordpress domain site. For instance, if you created the dummy Redis Nginx level caching Wordpress installation domain as wpredis.domain.com, then at end of centmin.sh menu option 22 run, an uninstaller script will be created for it at /root/tools/wp_uninstall_wpredis.domain.com.sh. Keep this script in mind for the final step at the bottom to remove the dummy Redis Nginx level cache Wordpress instance. DO NOT run this uninstall script yet as you still need to reference the dummy Wordpress instances created files. The purpose of creating the dummy Wordpress instance is that the routine will automatically set up the underlying Nginx upstream Redis configuration which will persist even after uninstalling the dummy Wordpress instance. This includes automatically installing Redis server if it hasn't been already installed. This is so you do not have to manually install Redis server and set up all the Nginx upstream Redis configuration settings.

    What is setup and persists are include files in Nginx config file at /usr/local/nginx/conf/nginx.conf for /usr/local/nginx/conf/redisupstream.conf
    Code (Text):
    include /usr/local/nginx/conf/redisupstream.conf;
    

    contents of redisupstream.conf where the default redis server for caching is 127.0.0.1:6379 on port 6379 which is Redis server's default port. Other server entries are commented out with a hash # in front to disable them as they are not created by default, but if you manually decide to start up more Redis server instances on different ports to load balance the Redis server caching, then these are examples of how.
    Code (Text):
    map $http_user_agent $redis_device {
        default                                     'desktop';
        ~*(iPad|iPhone|Android|IEMobile|Blackberry) 'mobile';
        "~*Firefox.*Mobile"                         'mobile';
        "~*ipod.*mobile"                            'mobile';
        "~*Opera\ Mini"                             'mobile';
        "~*Opera\ Mobile"                           'mobile';
        "~*Mobile"                                  'mobile';
        "~*Tablet"                                  'mobile';
        "~*Kindle"                                  'mobile';
        "~*Windows\ Phone"                          'mobile';
    }
    
    upstream redisbackend {
      zone upstream_redis 64k;
      server 127.0.0.1:6379 weight=1 max_fails=3 fail_timeout=30s;
      #server 127.0.0.1:6380 weight=1 max_fails=3 fail_timeout=30s;
      #server 127.0.0.1:6381 weight=1 max_fails=3 fail_timeout=30s;
      #server 127.0.0.1:6382 weight=1 max_fails=3 fail_timeout=30s;
      #server 127.0.0.1:6383 weight=1 max_fails=3 fail_timeout=30s;
      #server 127.0.0.1:6384 weight=1 max_fails=3 fail_timeout=30s;
    
      #server 127.0.0.1:6380 backup;
      keepalive 4096;
    }
    


    Step 3. Next copying over dummy Wordpress instances created include file /usr/local/nginx/conf/wpincludes/wpredis.domain.com/rediscache_wpredis.domain.com.conf by running these SSH commands where live.domain.com is the name of your live Wordpress site
    Code (Text):
    dummydomain=wpredis.domain.com
    livedomain=live.domain.com
    mkdir -p /usr/local/nginx/conf/wpincludes/${livedomain}
    \cp -af /usr/local/nginx/conf/wpincludes/${dummydomain}/rediscache_${dummydomain}.conf /usr/local/nginx/conf/wpincludes/${livedomain}/rediscache_${livedomain}.conf
    \cp -af /usr/local/nginx/conf/pre-staticfiles-local-${dummydomain}.conf /usr/local/nginx/conf/pre-staticfiles-local-${livedomain}.conf
    

    Example contents from dummy Wordpress instance's /usr/local/nginx/conf/wpincludes/wpredis.domain.com/rediscache_wpredis.domain.com.conf
    Code (Text):
    # Block nginx-help log from public viewing
    location ~* /wp-content/uploads/nginx-helper/ { deny all; }
    
    set $skip_cache 0;
    
    # exclude mobile devices from redis caching
    if ($redis_device = mobile) { set $skip_cache 1; }
    
    # POST requests and urls with a query string should always go to PHP
    if ($request_method = POST) {
      set $skip_cache 1;
    }
    
    if ($query_string != "") {
      set $skip_cache 1;
    }
    
    # Don't cache uris containing the following segments
    if ($request_uri ~* "\?add-to-cart=|\?wc-ajax=|\?wc-api=|/cart/|/my-account/|/checkout/|/shop/checkout/|/wp-json/|/store/checkout/|/customer-dashboard/|/addons/|/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
      set $skip_cache 1;
    }
    
    # Don't use the cache for logged in users or recent commenters
    if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in|wc-api|edd_items_in_cart|woocommerce_items_in_cart|woocommerce_cart_hash|woocommerce_recently_viewed|wc_session_cookie_HASH|wp_woocommerce_session_|wptouch_switch_toggle") {
      set $skip_cache 1;
    }
    
    # bypass cache for woocommerce
    if ($arg_add-to-cart != "") { set $skip_cache 1; }
    if ($arg_wc-api != "") { set $skip_cache 1; }
    
    ## bypass cache for empty woocommerce carts
    #if ($cookie_woocommerce_items_in_cart != "0") {
    #  set $skip_cache 1;
    #}
    
    location /redis-fetch {
      internal  ;
      set  $redis_key $args;
      redis_pass  redisbackend;
      redis_connect_timeout 60000;
      redis_read_timeout 60000;
      redis_send_timeout 60000;
    }
    
    location /redis-store {
      internal  ;
      set_unescape_uri $key $arg_key ;
      redis2_query set $key $echo_request_body;
      redis2_query expire $key 6h;
      redis2_pass  redisbackend;
      redis2_connect_timeout 60s;
      redis2_read_timeout 60s;
      redis2_send_timeout 60s;
    }
    
    location /redis-store-shortttl {
      internal  ;
      set_unescape_uri $key $arg_key ;
      redis2_query set $key $echo_request_body;
      redis2_query expire $key 3600;
      redis2_pass  redisbackend;
      redis2_connect_timeout 60s;
      redis2_read_timeout 60s;
      redis2_send_timeout 60s;
    }
    


    Step 4. Edit your live Wordpress Nginx vhost config file's /usr/local/nginx/conf/conf.d/live.domain.com.ssl.conf include files to support Redis Nginx level caching. Where live.domain.com is your live Wordpress domain.

    Uncomment the include file line for /usr/local/nginx/conf/wpincludes/live.domain.com/rediscache_live.domain.com.conf by removing hash # from the beginning of the line entry.
    Code (Text):
      #include /usr/local/nginx/conf/wpincludes/live.domain.com/wpcacheenabler_live.domain.com.conf;
      #include /usr/local/nginx/conf/wpincludes/live.domain.com/wpsupercache_live.domain.com.conf;
      # https://community.centminmod.com/posts/18828/
      include /usr/local/nginx/conf/wpincludes/live.domain.com/rediscache_live.domain.com.conf;
    


    Then in same live Wordpress Nginx vhost config file's /usr/local/nginx/conf/conf.d/live.domain.com.ssl.conf find the main location / {} context's try_files line and comment out with hash # in front the old cache enabler version if you haven't done so from step 1 above and uncomment by removing hash # from begging of Nginx level redis Wordpress try_files version.

    The location / {} context would now look like

    Code (Text):
      location / {
      include /usr/local/nginx/conf/503include-only.conf;
     
    
      # Enables directory listings when index file not found
      #autoindex  on;
    
      # for wordpress super cache plugin
      #try_files /wp-content/cache/supercache/$http_host/$cache_uri/index.html $uri $uri/ /index.php?q=$uri&$args;
    
      # for wp cache enabler plugin
      #try_files $cache_enabler_uri_webp $cache_enabler_uri $uri $uri/ $custom_subdir/index.php?$args;
    
      # Wordpress Permalinks
      #try_files $uri $uri/ /index.php?q=$uri&$args;
    
      # Nginx level redis Wordpress
      # https://community.centminmod.com/posts/18828/
      try_files $uri $uri/ /index.php?$args;
    
      }
    


    Then comment out with hash in front all lines for /usr/local/nginx/conf/php-wpsc.conf include file and uncomment the lines for /usr/local/nginx/conf/php-rediscache.conf include file. The php-rediscache.conf include file is responsible for handling PHP requests and passing back and forth request to Redis server for caching etc.


    Below example.
    Code (Text):
    location ~* /(wp-login\.php) {
        limit_req zone=xwplogin burst=1 nodelay;
        #limit_conn xwpconlimit 30;
        #auth_basic "Private";
        #auth_basic_user_file /home/nginx/domains/live.domain.com/htpasswd_wplogin;
        #include /usr/local/nginx/conf/php-wpsc.conf;
     
        # https://community.centminmod.com/posts/18828/
        include /usr/local/nginx/conf/php-rediscache.conf;
    }
    
    location ~* /(xmlrpc\.php) {
        limit_req zone=xwprpc burst=45 nodelay;
        #limit_conn xwpconlimit 30;
        #include /usr/local/nginx/conf/php-wpsc.conf;
        # https://jetpack.com/support/hosting-faq/
        include /usr/local/nginx/conf/jetpack_whitelist_ip.conf;
     
        # https://community.centminmod.com/posts/18828/
        include /usr/local/nginx/conf/php-rediscache.conf;
    }
    
    location ~* /wp-admin/(load-scripts\.php) {
        limit_req zone=xwprpc burst=5 nodelay;
        #limit_conn xwpconlimit 30;
        #include /usr/local/nginx/conf/php-wpsc.conf;
     
        # https://community.centminmod.com/posts/18828/
        include /usr/local/nginx/conf/php-rediscache.conf;
    }
    
    location ~* /wp-admin/(load-styles\.php) {
        limit_req zone=xwprpc burst=5 nodelay;
        #limit_conn xwpconlimit 30;
        #include /usr/local/nginx/conf/php-wpsc.conf;
     
        # https://community.centminmod.com/posts/18828/
        include /usr/local/nginx/conf/php-rediscache.conf;
    }
    
      include /usr/local/nginx/conf/wpincludes/live.domain.com/wpsecure_live.domain.com.conf;
      #include /usr/local/nginx/conf/php-wpsc.conf;
     
      # https://community.centminmod.com/posts/18828/
      include /usr/local/nginx/conf/php-rediscache.conf;
      include /usr/local/nginx/conf/pre-staticfiles-local-live.domain.com.conf;
      include /usr/local/nginx/conf/pre-staticfiles-global.conf;
      include /usr/local/nginx/conf/staticfiles.conf;
      include /usr/local/nginx/conf/drop.conf;
      #include /usr/local/nginx/conf/errorpage.conf;
      include /usr/local/nginx/conf/vts_server.conf;
    }
    


    Step 5. Restart Nginx and PHP-FPM services for all Nginx and PHP changes to take effect.

    Using service commands or Centmin Mod command shortcuts for an Nginx config check (-t) and then restarts
    Code (Text):
    nginx -t
    service nginx restart
    service php-fpm restart
    

    Code (Text):
    nginx -t
    nprestart
    


    Step 6. Login to live Wordpress instance admin settings menu and install Nginx Helper plugin and then go to Nginx Helper settings for Nginx Helper Enable Purging set Caching Method to Redis Cache, & set Purging Conditions. See below screenshots.

    wp-nginx-helper-redis-01.png

    wp-nginx-helper-redis-02.png
     
    Last edited: Jun 7, 2022
Thread Status:
Not open for further replies.