Best Way To Remove Whole URL Query String

359 views
Skip to first unread message

Hadi Abbasi

unread,
Apr 24, 2021, 6:08:00 AM4/24/21
to openresty-en

hello
when I wanna send request to origin server, I wanna try to remove whole url query string using openresty .
the first thing I can do is redirection. but it's not suggested way (seo problems)!
I thought I can remove url query string by below code but it's not working:
if  string.find(ngx.var.request_uri,"?",1, true) then
            local args = ngx.req.get_uri_args()
            for k , v in pairs(args) do
                args[k]=nil;
                --ngx.log(ngx.DEBUG,tostring(k) .. " --> " .. tostring(v))
            end
             ngx.req.set_uri_args(args)
            --return ngx.redirect(ngx.var.scheme .. "://" .. ngx.var.host..uri_without_query_str,  301)
        end
ngx.log(ngx.DEBUG,"============> " .. tostring(ngx.var.request_uri))
--nothing is changed
is there any way for me or the only way is redirection?
Best,
Hadi

Kshitij Joshi

unread,
Apr 24, 2021, 2:00:21 PM4/24/21
to openresty-en
Simply use 
ngx.var.uri. It does not contain any query strings.

Andrei

unread,
Apr 24, 2021, 2:25:36 PM4/24/21
to openre...@googlegroups.com
Blindly stripping query parameters from the requests going to the origin server is a horrible idea and has no positive bearing on SEO. On the contrary, the backend app will no longer be able to track utm_ params and unless all your Internal requests use post, I'm willing to bet some query params will be needed. If you're doing this to improve caching, there's always a better way.

--
You received this message because you are subscribed to the Google Groups "openresty-en" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openresty-en...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/openresty-en/60abd6f5-7f34-4159-bdaa-c6dc4af06665n%40googlegroups.com.

Peter Booth

unread,
Apr 24, 2021, 4:21:49 PM4/24/21
to openre...@googlegroups.com
Yes.

Open testy is a super powerful tool. The important question most if the time “what are you wanting to achieve, and why?” 
There are are often a dozen bad ways and only a couple of ideal ways to solve problems with Openresty. 

I haven’t yet found a web problem that openresty couldn’t help with, so I am a fanboy.

Sent from my iPhone

On Apr 24, 2021, at 2:25 PM, Andrei <lag...@gmail.com> wrote:



Hadi Abbasi

unread,
Apr 24, 2021, 5:21:35 PM4/24/21
to openresty-en

 thanks a lot..
but I simply can remove query string in a simple function...
the problem is that openresty has its builtin mechanism and it will send full request by query string to the origin server!

Kshitij Joshi

unread,
Apr 24, 2021, 5:27:14 PM4/24/21
to openresty-en

How are you sending requests to the backend server? By proxy_pass?
By default vars like ngx.var.uri,  ngx.var.args etc are immutable. 
It's to generate local variables by copying those variables and then modifying them. 

Hadi Abbasi

unread,
Apr 24, 2021, 5:36:56 PM4/24/21
to openresty-en
some of my customers wants to ignore sending query string as they need maximum  use of valid caches and ignore ddos and ... attacks with random query strings. their site doesn't use query string.
so all I wanna say and do is to ignore sending query string to their origin server and also  ignore it in cacheid generation! in other words, I wanna remove full url query string of clients request for those customers!
but openresty builtin mechanism doesn't allow me to do that but just by redirection!
even I can't do that using:


if  string.find(ngx.var.request_uri,"?",1, true) then
            local args = ngx.req.get_uri_args()
            for k , v in pairs(args) do
                args[k]=nil;
                --ngx.log(ngx.DEBUG,tostring(k) .. " --> " .. tostring(v))
            end
             ngx.req.set_uri_args(args)
            --return ngx.redirect(ngx.var.scheme .. "://" .. ngx.var.host..uri_without_query_str,  301)
        end
ngx.log(ngx.DEBUG,"============> " .. tostring(ngx.var.request_uri))
--nothing is changed



Hadi Abbasi

unread,
Apr 24, 2021, 5:37:43 PM4/24/21
to openresty-en

proxy_pass

Kshitij Joshi

unread,
Apr 24, 2021, 5:45:30 PM4/24/21
to openresty-en
What are you using to cache?

You could simply proxy_pass to
proxy_pass $host$uri.

I'm sure there should be some way to set the cacheKey to $host$uri.

Andrei

unread,
Apr 24, 2021, 5:54:53 PM4/24/21
to openre...@googlegroups.com
Yup, just tac on $uri and look over the docs for info on how it can change across locations/rewrites

--
You received this message because you are subscribed to the Google Groups "openresty-en" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openresty-en...@googlegroups.com.

Hadi Abbasi

unread,
Apr 24, 2021, 5:59:37 PM4/24/21
to openresty-en
lag...@gmail.com
kshiti...@gmail.com

yep, I will try it!
if it still sends query_string, I will continue  & will ask again!
thanks a lot...
Best,
Hadi

Andrei

unread,
Apr 24, 2021, 6:03:27 PM4/24/21
to openre...@googlegroups.com
Just read the docs on proxy_pass, $uri, $request_uri

Hadi Abbasi

unread,
Apr 24, 2021, 6:14:38 PM4/24/21
to openresty-en

Sure..
thanks...
All The Best,
Hadi
Reply all
Reply to author
Forward
0 new messages