Skip to content

Website Caching Nginx Lua proxy_cache and fastcgi_cache

Conor McKnight edited this page Sep 24, 2025 · 14 revisions

I built in a way to allow you to cache webpages to improve your sites performance and speed what will also help search engines see your site speed is fast.

https://github.com/C0nw0nk/Nginx-Lua-Anti-DDoS/blob/master/lua/anti_ddos_challenge.lua#L386

You can bypass the cache depending on cookies or urls and to cache content if bigger than or smaller than a certain size there are many features.

Make sure for any Cache Zone you create you also add to your nginx config the shared memory zone it requires.

http {
# Nginx config HTTP Block Caching shared memory zones
lua_shared_dict html_cache 10m; #HTML pages cache
lua_shared_dict mp4_cache 300m; #video mp4 cache
}
Expand view
localized.content_cache = {
	{
		".*", --regex match any site / path
		"text/html", --content-type valid types are text/css text/javascript
		--lua_shared_dict html_cache 10m; #HTML pages cache
		localized.ngx.shared.html_cache, --shared cache zone to use or empty string to not use "" lua_shared_dict html_cache 10m; #HTML pages cache
		60, --ttl for cache or ""
		1, --enable logging 1 to enable 0 to disable
		{200,206,}, --response status codes to cache
		{"GET",}, --request method to cache
		{ --bypass cache on cookie
			{
				"logged_in", --cookie name regex ".*" for any cookie
				"1", --cookie value ".*" for any value
				0, --0 guest user cache only 1 both guest and logged in user cache useful if logged_in cookie is present then cache key will include cookies
			},
			--{"name1","value1",0,},
		}, --bypass cache on cookie
		{"/login.html","/administrator","/admin*.$",}, --bypass cache urls
		1, --Send cache status header X-Cache-Status: HIT, X-Cache-Status: MISS
		1, --if serving from cache or updating cache page remove cookie headers (for dynamic sites you should do this to stay as guest only cookie headers will be sent on bypass pages)
		localized.request_uri, --url to use you can do "/index.html", as an example localized.request_uri is best.
		false, --true to use lua resty.http library if exist if you set this to true you can change localized.request_uri above to "https://www.google.com/", as an example.
		{ --Content Modifier Modification/Minification / Minify HTML output
			--Usage :
			--Regex, Replacement
			--Text, Replacement
			--You can use this to alter contents of the page output.
			--Example :
			--{"replace me", " with me! ",},
			--{"</head>", "<script type='text/javascript' src='../jquery.min.js'></script></head>",} --inject javascript into html page
			--{"<!--[^>]-->", "",}, --remove nulled out html example !! I DO NOT RECOMMEND REMOVING COMMENTS, THIS COULD BREAK YOUR ENTIRE WEBSITE FOR OLD BROWSERS, BE AWARE
			--{"(//[^.*]*.\n)", "",}, -- Example: this //will remove //comments (result: this remove)
			--{"(/%*[^*]*%*/)", "",}, -- Example: this /*will*/ remove /*comments*/ (result: this remove)
			--{"<style>(.*)%/%*(.*)%*%/(.*)</style>", "<style>%1%3</style>",},
			--{"<script>(.*)%/%*(.*)%*%/(.*)</script>", "<script>%1%3</script>",},
			--{"%s%s+", "",}, --remove blank characters from html
			--{"[ \t]+$", "",}, --remove break lines (execution order of regex matters keep this last)
			--{"<!%-%-[^%[]-->", "",},
			--{"%s%s+", " ",},
			--{"\n\n*", " ",},
			--{"\n*$", ""},
		},
		"", --1e+6, --Maximum content size to cache in bytes 1e+6 = 1MB content larger than this wont be cached empty string "" to skip
		"", --Minimum content size to cache in bytes content smaller than this wont be cached empty string "" to skip
		{"content-type","content-range","content-length","etag","last-modified","set-cookie",}, --headers you can use this to specify what headers you want to keep on your cache HIT/UPDATING output
	},
	{
		".*", --regex match any site / path
		"video/mp4", --content-type valid types are text/css text/javascript
		--lua_shared_dict mp4_cache 300m; #video mp4 cache
		localized.ngx.shared.mp4_cache, --shared cache zone to use or empty string to not use "" lua_shared_dict mp4_cache 300m; #video mp4 cache
		60, --ttl for cache or ""
		1, --enable logging 1 to enable 0 to disable
		{200,206,}, --response status codes to cache
		{"GET",}, --request method to cache
		{ --bypass cache on cookie
			{
				"logged_in", --cookie name ".*" for any cookie
				"1", --cookie value ".*" for any value
				0, --0 guest user cache only 1 both guest and logged in user cache useful if logged_in cookie is present then cache key will include cookies
			},
			--{"name1","value1",0,},
		}, --bypass cache on cookie
		{"/login.html","/administrator","/admin*.$",}, --bypass cache urls
		1, --Send cache status header X-Cache-Status: HIT, X-Cache-Status: MISS
		1, --if serving from cache or updating cache page remove cookie headers (for dynamic sites you should do this to stay as guest only cookie headers will be sent on bypass pages)
		localized.request_uri, --url to use you can do "/index.html", as an example localized.request_uri is best.
		false, --true to use lua resty.http library if exist if you set this to true you can change localized.request_uri above to "https://www.google.com/", as an example.
		"", --content modified not needed for this format
		4e+7, --Maximum content size to cache in bytes 1e+6 = 1MB, 1e+7 = 10MB, 1e+8 = 100MB, 1e+9 = 1GB content larger than this wont be cached empty string "" to skip
		200000, --200kb --Minimum content size to cache in bytes content smaller than this wont be cached empty string "" to skip
		{"content-type","content-range","content-length","etag","last-modified","set-cookie",}, --headers you can use this to specify what headers you want to keep on your cache HIT/UPDATING output
	},
}

