Get the most out of your Centmin Mod LEMP stack
Become a Member

Wordpress Cache-Enabler Plugin Caching search results with standard centminmod install ?

Discussion in 'Blogs & CMS usage' started by julliuz, Jul 16, 2020.

  1. eva2000

    eva2000 Administrator Staff Member

    45,703
    10,377
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +16,103
    Local Time:
    7:45 PM
    Nginx 1.19.x
    MariaDB 5.5/10.x
    @julliuz also can make a custom search cache invalidation cronjob

    Centmin Mod 123.09beta01's centmin.sh menu option 22 wordpress auto installer with Cache Enabler caching selected will also generate an optional cronjob to invalidate and remove Cache Enabler cached files from cache and look similar to:

    Remove Cache Enabler's cached files every day at 11:16pm (every 24hrs).
    Code (Text):
    16 23 * * * echo "cache-enabler.domain.com cacheenabler cron"; sleep 174s ; rm -rf /home/nginx/domains/cache-enabler.domain.com/public/wp-content/cache/cache-enabler/* > /dev/null 2>&1
    

    With above /search/ caching modifications, you could setup a separate /search/ cache invalidation cronjob which can have a shorter or longer interval than default Cache Enabler cache time.

    For example delete /search/ cached files every 5 minutes
    Code (Text):
    */5 * * * * echo "cache-enabler.domain.com cacheenabler search cron"; sleep 14s ; rm -rf /home/nginx/domains/cache-enabler.domain.com/public/wp-content/cache/cache-enabler/search/* > /dev/null 2>&1
    
     
  2. julliuz

    julliuz Member

    37
    3
    8
    Dec 20, 2018
    Ratings:
    +7
    Local Time:
    10:45 AM
    1.15
    Until I figured out why that method isn't 100% working I forced it to work with these 2 steps:

    Remove the bypass for is_search in cache enabler so when people browse to /search/*keyword* it caches the results.

    add this to nginx:
    location / {
    if ($args ~* "s=(.*)") {
    set $search $1;
    set $args '';
    rewrite .* /search/$search/ permanent;
    }

    it instantly returns a 301 to all searches and serves a cached 200 /search/*keyword* page, loads dropped a shitton instantly. (without the need for adjustments in php snippets)

    I'll investigate why the other method wasn't working, I suspect some config file is off somewhere.
     
  3. eva2000

    eva2000 Administrator Staff Member

    45,703
    10,377
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +16,103
    Local Time:
    7:45 PM
    Nginx 1.19.x
    MariaDB 5.5/10.x
    @julliuz updated the configuration outlined previously and made some changes in /usr/local/nginx/conf/wpincludes/cache-enabler.domain.com/wpcacheenabler_cache-enabler.domain.com.conf

    from
    Code (Text):
       if ($args ~* s=(.*)) {
         set $cache_uri $request_uri;
         set $check_surl $cache_uri;
         set $cache_uri /search/$1;
       }
       add_header Check-Uri "$check_surl";
       add_header Set-Uri "$cache_uri";
    
       # default html file
       set $cache_enabler_uri '${custom_subdir}/wp-content/cache/cache-enabler/${http_host}${cache_uri}index.html';
     
       # webp html file
       if ($http_accept ~* "image/webp") {
           set $cache_enabler_uri_webp '${custom_subdir}/wp-content/cache/cache-enabler/${http_host}${cache_uri}index-webp.html';
       }
    

    to the below version so Nginx takes over from Wordpress redirects so PHP-FPM request is totally bypassed for query string ?s= requests and specific /search/ location match so that you can do more specific things in future i.e. rate limit within /search/ if needed
    Code (Text):
       if ($args ~* s=(.*)) {
         set $cache_uri $request_uri;
         set $check_surl $cache_uri;
         set $cache_uri /search/$1/;
         set $cache_uri_search /search/$1/;
         set $cache_enabler_uri_search '${custom_subdir}/wp-content/cache/cache-enabler/${http_host}${cache_uri_search}index.html';
       }
       location ~ /search/(.*) {
         set $cache_uri $request_uri;
         set $check_surl $cache_uri;
         set $cache_uri_search $request_uri;
         set $cache_enabler_uri_search '${custom_subdir}/wp-content/cache/cache-enabler/${http_host}${check_surl}index.html';
         try_files $cache_enabler_uri_search $cache_enabler_uri_webp $cache_enabler_uri $uri $uri/ $custom_subdir/index.php?$args;
       }
       add_header Check-Uri "$check_surl";
       add_header Set-Uri "$cache_uri";
    
       # default html file
       set $cache_enabler_uri '${custom_subdir}/wp-content/cache/cache-enabler/${http_host}${cache_uri}index.html';
    
       # webp html file
       if ($http_accept ~* "image/webp") {
           set $cache_enabler_uri_webp '${custom_subdir}/wp-content/cache/cache-enabler/${http_host}${cache_uri}index-webp.html';
       }
    
       if (-f $document_root$cache_enabler_uri_search) {
         set $search_exists $cache_enabler_uri_search;
         return 302 https://$host$cache_uri_search;
       }
       if (!-f $document_root$cache_enabler_uri_search) {
         set $search_exists $cache_enabler_uri_search;
       }
       #add_header Check-File "$search_exists";
    

    The results for updated config for cached search requests benchmarks with my forked wrk, wrk-cmm tool now look like
    Code (Text):
    wrk-cmm -t4 -c50 -d20s --latency --breakout http://cache-enabler.domain.com/search/worldpress+cache/
    Running 20s test @ http://cache-enabler.domain.com/search/worldpress+cache/
      4 threads and 50 connections
      Thread Stats   Avg      Stdev     Max   +/- Stdev
        Latency     2.25ms   15.86ms 301.40ms   98.66%
        Connect   203.46us  148.93us 660.00us   87.50%
        TTFB        2.24ms   15.87ms 301.39ms   98.66%
        TTLB       10.40us   37.28us   9.02ms   99.96%
        Req/Sec    28.00k     9.43k   44.87k    66.67%
      Latency Distribution
         50%  311.00us
         75%  534.00us
         90%    1.61ms
         99%   49.76ms
      2226160 requests in 20.07s, 41.85GB read
    Requests/sec: 110910.78
    Transfer/sec:      2.08GB
    

    Code (Text):
    wrk-cmm -t4 -c50 -d20s --latency --breakout http://cache-enabler.domain.com/?s=wordpress+cache
    Running 20s test @ http://cache-enabler.domain.com/?s=wordpress+cache
      4 threads and 50 connections
      Thread Stats   Avg      Stdev     Max   +/- Stdev
        Latency   775.44us    1.85ms  29.36ms   91.06%
        Connect   237.04us  131.89us 567.00us   62.75%
        TTFB      771.92us    1.85ms  29.36ms   91.06%
        TTLB        2.09us   32.86us  10.16ms   99.98%
        Req/Sec    50.01k    12.86k   71.21k    68.12%
      Latency Distribution
         50%  151.00us
         75%  308.00us
         90%    2.14ms
         99%    9.24ms
      3985306 requests in 20.08s, 2.36GB read
    Requests/sec: 198476.64
    Transfer/sec:    120.38MB
    


    FYI, I created a Wordpress Pretty Search Url plugin which can do this functions.php change so you don't need to modify functions.php and link also outlines the whole process more clearly for configuring Centmin Mod LEMP stack auto installed Wordpress with Cache Enabler support for search caching :)
     
    Last edited: Jul 22, 2020
  4. julliuz

    julliuz Member

    37
    3
    8
    Dec 20, 2018
    Ratings:
    +7
    Local Time:
    10:45 AM
    1.15
    Wow yeah, that also did the trick without having to do the dirty workaround! amazing @eva2000 !
     
  5. eva2000

    eva2000 Administrator Staff Member

    45,703
    10,377
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +16,103
    Local Time:
    7:45 PM
    Nginx 1.19.x
    MariaDB 5.5/10.x
  6. julliuz

    julliuz Member

    37
    3
    8
    Dec 20, 2018
    Ratings:
    +7
    Local Time:
    10:45 AM
    1.15
    this one was required in my case for it to work at first, the reason was still unknown, in any case, now its working without this workaround!
     
  7. eva2000

    eva2000 Administrator Staff Member

    45,703
    10,377
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +16,103
    Local Time:
    7:45 PM
    Nginx 1.19.x
    MariaDB 5.5/10.x
    that is because instructions at Wordpress - Caching search results with standard centminmod install ? direct that code to be added to the include file i.e /usr/local/nginx/conf/wpincludes/cache-enabler.domain.com/wpcacheenabler_cache-enabler.domain.com.conf and not in your vhost config file's location / at /usr/local/nginx/conf/conf.d/cache-enabler.domain.com.conf (non-https) or /usr/local/nginx/conf/conf.d/cache-enabler.domain.com.ssl.conf (https)
     
  8. eva2000

    eva2000 Administrator Staff Member

    45,703
    10,377
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +16,103
    Local Time:
    7:45 PM
    Nginx 1.19.x
    MariaDB 5.5/10.x
    FYI, I created a Wordpress Pretty Search Url plugin which can do this functions.php change so you don't need to modify functions.php and link also outlines the whole process more clearly for configuring Centmin Mod LEMP stack auto installed Wordpress with Cache Enabler support for search caching :)
     
  9. julliuz

    julliuz Member

    37
    3
    8
    Dec 20, 2018
    Ratings:
    +7
    Local Time:
    10:45 AM
    1.15
    I'm happy I was the one to send you down this rabbit hole with great success !
     
  10. ahmed

    ahmed Active Member

    350
    48
    28
    Feb 21, 2017
    Ratings:
    +61
    Local Time:
    11:45 AM
    thanks a lot for this great effort

    I installed the plugin did the 2 changes in PHP and conf

    and I get this


    Code:
    HTTP/1.1 200 OK
    Date: Tue, 21 Jul 2020 22:44:30 GMT
    Content-Type: text/html; charset=UTF-8
    Connection: keep-alive
    Set-Cookie: __cfduid=d8c522a7ef3377897878730e231595371469; expires=Thu, 20-Aug-20 22:44:29 GMT; path=/; domain=.domain.com; HttpOnly; SameSite=Lax; Secure
    Vary: Accept-Encoding
    Set-Cookie: wp-wpml_current_language=en; expires=Tue, 21-Jul-2020 23:44:30 GMT; Max-Age=3600; path=/
    Set-Cookie: _icl_current_language=en; expires=Tue, 21-Jul-2020 23:44:30 GMT; Max-Age=3600; path=/
    X-Powered-By: centminmod
    X-Xss-Protection: 1; mode=block
    X-Content-Type-Options: nosniff
    CF-Cache-Status: DYNAMIC
    cf-request-id: 041525d8878888f1e0ac2200000001
    Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
    Strict-Transport-Security: max-age=0
    Server: cloudflare
    CF-RAY: 5b6872652b960bf1-AMS
    Am I right?

    also, this is the code for easier edits


    Code:
    domain=domain.com
    nano /usr/local/nginx/conf/wpincludes/$domain/wpcacheenabler_$domain.conf
     
    Last edited: Jul 22, 2020
  11. eva2000

    eva2000 Administrator Staff Member

    45,703
    10,377
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +16,103
    Local Time:
    7:45 PM
    Nginx 1.19.x
    MariaDB 5.5/10.x
    to see if a Wordpress html page or search page is cached, view the source code of the page in your browser and in footer you should see Cache Enabler's comment cache timestamp i.e.
    PHP:
    <!-- Cache Enabler by KeyCDN 21.07.2020 20:47:43 (html gzip) -->
     
  12. eva2000

    eva2000 Administrator Staff Member

    45,703
    10,377
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +16,103
    Local Time:
    7:45 PM
    Nginx 1.19.x
    MariaDB 5.5/10.x
    Yeah love these kinds of tweaks where only a small amount of effort allows tremendous performance boosts :D
     
  13. ahmed

    ahmed Active Member

    350
    48
    28
    Feb 21, 2017
    Ratings:
    +61
    Local Time:
    11:45 AM
    also is there a method to work around WPML

    this is the link where it switch lang

    Code:
    https://domain.com/?lang=en&s=s
    although the main link is domain.com/search/s/

    I can change the URL structure by making a folder for different lang

    also any interference for WPML cookie?
     
  14. ahmed

    ahmed Active Member

    350
    48
    28
    Feb 21, 2017
    Ratings:
    +61
    Local Time:
    11:45 AM
    I got the footer (I forgot to reboot Nginx)

    also changed WPML to directory structure for peace of mind
     
  15. ahmed

    ahmed Active Member

    350
    48
    28
    Feb 21, 2017
    Ratings:
    +61
    Local Time:
    11:45 AM
    an observation

    for WPML it creats a dir for the lang

    for example

    domain.com/ar/

    with these modifications the search URL is overridden and either lang button leads to the domain with a subfolder which is the default website lang

    any workaround this?

    I mean to cache every search page separately in 2 different lang, i.e respecting the subdirectory or the ? at the url
     
    Last edited by a moderator: Jul 22, 2020
  16. eva2000

    eva2000 Administrator Staff Member

    45,703
    10,377
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +16,103
    Local Time:
    7:45 PM
    Nginx 1.19.x
    MariaDB 5.5/10.x
    I have never used WPML so wouldn't know where to begin with this search caching tweak. What is the default WPML urls like ? got a wordpress site example you can link to ?
     
  17. ahmed

    ahmed Active Member

    350
    48
    28
    Feb 21, 2017
    Ratings:
    +61
    Local Time:
    11:45 AM
  18. eva2000

    eva2000 Administrator Staff Member

    45,703
    10,377
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +16,103
    Local Time:
    7:45 PM
    Nginx 1.19.x
    MariaDB 5.5/10.x
    ah WPML is a paid plugin it seems Purchase WPML - All Accounts Include One Year of Updates and Support so rules out me being able to test it. But I can see probably the default tweaks for cache search outlined in this thread would need adjusting if the official site's search result example is indicating German search result url as https://wpml.org/de/?s=wordpress. Does that mean the rewritten redirect is https://wpml.org/de/search/wordpress or https://wpml.org/search/de/wordpress ?

    @ahmed what is output for wordpress WPML enabled site for command
    Code (Text):
    curl -IL https://yourdomain.com/de/?s=wordpress
    

    also not sure if your Wordpress site's search forms need adjusting for WPML Search form - WPML ?

    edit: looks like WPML list of cache capatible plugins doesn't include Cache Enabler Plugins Compatibility - WPML not sure if list is up to date
     
    Last edited: Jul 22, 2020
  19. ahmed

    ahmed Active Member

    350
    48
    28
    Feb 21, 2017
    Ratings:
    +61
    Local Time:
    11:45 AM
    Code:
    [09:17][root@centos-7-1 feedback]# curl -IL https://domain/ar/?s=wordpress
    HTTP/1.1 200 OK
    Date: Wed, 22 Jul 2020 09:17:29 GMT
    Content-Type: text/html; charset=UTF-8
    Connection: keep-alive
    Set-Cookie: __cfduid=dda4f23efc6a3e33w9ad9e51595409448; expires=Fri, 21-Aug-20 09:17:28 GMT; path=/; domain=.domain.com; HttpOnly; SameSite=Lax; Secure
    Vary: Accept-Encoding
    Set-Cookie: wp-wpml_current_language=ar; expires=Wed, 22-Jul-2020 10:17:29 GMT; Max-Age=3600; path=/
    Set-Cookie: _icl_current_language=ar; expires=Wed, 22-Jul-2020 10:17:29 GMT; Max-Age=3600; path=/
    X-Powered-By: centminmod
    X-Xss-Protection: 1; mode=block
    X-Content-Type-Options: nosniff
    Check-Uri: /ar/?s=wordpress
    Set-Uri: /search/wordpress/
    CF-Cache-Status: DYNAMIC
    cf-request-id: 041769566000000b8023200000001
    Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
    Strict-Transport-Security: max-age=0
    Server: cloudflare
    CF-RAY: 5b6c119d6cb40b80-AMS



    However a previously used search for s

    gives



    Code:
    [09:16][root@centos-7-1 feedback]# curl -IL https://domain.com/ar/?s=s
    HTTP/1.1 302 Moved Temporarily
    Date: Wed, 22 Jul 2020 09:16:59 GMT
    Content-Type: text/html
    Connection: keep-alive
    Set-Cookie: __cfduid=d36533602824b05032ad2b42cfdf183591595409419; expires=Fri, 21-Aug-20 09:16:59 GMT; path=/; domain=.domain.com; HttpOnly; SameSite=Lax; Secure
    Location: https://domain.com/search/s/
    X-Powered-By: centminmod
    X-Xss-Protection: 1; mode=block
    X-Content-Type-Options: nosniff
    Check-Uri: /ar/?s=s
    Set-Uri: /search/s/
    CF-Cache-Status: DYNAMIC
    cf-request-id: 041768e33100000b4700938200000001
    Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
    Strict-Transport-Security: max-age=0
    Server: cloudflare
    CF-RAY: 5b6c10e51cf20b47-AMS
    HTTP/1.1 200 OK
    Date: Wed, 22 Jul 2020 09:16:59 GMT
    Content-Type: text/html; charset=utf-8
    Connection: keep-alive
    Set-Cookie: __cfduid=d36533602824b05032ad2b42cfdf183591595409419; expires=Fri, 21-Aug-20 09:16:59 GMT; path=/; domain=.domain.com; HttpOnly; SameSite=Lax; Secure
    Last-Modified: Tue, 21 Jul 2020 23:19:52 GMT
    Vary: Accept-Encoding
    X-Powered-By: centminmod
    X-Xss-Protection: 1; mode=block
    X-Content-Type-Options: nosniff
    Check-Uri: /search/s/
    Set-Uri: /search/s/
    CF-Cache-Status: DYNAMIC
    cf-request-id: 041768e37900000b470093d200000001
    Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
    Strict-Transport-Security: max-age=0
    Server: cloudflare
    CF-RAY: 5b6c10e58ddd0b47-AMS
     
  20. ahmed

    ahmed Active Member

    350
    48
    28
    Feb 21, 2017
    Ratings:
    +61
    Local Time:
    11:45 AM
    any suggestions for this?