$request_uri without args

Hi
Is it possible to access the $request_uri without the args at the end?
I know I can use $uri - but this returns the url-decoded uri.
I need the url-encoded (I.e. unchanged from original request) but with
the args stripped

On Wed, Sep 14, 2011 at 9:50 PM, Richard K.
[email protected] wrote:

Hi

Is it possible to access the $request_uri without the args at the end?

I know I can use $uri but this returns the url-decoded uri.

I need the url-encoded (I.e. unchanged from original request) but with the
args stripped

Why not strip $request_uri yourself? You can use “if” + pcre regex or
more preferably, set_by_lua/rewrite_by_lua provided by ngx_lua, to
process what in $request_uri :slight_smile:

Regards,
-agentzh

Agentzh, I’m glad you replied :slight_smile:
I am trying to find out why something is happening - a bug in nginx or
lua module (currently finding it hard to reproduce)
I’m using:

location /proxy
{
proxy_pass http://backend;
}

access_by_lua ’
local res =
ngx.location.capture(“/proxy” … ngx.var.request_uri, { args = { country
= ngx.var.geoip_country_code, host = ngx.var.host } } )

if ngx.var.request_url contains args like this:

/something/blah.jpg?34567

It gives 404

But like this:

/something/blah.jps?anything=34567

Works and gets processed ok

I don’t need args at backend - but I DO need the url still encoded

Backend when accessed directly with curl/wget with
“/something/blah.jpg?34567” returns ok

Any ideas?

On Wed, Sep 14, 2011 at 10:30 PM, Richard K.
[email protected] wrote:

I don’t need args at backend - but I DO need the url still encoded

Use the following line of Lua to strip the query args part (if any)
from ngx.var.request (i.e., the nginx variable $request_uri):

local uri = string.gsub(ngx.var.request_uri, "?.*", "")

and then you can feed the resulting uri variable into your subrequest
call instead of ngx.var.request_uri:

local res = ngx.location.capture("/proxy" .. uri,
        { args =
              { country = ngx.var.geoip_country_code, host = 

ngx.var.host }
} )

Hope it works for you :slight_smile:

Regards,
-agentzh

On Thu, Sep 15, 2011 at 12:33 AM, Richard K.
[email protected] wrote:

Many thanks, this fixed the problem (or got around it at least :slight_smile: - so not sure
where the bug is but I suspect it’s the mixture of ?3456 and ?val=3456 in the
query string after the lua pass to backend (I wasn’t reading “host” at backend
properly, hence 404)

I believe it’s not a bug because it’s wrong to pass the query args
part as the URI argument to ngx.location.capture. The behavior is
unspecified. So please avoid that in the first place.

Regards,
-agentzh

Many thanks, this fixed the problem (or got around it at least :slight_smile: - so
not sure where the bug is but I suspect it’s the mixture of ?3456 and
?val=3456 in the query string after the lua pass to backend (I wasn’t
reading “host” at backend properly, hence 404)