Skip to content

Static cache setup

Mark Croxton edited this page Mar 20, 2017 · 23 revisions

Follow these steps to setup static caching:

  1. Create a cache directory (e.g. 'static_cache') in your public webroot and CHMOD to 777 (or CHOWN to the user PHP runs as).

  2. Set stash_static_basepath to the full path of your newly created cache directory in your config. Stash will read and write to this directory so be very careful to specify the correct path, especially if your PHP installation can write anywhere in your filesystem (e.g. MAMP).

  3. Set stash_static_url to the absolute or root-relative URL of the cache directory in your config.

  4. Set stash_static_cache_enabled to TRUE in your config.

  5. If you are using Mustash, visit the 'Stash cache rewrite rules' page in Settings to view an auto-generated htaccess file that you can copy and paste into your public webroot. Alternatively, edit the .htaccess file below with the path and url to your static cache directory, and save it in your public webroot. Replace '[site_id]' with the id number of your site. If you are using MSM, create an .htaccess file for each site and save it in the site's directory.

.htaccess file

<IfModule mod_rewrite.c>
 
RewriteEngine on    

#################################################################################
# START STASH STATIC CACHE

# Exclude image files
RewriteCond $1 !\.(gif|jpe?g|png|css|js|ico)$ [NC]

# We only want GET requests
RewriteCond %{REQUEST_METHOD} ^GET

# Exclude ACT
RewriteCond %{HTTP:ACT} !=^$

# Exclude AJAX
RewriteCond %{HTTP:X-Requested-With} !=XMLHttpRequest

# Exclude when css, ACT, URL or 'preview' in query string
RewriteCond %{QUERY_STRING} !^(css|ACT|URL|preview)

# Uncomment this if you want to disable static caching for logged-in users
#RewriteCond %{HTTP_COOKIE} !exp_sessionid [NC]

# Remove index.php from conditions
RewriteCond $1 ^(index.php/)*(.*)(/*)$

# Check if cached index.html exists
RewriteCond %{DOCUMENT_ROOT}/static_cache/[site_id]/$2/index.html (.*\.(.*))$
RewriteCond %1 -f

# Rewrite to the cached page
RewriteRule ^(index.php/*)*(.*)(/*) /static_cache/[site_id]/$2/index.%2 [L]

# END STASH STATIC CACHE RULES
#################################################################################

# -------------------------------------------------------------------------------
# Officially supported method to remove index.php from ExpressionEngine URLs
# See: http://ellislab.com/expressionengine/user-guide/urls/remove_index.php.html
# -------------------------------------------------------------------------------

RewriteBase /

# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]

# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]

</IfModule>

POST requests not working?

you may have trouble in some environments getting the static cache rewrite rule to ignore POSTed requests, such as those generated by a Low Search form. Please see this thread for possible resolutions: http://expressionengine.stackexchange.com/questions/18550/stash-static-caching-not-ignoring-posts-like-i-think-it-should

Every page request shows the homepage?

In some environments you may find that static caching the homepage causes all other urls to start rewriting to the homepage. See this article for a solution: Static-caching-homepage-in-FastCGI-environments

Nginx version

Drop this anywhere in your main location block:

# BEGIN STATIC CACHE RULES
set $cache false;

# Only serve up cache if this cache file exists
if (-f $document_root/static_cache/[site_id]$request_uri/index.html) {
    set $cache true;
}

# Don't serve up cache if not GET request
if ($request_method != GET) {
    set $cache false;
}

# Don't serve up cache if any of these arg names start the query string
if ($args ~* ^(css|ACT|URL|preview)) {
    set $cache false;
}

# Uncomment this to disable static caching for logged-in users
# if ($http_cookie ~ 'exp_sessionid') {
#     set $cache false;
# }

# If cache is still enabled, go!
if ($cache = true) {
    rewrite ^(.*)$ /cache/1$request_uri/index.html break;
}
# END STATIC CACHE RULES

(source: http://expressionengine.stackexchange.com/questions/32314/how-can-i-set-up-nginx-to-serve-stashs-static-cache)

Clone this wiki locally