Skip to content

Anti‐DDoS Stats and Metrics

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

For those who want to view and see statistics of the Anti-DDoS script how many requests blocked how many concurrent requests etc. I made this as an example you can use for your dashboard to read as a application/json api output.

Create a location in your nginx config nginx.conf

You can access it via http://[::1]/ddos_stats or http://localhost/ddos_stats

#http block
# you do not need the following line if you are using
# the ngx_openresty bundle:
# https://github.com/openresty/lua-cjson
lua_package_path "./conf/lua/lua-cjson/lua/cjson/?.lua;;";

server {

	location = /ddos_stats {

		#open content by lua tag
		# for large configurations use
		#content_by_lua_file custom_content.lua #you can make your configurations as big as you like without hitting limits
		content_by_lua_block {
			local metrics = ngx.shared.antiddos
			local ddos_counter = ngx.shared.antiddos --ngx.shared.ddos_counter

			local json = require "cjson"
			local function get_metrics()
				if not metrics then
					return {error = "Metrics not available"}
				end
				if not ddos_counter then
					return {error = "Metrics not available"}
				end

				local json_table = {}
				local metrics_keys_table = metrics:get_keys(0)
				for key, value in next, metrics_keys_table do
					json_table[#json_table+1] = "ip - " .. value .. " - request count " .. metrics:get(value)
				end
			
				return {
					requests_blocked = ddos_counter:get("blocked_ip") or 0,
					current_timestamp = ngx.time(),
					total_requests_per_user = json_table,
					concurrent_users = #metrics_keys_table or 0,
				}
			end

			ngx.header.content_type = "application/json"
			ngx.status = 200
			ngx.say(json.encode(get_metrics()))
			ngx.exit(200)

		} #end content_by_lua_block
	
	} #end location block

} #end server block

Clone this wiki locally