0

Mark Croxton helpfully provided this sample .htaccess for using Stash's static cache on Apache servers. Unfortunately, I'm running this particular site on nginx, and I haven't found any good answers on how to accomplish these same rewrite conditions with nginx. I'm hoping someone else has done it already and doesn't mind sharing their solution.

2 Answers 2

1

So I dug into it, and here's what I've come up with. I've tested it extensive on my setup (Laravel Forge), but of course YMMV.

The methodology is a little different than the Apache version. Since we have to check for the existence of the cache file anyway (and we know there won't be a cache file for anything that isn't supposed to be cached), there's no benefit to excluding image files or other file assets from the rewrite conditions.

Essentially, if the cache file exists, it's a GET request, and there isn't a query string that starts with css, ACT, URL, or preview, serve up the cache file.

Put this in the server block (but not within a location block):

# BEGIN STATIC CACHE RULES
set $cache false;

# Disallow direct access of cache directory
location /cache {
    internal;
}

# 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;
}

# Don't serve up cache to any logged-in user
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
2
  • Could you add this to exclude logged in users? if ($http_cookie ~ 'exp_sessionid') { set $cache false; } Jul 16, 2015 at 10:43
  • Yep, just tested it and edited the code block above to include that. Jul 16, 2015 at 17:38
0

This may not be the most helpful answer, but their are conversion tools for htaccess files to Nginx configuration files. See the first answer to this stack answer:

https://stackoverflow.com/questions/5840497/convert-htaccess-to-nginx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.