Skip to content

Instantly share code, notes, and snippets.

@wernersmit
Created September 20, 2017 10:28
Show Gist options
  • Save wernersmit/dd60b2134d803dc437ace342b398220b to your computer and use it in GitHub Desktop.
Save wernersmit/dd60b2134d803dc437ace342b398220b to your computer and use it in GitHub Desktop.
Nginx Microcaching in Plesk

Create /etc/nginx/conf.d/cache.conf with contents:

proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=microcache:5m max_size=1000m;
proxy_cache_key "$host$request_uri$cookie_user";

Add the following to nginx directives for the specific domain:


# Setup var defaults
set $no_cache "";

# If non GET/HEAD, don't cache & mark user as uncacheable for 1 second via cookie
if ($request_method !~ ^(GET|HEAD)$) {
	set $no_cache "1";
}

# Drop no cache cookie if need be
# (for some reason, add_header fails if included in prior if-block)
# if ($no_cache = "1") {
	#    add_header Set-Cookie "_mcnc=1; Max-Age=2; Path=/";
	#    add_header X-Microcachable "0";
# }

# Bypass cache if no-cache cookie is set
if ($http_cookie ~* "_mcnc") {
	set $no_cache "1";
}

# Bypass cache if flag is set
proxy_no_cache $no_cache;
proxy_cache_bypass $no_cache;

# Set cache zone
proxy_cache microcache;

# Set cache key to include identifying components
proxy_cache_key $scheme$host$request_method$request_uri;

# Only cache valid HTTP 200 responses for 1 second
proxy_cache_valid 200 1s;

# Serve from cache if currently refreshing
proxy_cache_use_stale updating;

@rkok
Copy link

rkok commented May 7, 2019

Thank you. Elegant solution!

  1. proxy_cache_key in /etc/nginx/conf.d/cache.conf seems superfluous; I think it can be safely removed
  2. This does not work for files with a Content-Length >= 1024 if "Smart static files processing" is enabled in Plesk. I was unable to figure out exactly why this is, but this is the offending code it adds to the vhost config:
# diff /tmp/nonsmart.conf /tmp/smart.conf
(...)
63a65,69
> 	location /internal-nginx-static-location/ {
> 		alias /var/www/vhosts/mydomain.com/sub.mydomain.conf/;
> 		internal;
> 	}
> 
68a75
> 		proxy_set_header X-Accel-Internal /internal-nginx-static-location;
(...)

In case it helps anyone, here's my own use-case: caching Divi CSS files which are removed while W3 Total Cache still references them in cached pages:

###########################
# Cache Divi assets in nginx so they don't disappear while W3 Total Cache is still full-page-caching them
###########################

# Default: don't cache
set $no_cache "1";

# Only cache Divi ET cache assets
if ($request_uri ~ ".*cache\/et.*") {
        set $no_cache "";
}

# Bypass cache if flag is set
proxy_no_cache $no_cache;
proxy_cache_bypass $no_cache;

proxy_ignore_headers Cache-Control Expires Set-Cookie;
proxy_hide_header Set-Cookie;
proxy_cache microcache;
proxy_cache_revalidate off;
proxy_cache_min_uses 1;
proxy_cache_background_update on;
proxy_cache_lock on;
proxy_buffer_size 8k;
proxy_buffering on;

# Set cache key to include identifying components
proxy_cache_key $scheme$host$request_method$request_uri;

# Cache responses for 30 days
proxy_cache_valid 30d;

add_header x-cache $upstream_cache_status;

#############
# Modified alternative for "Serve static files directly through nginx"
#############

location ~ ^/((?!.*cache\/et.*).*\.(ac3|avi|bmp|bz2|css|cue|dat|doc|docx|dts|eot|exe|flv|gif|gz|htm|html|ico|img|iso|jpeg|jpg|js|mkv|mp3|mp4|mpeg|mpg|ogg|pdf|png|ppt|pptx|qt|rar|rm|svg|swf|tar|tgz|ttf|txt|wav|woff|woff2|xls|xlsx|zip))$ {
	try_files $uri @fallback;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment