Welcome! Log In Create A New Profile

Advanced

Nginx Lua Caching and removing unwanted Arguements for higher HIT ratio issue

July 11, 2018 10:48AM
So my issue is mostly directed towards Yichun Zhang (agentzh) if he is still active here. I hope so.


My problem is I am trying to increase my Cache HIT ratio by removing arguments from the URL that are fake / unwanted and order the arguments in a alphabetical (same order every time) for a higher Cache HIT ratio.

Here is my code.

location ~ \.php$ { ##within the PHP location block

## Create fastcgi_param vars

# remove duplicate values like index.php?variable=value&&&&&etc from URL's for higher cache hit ratio
set_by_lua_block $cache_request_uri {
local function fix_url(s,C)
for c in C:gmatch(".") do
s=s:gsub(c.."+",c)
end
return s
end

return string.lower(fix_url(ngx.var.request_uri, "+/=&?;~*@$,:"))
}

#TODO : Order request body variables so they are the same order for higher cache hit ratio
set_by_lua_block $cache_request_body { return ngx.var.request_body }

# Order Arguement variables for higher cache hit ratio and remove any custom defined arguements that users may be using to bypass cache in an attempt of DoS.
set_by_lua_block $cache_request_uri {
ngx.log(ngx.ERR, "before error: ", ngx.var.request_uri)
ngx.log(ngx.ERR, "test: ", ngx.var.uri)
local function has_value (tab, val)
for index, value in ipairs(tab) do
-- We grab the first index of our sub-table instead
if string.lower(value) == string.lower(val) then
return true
end
end

return false
end

--Anti-DDoS and Remove arguements from URLs
local args = ngx.req.get_uri_args()
local remove_args_table = { --table of blacklisted arguement to remove from url to stop DoS and increase Cache HIT ratio.
"rnd",
"rand",
"random",
"ddos",
"dddddooooossss",
"randomz",
}
for key,value in pairs(args) do
if has_value(remove_args_table, value) then
--print 'Yep'
--print(value .. " ")
ngx.log(ngx.ERR, "error: ", key .. " | " .. value)
args[key] = nil --remove the arguement from the args table
else
--print 'Nope'
end
end
--ngx.req.set_uri_args(args)
--for k,v in pairs(args) do --[[print(k,v)]] ngx.log(ngx.ERR, "error: ", k .. " | " .. v) end
ngx.log(ngx.ERR, "after error: ", ngx.var.request_uri)
--return ngx.req.set_uri_args(args)
return ngx.var.uri .. args
--Anti-DDoS and Remove arguements from URLs
}

fastcgi_cache microcache;
fastcgi_cache_key "$scheme$host$cache_request_uri$request_method$cache_request_body";
fastcgi_param REQUEST_URI $cache_request_uri; #need to make sure that web application URI has been modified by Lua

http://www.networkflare.com/
Subject Author Posted

Nginx Lua Caching and removing unwanted Arguements for higher HIT ratio issue

c0nw0nk July 11, 2018 10:48AM



Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 67
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 254 on July 05, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready