set $new_request_uri '';
ngx.var.new_request_uri = ngx.var.request_uri --make our empty var contents the same as the request URI contents to then be modified.
fastcgi_cache_key "$session_id_value$scheme$host$request_uri$request_method$cache_request_body";
fastcgi_cache_key "$session_id_value$scheme$host$new_request_uri$request_method$cache_request_body";
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param REQUEST_URI $new_request_uri;
Do you know what directive i should use because of the execution order of things some *_lua directives execute before others.
Can you help with the Lua code to modify and orde ngx.var.new_request_uri
local args_table = ngx.req.get_uri_args
local args_name = {}
for name, _ in pairs(args_table) do
table.insert(args_name, name) -- maybe you would like incase-sensitive.
end
table.sort(args_name)
local newargs = {}
for _, name in ipairs(args_name) do
table.insert(newargs, name .. "=" .. args_table[name])
end
ngx.var.args = table.concat(newargs, "&")
?
” if a request line has arguments,
or an empty string otherwise
location = /test_GET_args {
content_by_lua_block {
local args = ngx.req.get_uri_args()
for key, val in pairs(args) do
if type(val) == "table" then
ngx.say(key, ": ", table.concat(val, ", "))
else
ngx.say(key, ": ", val)
end
end
}
}
location = /test_POST_args {
content_by_lua_block {
ngx.req.read_body()
local args, err = ngx.req.get_post_args()
if not args then
ngx.say("failed to get post args: ", err)
return
end
for key, val in pairs(args) do
if type(val) == "table" then
ngx.say(key, ": ", table.concat(val, ", "))
else
ngx.say(key, ": ", val)
end
end
}
}
ngx.req.get_uri_args
ngx.req.get_uri_args()
location = /test_GET_args {
content_by_lua_block {
local args_table = ngx.req.get_uri_args()
local args_name = {}
for key, val in pairs(args_table) do
if type(val) == "table" then
--ngx.say(key, ": ", table.concat(val, ", "))
else
--ngx.say(key, ": ", val)
table.insert(args_name, name)
end
end
table.sort(args_name)
local newargs = {}
for _, name in ipairs(args_name) do
table.insert(newargs, name .. "=" .. args_table[name])
end
local output = table.concat(newargs, "&")
if output then
ngx.say(" FINISH " .. output .. "")
ngx.var.args = output --set the args to be the output
end
--Check the args order has been optimized
local args_table = ngx.req.get_uri_args()
for key, val in pairs(args_table) do
if type(val) == "table" then
ngx.say(key, ": ", table.concat(val, ", "))
else
ngx.say(key, ": ", val)
end
end
}
}
/test_GET_args?m=hello&a=3&b=42&c=1&z=2&y=4&x=5
/test_GET_args?a=3&b=42&c=1&m=hello&x=5&y=4&z=2
ngx.say(" FINISH " .. output .. "")
And the following Check should output everything that has been ordered but does not output anything...
--Check the args order has been optimized
local args_table = ngx.req.get_uri_args()
for key, val in pairs(args_table) do
if type(val) == "table" then
ngx.say(key, ": ", table.concat(val, ", "))
else
ngx.say(key, ": ", val)
end
end
I don't know what is wrong because there are no errors anymore...
for key, val in pairs(args_table) do
if type(val) == "table" then
--ngx.say(key, ": ", table.concat(val, ", "))
else
--ngx.say(key, ": ", val)
table.insert(args_name, name) -- table.insert(args_name, key)
end
end
location = /testargs {
content_by_lua_block {
local args_table = ngx.req.get_uri_args()
local args_name = {}
for name, value in pairs(args_table) do
if type(value) == "table" then
--ngx.say(name, ": ", table.concat(value, ", "))
--This needs fixing incase multiple arguements share the same name. (Example multiple of the same arguement name with different values)
--&argument1=cake&arguement1=cheese&arguement1=frog
else
table.insert(args_name, name) -- maybe you would like incase-sensitive.
end
end
table.sort(args_name) --Sort the table into order
local newargs = {}
for _, name in ipairs(args_name) do
table.insert(newargs, name .. "=" .. args_table[name])
end
local output = table.concat(newargs, "&")
if output then
ngx.say(" FINISH " .. output .. " END ")
ngx.var.args = output --set the args to be the output
end
--Check the args order has been optimized
local args_table = ngx.req.get_uri_args()
for key, val in pairs(args_table) do
if type(val) == "table" then
ngx.say(key, ": ", table.concat(val, ", "))
else
ngx.say(key, ": ", val)
end
end
}
}
localhost/test_GET_args?lol=1&lol2=lollypop&argument1=cake&arguement1=cheese&arguement1=frog
Output :argument1=cake&lol=1&lol2=lollypop
if type(value) == "table" then
else
argument1=cake&arguement1=cheese&arguement1=frog
&lol=1&lol2=lollypop
local function sort_args(input)
local args_table = input
local args_name = {}
for name, value in pairs(args_table) do
if type(value) == "table" then
--ngx.say(name, ": ", table.concat(value, ", "))
--This needs fixing incase multiple arguements share the same name. (Example multiple of the same arguement name with different values)
--&argument1=cake&arguement1=cheese&arguement1=frog
else
table.insert(args_name, name) -- maybe you would like incase-sensitive.
end
end
table.sort(args_name) --Sort the table into
order
local newargs = {}
for _, name in ipairs(args_name) do
table.insert(newargs, name .. "=" .. args_table[name])
end
local output = table.concat(newargs, "&")
return output --set the args to be the output
end
--GET requests
if ngx.HTTP_GET then
local get_uri_args = ngx.req.get_uri_args()
ngx.var.args = sort_args(get_uri_args)
ngx.say(" GET : FINISH " .. ngx.var.args .. " END ")
end
--END GET requests
--POST requests
local function sort_POST_body(input)
end
if ngx.HTTP_POST then
ngx.req.read_body()
local get_POST_args, POST_args_err = ngx.req.get_post_args()
if not get_POST_args then
ngx.say("failed to get post args: ", POST_args_err)
return
end
if get_POST_args then --Sort the URL args
ngx.var.args = sort_args(get_POST_args)
ngx.say(" POST : FINISH " .. ngx.var.args .. " END ")
end
--Sort the BODY args
end
--END POST requests
curl -X POST -d "foo=bar&bar=baz&bar=blah&assemble=put" http://localhost:80/test_GET_args
curl -H "Content-Type: application/json" -X POST -d "{"username":"xyz","password":"xyz"}" http://localhost:80/test_GET_args
-- maybe you would like incase-sensitive.
"table.insert(args_name, string.lower(name)) -- maybe you would like incase-sensitive.
lua_args_sort.lua:26: attempt to concatenate a nil value
##HTTP BLOCK
map $request_body $cache_request_body {
default $request_body;
}
map $request_uri $cache_request_uri {
default $request_uri;
}
##END HTTP BLOCK
##etc all default Nginx stuff
##INSIDE SERVER BLOCK
# Support Clean (aka Search Engine Friendly) URLs | https://docs.joomla.org/Nginx#Configure_Nginx
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
#rewrite_by_lua_block { ##tried this one too also did not work
access_by_lua_block {
local function sort_args(input)
local args_table = input
local args_name = {}
local newargs = {}
for name, value in pairs(args_table) do
if type(value) == "table" then
for k, v in pairs(value) do
table.insert(newargs, name .. "=" .. value[k])
end
else
table.insert(args_name, name) -- maybe you would like incase-sensitive.
end
end
for _, name in ipairs(args_name) do
table.insert(newargs, name .. "=" .. args_table[name])
end
table.sort(newargs) --Sort the table into order
local output = table.concat(newargs, "&")
return output --set the args to be the output
end
--GET requests
--if ngx.HTTP_GET then
local get_uri_args = ngx.req.get_uri_args()
local args_output = sort_args(get_uri_args)
local uri = (ngx.var.uri or "")
local is_args = (ngx.var.is_args or "")
local args = (args_output or "")
--Set the cache URI key
ngx.var.cache_request_uri = string.lower(uri .. is_args .. args) --make all lowercase for a higher cache hit ratio
--ngx.exec(ngx.var.cache_request_uri) --nulled this out because this caused the following error | rewrite or internal redirection cycle while internally redirecting to "/index.php",
--end
--END GET requests
--]]
} #END LUA BLOCK
#etc fastcgi caching settings and key
fastcgi_cache_key "$scheme$host$cache_request_uri$request_method$cache_request_body";
#fastcgi stuff
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi.conf;
} #END PHP BLOCK
##END INSIDE SERVER BLOCK
/index.php?option=com_details
/details
/details&help=me
/details&contact=page