-
Notifications
You must be signed in to change notification settings - Fork 11
Home
The statcache extra is made up of a Plugin for MODX Revolution that writes out static representations of fully cacheable MODX Resources to a configurable location on the filesystem. You can then use your web server's rewrite engine (or equivalent) to serve the static files first, if they exist. This has the advantage of bypassing MODX (and PHP) entirely, allowing you to serve potentially thousands of more requests per second. It has the disadvantage of not allowing any dynamic content to be served however.
See StaticCache Plugin for details on Plugin Events and Properties.
Once the extra is installed and the StaticCache plugin is enabled and generating your static files, you can configure nginx to serve those files first by preceding the standard MODX rules for nginx. You will also want to exclude the User Agent you configure in the plugin from serving the static files.
set $cache_prefix 'statcache';
if ($http_user_agent = 'MODX RegenCache') {
set $cache_prefix 'no cache';
}
location / {
try_files /$cache_prefix$uri~index.html /$cache_prefix$uri $uri $uri/ @modx-rewrite;
}
location @modx-rewrite {
rewrite ^/(.*)$ /index.php?q=$1 last;
}
This instructs nginx to look for a static representation of the requested resource within the statcache/
directory inside your document root before all the standard MODX checks, unless the User Agent is MODX RegenCache
. The initial version with the ~index.html
check is there to handle MODX-generated URI's that end with a trailing /
. These are written by the plugin as ~index.html
files so they can be served properly by the web server.
set $cache_prefix 'statcache';
if ($http_user_agent = 'MODX RegenCache') {
set $cache_prefix 'no cache';
}
location /modx {
try_files /modx/$cache_prefix$uri~index.html /modx/$cache_prefix$uri $uri $uri/ @modx-rewrite2;
}
location @modx-rewrite2 {
rewrite ^/(.[^/]*)/(.*)$ /$1/index.php?q=$2 last;
}
This instructs nginx to look for a static representation of the requested resource within the statcache/
directory inside your MODX_BASE_PATH before all the standard MODX checks, unless the User Agent is MODX RegenCache
. The initial version with the ~index.html
check is there to handle MODX-generated URI's that end with a trailing /
. These are written by the plugin as ~index.html
files so they can be served properly by the web server.
set $cache_prefix 'statcache';
if ($http_user_agent = 'MODX RegenCache') {
set $cache_prefix 'no cache';
}
location /modx {
try_files /modx/$cache_prefix/$host$uri~index.html /modx/$cache_prefix/$host$uri $uri $uri/ @modx-rewrite2;
}
location @modx-rewrite2 {
rewrite ^/(.[^/]*)/(.*)$ /$1/index.php?q=$2 last;
}
This instructs nginx to look for a static representation of the requested resource in a domain-specific directory within the statcache/
directory inside your MODX_BASE_PATH before all the standard MODX checks, unless the User Agent is MODX RegenCache
. The initial version with the ~index.html
check is there to handle MODX-generated URI's that end with a trailing /
. These are written by the plugin as ~index.html
files so they can be served properly by the web server.
This configuration example supports multi-Context sites using multiple domains or subdomains.
Once the extra is installed and the StaticCache plugin is enabled and generating your static files, you can configure Apache to serve those files first by preceding the standard MODX rules for Apache .htaccess with:
RewriteCond %{USER_AGENT} "MODX RegenCache"
RewriteRule .* - [E=CACHE_PREFIX:no cache]
RewriteCond %{USER_AGENT} !"MODX RegenCache"
RewriteRule .* - [E=CACHE_PREFIX:statcache]
# If MODX is directly in your DOCUMENT_ROOT,
# add this before your MODX Friendly URLs RewriteCond's and RewriteRule...
RewriteCond %{DOCUMENT_ROOT}/%{ENV:CACHE_PREFIX}%{REQUEST_URI}~index.html -f
RewriteRule ^(.*)$ /%{ENV:CACHE_PREFIX}/$1~index.html [L,QSA]
RewriteCond %{DOCUMENT_ROOT}/%{ENV:CACHE_PREFIX}%{REQUEST_URI} -f
RewriteRule ^(.*)$ /%{ENV:CACHE_PREFIX}/$1 [L,QSA]
RewriteCond %{USER_AGENT} "MODX RegenCache"
RewriteRule .* - [E=CACHE_PREFIX:no cache]
RewriteCond %{USER_AGENT} !"MODX RegenCache"
RewriteRule .* - [E=CACHE_PREFIX:statcache]
# If MODX is in a subdirectory of your DOCUMENT_ROOT,
# add this before your MODX Friendly URLs RewriteCond's and RewriteRule...
RewriteCond %{DOCUMENT_ROOT}/modx/%{ENV:CACHE_PREFIX}%{REQUEST_URI}~index.html -f
RewriteRule ^(.*)$ /modx/%{ENV:CACHE_PREFIX}/$1~index.html [L,QSA]
RewriteCond %{DOCUMENT_ROOT}/modx/%{ENV:CACHE_PREFIX}%{REQUEST_URI} -f
RewriteRule ^(.*)$ /modx/%{ENV:CACHE_PREFIX}/$1 [L,QSA]
RewriteCond %{USER_AGENT} "MODX RegenCache"
RewriteRule .* - [E=CACHE_PREFIX:no cache]
RewriteCond %{USER_AGENT} !"MODX RegenCache"
RewriteRule .* - [E=CACHE_PREFIX:statcache]
# If MODX is in a subdirectory of your DOCUMENT_ROOT,
# add this before your MODX Friendly URLs RewriteCond's and RewriteRule...
RewriteCond %{DOCUMENT_ROOT}/modx/%{ENV:CACHE_PREFIX}/%{HTTP_HOST}%{REQUEST_URI}~index.html -f
RewriteRule ^(.*)$ /modx/%{ENV:CACHE_PREFIX}/%{HTTP_HOST}/$1~index.html [L,QSA]
RewriteCond %{DOCUMENT_ROOT}/modx/%{ENV:CACHE_PREFIX}%{REQUEST_URI} -f
RewriteRule ^(.*)$ /modx/%{ENV:CACHE_PREFIX}/%{HTTP_HOST}/$1 [L,QSA]
Both Apache and nginx need to be configured to serve the appropriate file type based on file extension. If you set MODX not to use the .html extension with any text/html
Content Types, make sure you configure the web server to serve the appropriate mime-type for whatever extension you choose.
IMPORTANT: If you do not use any extension, you may need to force the web server to serve text/html as the default mime-type.
Here is an .htaccess
example for forcing text/html by default:
<files *>
ForceType text/html
</files>
This .htaccess
would need to live in the root statcache_path
folder to function.