Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get complete url using nginx and lua with openresty

Tags:

nginx

lua

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)
                         ';
                }
like image 572
Prashant Gaur Avatar asked Nov 15 '13 12:11

Prashant Gaur


People also ask

How does Lua work with Nginx?

Nginx+Lua is a self-contained web server embedding the scripting language Lua. Powerful applications can be written directly inside Nginx without using cgi, fastcgi, or uwsgi. By adding a little Lua code to an existing Nginx configuration file, it is easy to add small features.

What version of Lua does OpenResty use?

LuaJIT 2.0. x is always supported in OpenResty though LuaJIT 2.1+ is highly recommended.

What is the meaning of OpenResty?

OpenResty® is a full-fledged web platform that integrates our enhanced version of the Nginx core, our enhanced version of LuaJIT, many carefully written Lua libraries, lots of high quality 3rd-party Nginx modules, and most of their external dependencies.


5 Answers

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;
like image 165
Mohammad AbuShady Avatar answered Nov 09 '22 00:11

Mohammad AbuShady


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)

like image 30
Lyfing Avatar answered Nov 09 '22 00:11

Lyfing


ngx.say(ngx.var.host .. '/' .. ngx.var.uri)
like image 26
Jacky Avatar answered Nov 08 '22 23:11

Jacky


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

like image 44
catwell Avatar answered Nov 08 '22 23:11

catwell


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)
like image 30
Alex Pop Avatar answered Nov 08 '22 22:11

Alex Pop