Welcome to Centmin Mod Community
Become a Member

Wordpress Missing "dont cache query strings" in nginx Cache Enabler section(?)

Discussion in 'Blogs & CMS usage' started by Benjamin74, Aug 5, 2018.

  1. Benjamin74

    Benjamin74 Member

    36
    2
    8
    May 2, 2016
    Ratings:
    +6
    Local Time:
    11:45 AM
    Hello,

    Not sure at all what's going on and/or if it's a real bug so take it with a grain of salt ;-)

    Very recently I have weird behaviors on a lead generation form that's dynamically populating incoming Query String parameters into the form fields through PHP (it's a gravity form) : basically sometimes after the cache is deleted by the centminmod CRON job, the query parameters won't be saved in the fields anymore.

    Gravity form supports says that this page should not be cached... (so how did it work for so long??)

    After investigating a bit, I found that KeyCDN Cache Enabler support page states an Nginx directive to avoid caching of pages with query strings:

    WordPress Cache Enabler Plugin - KeyCDN Support

    Code:
    server {
    
        set $cache_uri $request_uri;
    
        # bypass cache if POST requests or URLs with a query string
        if ($request_method = POST) {
            set $cache_uri 'nullcache';
        }
        if ($query_string != "") {
            set $cache_uri 'nullcache';
        }
    ...
    After looking into centmindmod Vhost configuration, I can't find the equivalent of this "if ($query_string != "") {set $cache_uri 'nullcache';}".

    Is this normal or a bug or a choice(I which case maybe I should have configured that query string not cached in another way) ?

    Thanks a lot,


    • CentOS Version: i.e. CentOS 6 64bit
    • Centmin Mod Version Installed: 123.09beta01
    • Nginx Version Installed: i.e. 1.11.13
    • PHP Version Installed: i.e. 5.6.30
    • MariaDB MySQL Version Installed: no clue
    • When was last time updated Centmin Mod code base ? : no clue
    • Persistent Config: Do you have any persistent config file options set in /etc/centminmod/custom_config.inc ? No
     
  2. eva2000

    eva2000 Administrator Staff Member

    36,422
    7,994
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +12,308
    Local Time:
    7:45 PM
    Nginx 1.15.x
    MariaDB 5.5/10.x
    When you create a new nginx vhost domain via centmin.sh menu option 2 or menu option 22 or via /usr/bin/nv cli command line, you will create the Nginx vhost files and directories. You will get an outputted the path location where it will create the domain name's vhost conf file named newdomain.com.conf (and newdomain.com.ssl.conf if you selected yes to self signed SSL)
    • Nginx vhost conf path will be at /usr/local/nginx/conf/conf.d/newdomain.com.conf
    • Nginx HTTP/2 SSL vhost conf path will be at /usr/local/nginx/conf/conf.d/newdomain.com.ssl.conf
    • Nginx Self-Signed SSL Certificate Directory at /usr/local/nginx/conf/ssl/newdomain.com
    • Vhost public web root will be at /home/nginx/domains/newdomain.com/public
    • Vhost log directory will be at /home/nginx/domains/newdomain.com/log
    Please post the contents of /usr/local/nginx/conf/conf.d/newdomain.com.conf and if applicable /usr/local/nginx/conf/conf.d/newdomain.com.ssl.conf wrapped in CODE tags (outlined at How to use forum BBCODE code tags)

    Also when you create wordpress site via centmin.sh menu option 22, it would generate an include file for advanced KeyCDN Cache enabler config settings at /usr/local/nginx/conf/wpincludes/${vhostname}/wpcacheenabler_${vhostname}.conf where ${vhostname} = yourdomain.com. Post the contents of that file too.

    You can replace actual domain name with dummy one if you prefer in the contents.
     
  3. eva2000

    eva2000 Administrator Staff Member

    36,422
    7,994
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +12,308
    Local Time:
    7:45 PM
    Nginx 1.15.x
    MariaDB 5.5/10.x
    Also cache enabler can exclude caches by post or page ids too, so if the form is embedded into a custom page, you can add page id to excluded caching

    upload_2018-8-5_17-57-14.png
     
    • Like Like x 1
  4. Benjamin74

    Benjamin74 Member

    36
    2
    8
    May 2, 2016
    Ratings:
    +6
    Local Time:
    11:45 AM
    Here we go: main config file:

    Code:
    server {
    
      server_name mydomain.com www.mydomain.com;
    
    # ngx_pagespeed & ngx_pagespeed handler
    #include /usr/local/nginx/conf/pagespeed.conf;
    #include /usr/local/nginx/conf/pagespeedhandler.conf;
    #include /usr/local/nginx/conf/pagespeedstatslog.conf;
    
      #add_header X-Frame-Options SAMEORIGIN;
      #add_header X-Xss-Protection "1; mode=block" always;
      #add_header X-Content-Type-Options "nosniff" always;
    
      # limit_conn limit_per_ip 16;
      # ssi  on;
    
      access_log /home/nginx/domains/mydomain.com/log/access.log main_ext buffer=256k$
      error_log /home/nginx/domains/mydomain.com/log/error.log;
    
      include /usr/local/nginx/conf/autoprotect/mydomain.com/autoprotect-travaillerav$
      root /home/nginx/domains/mydomain.com/public;
      # uncomment cloudflare.conf include if using cloudflare for
      # server and/or vhost site
      #include /usr/local/nginx/conf/cloudflare.conf;
      include /usr/local/nginx/conf/503include-main.conf;
    
      include /usr/local/nginx/conf/wpincludes/mydomain.com/wpcacheenabler_travailler$
      #include /usr/local/nginx/conf/wpincludes/mydomain.com/wpsupercache_travaillera$
      # https://community.centminmod.com/posts/18828/
      #include /usr/local/nginx/conf/wpincludes/mydomain.com/rediscache_travaillerave$
    
      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$
    
      # for wp cache enabler plugin
      try_files $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;
    
      }
    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/mydomain.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://community.centminmod.com/posts/18828/
        #include /usr/local/nginx/conf/php-rediscache.conf;
    }
      include /usr/local/nginx/conf/wpincludes/mydomain.com/wpsecure_travailleravecle$
      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/staticfiles.conf;
      include /usr/local/nginx/conf/drop.conf;
      #include /usr/local/nginx/conf/errorpage.conf;
      include /usr/local/nginx/conf/vts_server.conf;
    }
    And the cache enabler one:

    Code:
       # Block nginx-help log from public viewing
        location ~* /wp-content/uploads/nginx-helper/ { deny all; }
    
        set $cache_uri $request_uri;
    
        # exclude mobile devices from redis caching
        if ($cmwpcache_device = mobile) { set $cache_uri 'nullcache'; }
    
        # bypass cache if POST requests or URLs with a query string
        if ($request_method = POST) {
            set $cache_uri 'nullcache';
        }
        if ($query_string != "") {
            set $cache_uri 'nullcache';
        }
    
        # bypass cache if URLs containing the following strings
        if ($request_uri ~* "(\?add-to-cart=|/cart/|/my-account/|/checkout/|/shop/checkout/|/store/c$
            set $cache_uri 'nullcache';
        }
    
        # bypass cache if the cookies containing the following strings
        if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_logged_in|edd_$
            set $cache_uri 'nullcache';
        }
    
        # custom sub directory e.g. /blog
        set $custom_subdir '';
    
        # default html file
        set $cache_enabler_uri '${custom_subdir}/wp-content/cache/cache-enabler/${http_host}${cache_$
        # webp html file
        if ($http_accept ~* "image/webp") {
            set $cache_enabler_uri '${custom_subdir}/wp-content/cache/cache-enabler/${http_host}${ca$
        }
    I didn't know there a specific config file for Cache Enabler and indeed: the code to bypass cache when there is a query string is there as expected.

    So I guess I'm not looking in the right direction to find the source of this problem... I find it pretty odd that the problem occurs after the CRON deletes the cache, I also suspected a problem with Autoptimize but I'm still waiting for this to be confirmed. Since the developer said it was handled by PHP, I doubt Autoptimize to be the culprit but we'll see.

    If you have any other idea on what could cause this, I'm interested ;-)

    Thanks for the help anyway, it doesn't look like it's the nginx config!

    Cheers,
     
  5. Benjamin74

    Benjamin74 Member

    36
    2
    8
    May 2, 2016
    Ratings:
    +6
    Local Time:
    11:45 AM
    Thanks, yeah I'll try the exclude cache by post ID too ;-)
     
  6. eva2000

    eva2000 Administrator Staff Member

    36,422
    7,994
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +12,308
    Local Time:
    7:45 PM
    Nginx 1.15.x
    MariaDB 5.5/10.x
    FYI, your contents are being cut off by word wrap in your linux editor.. you should use cat command to output contents of a file so it ignores word wrap i.e.
    Code (Text):
    cat /usr/local/nginx/conf/conf.d/newdomain.com.ssl.conf
    

    Yeah include makes the vhost much cleaner to configure stuff :)

    no clue not a PHP programmer heh. Excluding post id from cache enabler would rule out cache enabler (the plugin) as the issue if it still happens.
     
    • Like Like x 1
  7. Benjamin74

    Benjamin74 Member

    36
    2
    8
    May 2, 2016
    Ratings:
    +6
    Local Time:
    11:45 AM
    OK so in case someone has the issue someday : despite nginx config saying that URL query parameters shouldn't be cached, somehow GravityForm dynamic field population based on URL query parameters IS GETTING CACHED... so basically the form gets populated with always the same parameter no matter was is passed in the URL.

    Now the good new: excluding by POST IDS inside the Cache Enabler plugin did solve the problem.

    I'm not exactly sure what's going on but at least I found a working configuration.
     
    • Informative Informative x 1
  8. eva2000

    eva2000 Administrator Staff Member

    36,422
    7,994
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +12,308
    Local Time:
    7:45 PM
    Nginx 1.15.x
    MariaDB 5.5/10.x
    Indeed probably best way.. don't think Nginx can known when dynamic query strings are appended/inserted
     
..