keycdn / cache-enabler

A lightweight caching plugin for WordPress that makes your website faster by generating static HTML files.
https://wordpress.org/plugins/cache-enabler/
123 stars 46 forks source link

scheme-based cache file generation bug in v1.4.6 #108

Closed centminmod closed 4 years ago

centminmod commented 4 years ago

On my local test Wordpress install I installed Cache Enabler and updated to 1.4.6 but I see from https://github.com/keycdn/cache-enabler/pull/94 the cache filed is now scheme based. However, if I setup a non-https wordpress site i.e. http://cache-enabler.domain.com the Cache Enabler still generates a https-index.html instead of http-index.html file ?

ls -lah /home/nginx/domains/cache-enabler.domain.com/public/wp-content/cache/cache-enabler/cache-enabler.domain.com/
total 40K
drwxrws--- 2 nginx nginx 4.0K Aug 27 03:02 .
drwxrws--- 3 nginx nginx 4.0K Aug 27 03:02 ..
-rw-rw---- 1 nginx nginx  24K Aug 27 03:02 https-index.html
-rw-rw---- 1 nginx nginx 6.6K Aug 27 03:02 https-index.html.gz
curl -s "http://cache-enabler.domain.com" | tail -2                                                                 

<!-- Cache Enabler by KeyCDN @ 27.08.2020 03:02:03 (https html) -->

from wp-cli

WP-CLI 2.4.0
WP-Home    http://cache-enabler.domain.com
WP-SiteURL http://cache-enabler.domain.com
WordPress  version:   5.5
Database   revision:  48748
+--------------------+----------+--------+---------+
| name               | status   | update | version |
+--------------------+----------+--------+---------+
| akismet            | inactive | none   | 4.1.6   |
| autoptimize        | active   | none   | 2.7.7   |
| autoptimize-gzip   | active   | none   | 0.1     |
| cache-enabler      | active   | none   | 1.4.6   |
| cdn-enabler        | inactive | none   | 1.0.9   |
| classic-editor     | active   | none   | 1.6     |
| disable-xml-rpc    | active   | none   | 1.0.1   |
| pretty-search-url  | active   | none   | 0.1     |
| sucuri-scanner     | active   | none   | 1.8.24  |
| webp-express       | active   | none   | 0.17.5  |
| advanced-cache.php | dropin   | none   |         |
+--------------------+----------+--------+---------+

I setup my Nginx advanced cache rules to use $scheme nginx variable to prefix index.html with either https- or http- but this wouldn't work if Cache Enabler is generating https- prefix even for non-https setup Wordpress site !

    # default html file
    set $cache_enabler_uri '${custom_subdir}/wp-content/cache/cache-enabler/${http_host}${cache_uri}${scheme}-index.html';

    # webp html file
    if ($http_accept ~* "image/webp") {
        set $cache_enabler_uri '${custom_subdir}/wp-content/cache/cache-enabler/${http_host}${cache_uri}${scheme}-index-webp.html';
    }

PHP 7.3.21

php -v
PHP 7.3.21 (cli) (built: Aug 12 2020 22:45:53) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.21, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.3.21, Copyright (c) 1999-2018, by Zend Technologies

phpinfo variables report

$_SERVER['SERVER_NAME'] | cache-enabler.domain.com
$_SERVER['SERVER_PORT'] | 80
$_SERVER['REQUEST_SCHEME'] | http
$_SERVER['HTTPS'] | off

Maybe instead of using port 443/80 to determine if https or https prefix is set, use $_SERVER['REQUEST_SCHEME'] and/or $_SERVER['HTTPS'] ? This would also allow non-standard ports to map correctly i.e. HTTP site on port 8080 or HTTPS site on port 8443 would be properly detected too. edit: looks like you did that in https://github.com/keycdn/cache-enabler/commit/beac15dbe6806330fd8478c911564d7e6045c004

and verified my copy has that

grep -n 'HTTPS' /home/nginx/domains/cache-enabler.domain.com/public/wp-content/plugins/cache-enabler/inc/cache_enabler_disk.class.php

470:        return ( ( isset( $_SERVER['HTTPS'] ) && ! empty( $_SERVER['HTTPS'] ) ) || $_SERVER['SERVER_PORT'] === '443' ) ? 'https' : 'http';

however, my phpinfo reports $_SERVER['HTTPS'] | off so for non-https site the variable isn't empty but = off so maybe that falsely matches your check ?

so maybe

        return ( ( isset( $_SERVER['HTTPS'] ) &&  $_SERVER['HTTPS'] !== 'off' ) || $_SERVER['SERVER_PORT'] === '443' ) ? 'https' : 'http';

which seems to work

curl -s "http://cache-enabler.domain.com" | tail -2                             
<!-- Cache Enabler by KeyCDN @ 27.08.2020 04:41:34 (http html) -->
ls -lah /home/nginx/domains/cache-enabler.domain.com/public/wp-content/cache/cache-enabler/cache-enabler.domain.com/
total 40K
drwxrws--- 2 nginx nginx 4.0K Aug 27 04:41 .
drwxrws--- 3 nginx nginx 4.0K Aug 27 04:41 ..
-rw-rw---- 1 nginx nginx  24K Aug 27 04:41 http-index.html
-rw-rw---- 1 nginx nginx 6.6K Aug 27 04:41 http-index.html.gz

I checked on Apache PHP server $_SERVER['HTTPS'] is empty none existent on non-https requests but on Nginx with PHP-FPM $_SERVER['HTTPS'] is set to off for non-https requests

On Nginx/PHP-FPM https request shows $_SERVER['HTTPS'] | on

the browser request that generated the cached file

image

centminmod commented 4 years ago

Added pull request fix https://github.com/keycdn/cache-enabler/pull/109 :)