This is a small guide for caching static content for visitors that are not logged in. Logged in users "BYPASS" the cache entirely and this guide does not cache for users who are logged in.
I have done some limited testing with good results (predictable BYPASS and HIT/EXPIRED), and completed a "load" test that went well. As expected, there is a delay between posts submitted and being shown for visitors. I've been unable to produce any issues like inability to log in / out, being logged in as other users, etc.
I'm currently testing this configuration on Devflarum. Feel free to try find any critical issues with this there.
As always, backup files before adjusting them
In your nginx.conf file (/etc/nginx/nginx.conf) add this somewhere in http { }
fastcgi_cache_path /etc/nginx/cache levels=1:2 keys_zone=yourcache:20m max_size=500m inactive=1d;
In your server block for your flarum install:
Change this: (adjust as needed)
location ~* \.php$ {
fastcgi_split_path_info ^(.+.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTP_PROXY ""; # Fix for https://httpoxy.org/ vulnerability
fastcgi_index index.php;
}
to this:
set $fastcgi_skipcache 0;
if ($http_cookie ~ "flarum_remember") {
set $fastcgi_skipcache 1;
}
location ~* \.php$ {
fastcgi_split_path_info ^(.+.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_index index.php;
fastcgi_cache yourcache;
fastcgi_cache_bypass $fastcgi_skipcache;
fastcgi_no_cache $fastcgi_skipcache;
fastcgi_cache_key $proxy_host$request_uri;
fastcgi_cache_methods GET HEAD;
fastcgi_cache_valid 200 301 302 1m;
fastcgi_cache_revalidate on;
fastcgi_cache_lock on;
fastcgi_cache_use_stale updating error timeout invalid_header http_500;
fastcgi_pass_header Set-Cookie;
fastcgi_pass_header Cookie;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
}