Skip to content

Memcached setup example

Conor McKnight edited this page Jun 10, 2026 · 3 revisions

To use this script with memcached is very simple / easy.

Spoiler : rewrite_by_lua works with New Nginx Lua versions ONLY
#http block
# you do not need the following line if you are using
# the OpenResty bundle:
# https://github.com/openresty/lua-resty-memcached
lua_package_path "./conf/lua/lua-resty-memcached/lib/?.lua;;";

lua_shared_dict antiddos 70m; #Anti-DDoS shared memory zone to track requests per each unique user
access_by_lua_file conf/lua/anti_ddos_challenge.lua;

server {

	location /test {

		# need to specify the resolver to resolve the hostname
		resolver 8.8.8.8;

		#open rewrite by lua tag
		# for large configurations use
		#rewrite_by_lua_file custom_settings.lua #you can make your configurations as big as you like without hitting limits
		rewrite_by_lua_block {

			local memcached = require "resty.memcached"
			local memc = memcached:new()

			memc:set_timeouts(1000, 1000, 1000) -- 1 sec

			--or connect to a unix domain socket file listened
			--by a memcached server:
			--local ok, err = memc:connect("unix:/path/to/memcached.sock")

			-- connect via ip address directly
			local ok, err = memc:connect("127.0.0.1", 11211)

			if not ok then
				--ngx.say("failed to connect: ", err)
				return
			end

			if localized_global == nil then --if global not exists
				localized_global = {} --define global var that script can read
			end

			local ok, err = memc:set("secret", " enigma")
			if not ok then
				--ngx.say("failed to set secret key: ", err)
				return
			end

			--ngx.say("set result: ", ok)

			local res, err = memc:get("secret")
			if not res then
				--ngx.say("failed to get secret: ", err)
				return
			else
				--result found do something
				localized_global.secret = res --nginx will now set secret key via memcached Database
			end

			if res == ngx.null then
				--ngx.say("secret not found.")
				return
			end

			--ngx.say("secret key: ", res)

			-- put it into the connection pool of size 100,
			-- with 10 seconds max idle time
			local ok, err = memc:set_keepalive(10000, 100)
			if not ok then
				--ngx.say("failed to set keepalive: ", err)
				return
			end

			--or just close the connection right away:
			--local ok, err = memc:close()
			--if not ok then
			--	ngx.say("failed to close: ", err)
			--	return
			--end

		} #end rewrite_by_lua_block

	} #end location block

} #end server block
Spoiler : set_by_lua works with All Old and New Nginx Lua versions
#http block
# you do not need the following line if you are using
# the OpenResty bundle:
# https://github.com/openresty/lua-resty-memcached
lua_package_path "./conf/lua/lua-resty-memcached/lib/?.lua;;";

lua_shared_dict antiddos 70m; #Anti-DDoS shared memory zone to track requests per each unique user
access_by_lua_file conf/lua/anti_ddos_challenge.lua;

server {

	location /test {

		# need to specify the resolver to resolve the hostname
		resolver 8.8.8.8;

		#open set by lua tag
		# for large configurations use
		#set_by_lua_file $localized_global custom_settings.lua #you can make your configurations as big as you like without hitting limits
		set_by_lua $localized_global '

			local load = load
			local localized_global = [[

			local memcached = require "resty.memcached"
			local memc = memcached:new()

			memc:set_timeouts(1000, 1000, 1000) -- 1 sec

			--or connect to a unix domain socket file listened
			--by a memcached server:
			--local ok, err = memc:connect("unix:/path/to/memcached.sock")

			-- connect via ip address directly
			local ok, err = memc:connect("127.0.0.1", 11211)

			if not ok then
				--ngx.say("failed to connect: ", err)
				return
			end

			if localized_global == nil then --if global not exists
				localized_global = {} --define global var that script can read
			end

			local ok, err = memc:set("secret", " enigma")
			if not ok then
				--ngx.say("failed to set secret key: ", err)
				return
			end

			--ngx.say("set result: ", ok)

			local res, err = memc:get("secret")
			if not res then
				--ngx.say("failed to get secret: ", err)
				return
			else
				--result found do something
				localized_global.secret = res --nginx will now set secret key via memcached Database
			end

			if res == ngx.null then
				--ngx.say("secret not found.")
				return
			end

			--ngx.say("secret key: ", res)

			-- put it into the connection pool of size 100,
			-- with 10 seconds max idle time
			local ok, err = memc:set_keepalive(10000, 100)
			if not ok then
				--ngx.say("failed to set keepalive: ", err)
				return
			end

			--or just close the connection right away:
			--local ok, err = memc:close()
			--if not ok then
			--	ngx.say("failed to close: ", err)
			--	return
			--end
			
			]]
			return load(localized_global,"=localized_global_memcached1")() --loadstring to run lua code from our set ngx variable

		'; #end set_by_lua tag

	} #end location block

} #end server block

Well done you are on your way to building your very own CDN just like Cloudflare.

You can find more examples on how to Build your own control panel and database here https://github.com/openresty/lua-resty-memcached#synopsis

Clone this wiki locally