How can i setup Joomla CMS with fastcgi_cache or proxy_cache

Joomla's Nginx config : https://docs.joomla.org/Nginx

There are two ways to do this Method #1 or Method #2

Expand Method #1 view

Change the URL bypass to match this so on these Joomla URLs / Pages they wont be cached allowing users to login and register still.

Expand view
{
"/login",
"/component/users/login",
"/component/users/reset",
"/component/users/remind",
"/register",
"/component/users/register",
"/component/users/registration",
"/administrator",
"/admin*.$",
--NON SEF / SEO URLs not optimized or search engine friendly or optimized joomla urls
"index.php?option=com_users&view=registration",
"index.php?option=com_users&view=login",
"index.php?option=com_users&view=reset",
"index.php?option=com_users&view=remind",
"/user/login?view=reset",
"/user/login?view=registration",
"/user/login?view=remind",
},

Set your cookie bypass

Expand view
{ --bypass cache on cookie
	{
		".*", --cookie name regex ".*" for any cookie
		".*", --cookie value ".*" for any value
		1, --0 guest user cache only 1 both guest and logged in user cache useful if logged_in cookie is present then cache key will include cookies
	},
}, --bypass cache on cookie
Expand Method #2 view

Change the URL bypass to match this so on these Joomla URLs / Pages they wont be cached allowing users to login and register still.

Expand view
{
"/login",
"/component/users/login",
"/component/users/reset",
"/component/users/remind",
"/register",
"/component/users/register",
"/component/users/registration",
"/administrator",
"/admin*.$",
--NON SEF / SEO URLs not optimized or search engine friendly or optimized joomla urls
"index.php?option=com_users&view=registration",
"index.php?option=com_users&view=login",
"index.php?option=com_users&view=reset",
"index.php?option=com_users&view=remind",
"/user/login?view=reset",
"/user/login?view=registration",
"/user/login?view=remind",
},

Turn on the cache above and then edit your root /index.php to reflect this

Expand view
<?php
//JOOMLA INDEX.PHP ETC

/**
 * Constant that is checked in included files to prevent direct access.
 * define() is used rather than "const" to not error for PHP 5.2 and lower
 */
define('_JEXEC', 1);

