7

If request URL is

test.com/gifts.
If i am using
 ngx.var.uri
output is
/gifts/
expected output is
test.com/gifts
Code :

 location /gifts {
                    try_files $uri @url_change;
                }


                location @url_change {
                default_type text/html;
                content_by_lua '

                        ngx.say(ngx.var.uri)
                         ';
                }
1
  • you can get it by, ngx.var.request_uri Commented Jan 5, 2017 at 12:37

5 Answers 5

19

If anyone is looking for the original request uri, it's here:

ngx.var.request_uri

While ngx.var.uri is the new uri after ngx rewrite phase.

Try it:

ngx.say(ngx.var.request_uri)

Sign up to request clarification or add additional context in comments.

Comments

16

I don't really understand what you want, but if you want the actual full URL i think you can use this

$http_host$request_uri;

4 Comments

This is nowhere near a "complete URL" that would contain scheme, user if present, password if present, host, port if present, path, and query if present.
Thanks, your help works like a charm, $request_uri will get full uri event .html
ngx.var.scheme.."://".. ngx.var.http_host.. ngx.var.request_uri.."?"..ngx.var.query_string
@ahuigo I got "attempt to concatenate field 'query_string' (a nil value)" error
6
ngx.say(ngx.var.host .. '/' .. ngx.var.uri)

Comments

2

Isn't what you need just the $host variable, i.e. ngx.var.host?

3 Comments

Can you please have a look at my question?
@abi1964's question has been moved or removed.
@JoeMcMahon: Sorry it was deleted. Thanks for your help :)
1

Generate the full url and avoid getting an error when query_string is nil:

local full_url = ngx.var.scheme.."://"..ngx.var.http_host..ngx.var.request_uri
if ngx.var.query_string ~= nil then
  full_url = full_url.."?"..ngx.var.query_string
end
ngx.say(full_url)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.