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

c0nw0nk nginx-forum at
Wed Jul 11 14:48:01 UTC 2018

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
    return s

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
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

    return false

--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.
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
		--print 'Nope'
--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_param REQUEST_URI $cache_request_uri; #need to make sure that web
application URI has been modified by Lua

Posted at Nginx Forum:,280500,280500#msg-280500

More information about the nginx mailing list