// Run the application - All executable code should be triggered through this file
require_once __DIR__ . '/includes/app.php';

$user =& JFactory::getUser();
if($user->id!=0){
//user is logged in

$cookie_name = "logged_in";
$cookie_value = "1";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day

} else {

//User is not logged in is a guest
$cookie_name = "logged_in";
$cookie_value = "0";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
}


Set your cookie bypass

Expand view
{ --bypass cache on cookie
	{
		"logged_in", --cookie name regex ".*" for any cookie
		"1", --cookie value ".*" for any value
		1, --0 guest user cache only 1 both guest and logged in user cache useful if logged_in cookie is present then cache key will include cookies
	},
}, --bypass cache on cookie

How can i setup a PHP website with fastcgi_cache or proxy_cache and Lua caching

There are two ways to do this Method #1 or Method #2

Expand Method #1 view

Change the URL bypass to match this so on these URLs / Pages they wont be cached allowing users to login and register still.

Expand view
{
"/login.php",
"/register.php",
"/administrator",
"/admin*.$",
},

Set your cookie bypass

Expand view
{ --bypass cache on cookie
	{
		".*", --cookie name regex ".*" for any cookie
		".*", --cookie value ".*" for any value
		1, --0 guest user cache only 1 both guest and logged in user cache useful if logged_in cookie is present then cache key will include cookies
	},
}, --bypass cache on cookie
Expand Method #2 view

Change the URL bypass to match this so on these URLs / Pages they wont be cached allowing users to login and register still.

Expand view
{
"/login.php",
"/register.php",
"/administrator",
"/admin*.$",
},

As a example with php you can do this and STATIC pages ARE cached and DYNAMIC content for logged in users will NOT be cached.

Expand view
<?php
//Just change the code for your CMS / APP Joomla / Drupal etc have plenty of examples.
if($user->guest = 1){
//User in not logged in is a guest
$cookie_name = "logged_in";
$cookie_value = "0";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
}
else
{
//User is logged in
$cookie_name = "logged_in";
$cookie_value = "1";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
}
?>

Set your cookie bypass

Expand view
{ --bypass cache on cookie
	{
		"logged_in", --cookie name regex ".*" for any cookie
		"1", --cookie value ".*" for any value
		1, --0 guest user cache only 1 both guest and logged in user cache useful if logged_in cookie is present then cache key will include cookies
	},
}, --bypass cache on cookie

How can i setup MyBB forum with fastcgi_cache or proxy_cache and Lua caching

Set your bypass cache urls to

Expand view
{
"/member.php.*", --allow users to register and login
}

Set your cookie bypass

Expand view
{ --bypass cache on cookie
	{
		".*", --cookie name regex ".*" for any cookie
		".*", --cookie value ".*" for any value
		1, --0 guest user cache only 1 both guest and logged in user cache useful if logged_in cookie is present then cache key will include cookies
	},
}, --bypass cache on cookie

How can i setup vBulletin forum with fastcgi_cache or proxy_cache and Lua caching

Set your bypass cache urls to

Expand view
{
"/auth/ajax-login.*", --allow users to register and login
"/register.*", --registration page
"/lostpw.*", --forgot password page
"/auth/", --POST data goes to auth urls
}

Set your cookie bypass

Expand view
{ --bypass cache on cookie
	{
		".*", --cookie name regex ".*" for any cookie
		".*", --cookie value ".*" for any value
		1, --0 guest user cache only 1 both guest and logged in user cache useful if logged_in cookie is present then cache key will include cookies
	},
}, --bypass cache on cookie

How can i setup Wordpress forum with fastcgi_cache or proxy_cache and Lua caching

Set your bypass cache urls to

Expand view
{
"/wp-login.php.*", --allow users to register and login
}

Set your cookie bypass

Expand view
{ --bypass cache on cookie
	{
		".*", --cookie name regex ".*" for any cookie
		".*", --cookie value ".*" for any value
		0, --0 guest user cache only 1 both guest and logged in user cache useful if logged_in cookie is present then cache key will include cookies
	},
}, --bypass cache on cookie

Clone this wiki locally