Fetching a string by parsing URL

I have a web server sitting behind Nginx. If there is an error, then I
want
to fetch some information from the url and pass it on to a static file
as
parameters. I have configured Nginx to fetch the query parameters from
the
url using $arg_param_name.

However, I also need to fetch a String from the url path itself. For
instance, if the url is
www.website.com/path1/path2?arg1=val&arg2=someval”,
how can I parse this url to fetch the last path (path2 in this case)? My
location directive is as below:

location ~*/path1/{

}

The url, however, need not always have the same number of paths. It can
also
have 3 paths. So I can’t use $1, $2 etc. I need to fetch the last path,
i.e
the path which is immediately followed by the query parameters (the ?
symbol). Is it possible to do this using Nginx directly?

Thanks.

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,260458,260458#msg-260458

On Jul 22, 2015 11:43, “sudharshanr” [email protected] wrote:

I have a web server sitting behind Nginx. If there is an error, then I
want
to fetch some information from the url and pass it on to a static file as
parameters. I have configured Nginx to fetch the query parameters from the
url using $arg_param_name.

However, I also need to fetch a String from the url path itself. For
instance, if the url is "www.website.com/path1/path2?arg1=val&arg2=someval
",
how can I parse this url to fetch the last path (path2 in this case)? My
location directive is as below:

location ~*/path1/{

}

The url, however, need not always have the same number of paths. It can
also
have 3 paths. So I can’t use $1, $2 etc. I need to fetch the last path,
i.e
the path which is immediately followed by the query parameters (the ?
symbol). Is it possible to do this using Nginx directly?

this? /([^/]+/?)$

Thanks.

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,260458,260458#msg-260458

On Tuesday, July 21, 2015 at 9:43 PM, sudharshanr wrote:

location ~*/path1/{
Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,260458,260458#msg-260458


nginx mailing list
[email protected]
http://mailman.nginx.org/mailman/listinfo/nginx

Regex match Might work here?

Edho A.: Thanks, but I think I need to rephrase my question. Right
now,
my nginx.conf looks like this:

         location ~*/path1/{
                 if (-f $document_root/error503.html) {
                      return 503;
                 }
        }

       error_page 503

<path_to_static_file>?arg1=$arg_queryparam1&arg2=

As you can see, if there is a 503 error, then I return a static file.
The
arguments to the static file are
1. the param from url whose name is queryparam1,
2. the last path from the url

Now how do I fetch the last path from the url and pass it as a param to
the
static file? I’m new to Nginx, so I might not have understood what you
meant. Or is what I’m doing completely wrong?

Thanks.

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,260458,260461#msg-260461

On Thu, Jul 23, 2015 at 2:08 AM, sudharshanr [email protected]
wrote:

from url>

As you can see, if there is a 503 error, then I return a static file. The
arguments to the static file are:

  1. the param from url whose name is queryparam1,
  2. the last path from the url

Now how do I fetch the last path from the url and pass it as a param to the
static file? I’m new to Nginx, so I’m not sure if I’m doing this right.

map $uri $last_path {
~/(?[^/]+)/?$ $pathname;
}

server {

error_page … …=$last_path;
}

Note that nothing will see the arguments if the error page is just a
static file and not a redirect.

For some reason, I’m getting it as null. This is my config file:

map $uri $last_path {
~*/(?[^/]+)/?$ $pathname;
}

server {
listen 80;
root /basefolder;

error_page 500 501 502 503 504 =

/error5x.html?name=$arg_name&path=$last_path;

location  ~*/error5x.html? {
   alias <static file location>/error5x.html;
 }

location ~*/path1/  {

    if (-f $document_root/error503.html) {
      return 503;
    }
....

}

However, if I update the location directive as below:

location ~*/error5x.html? {
alias /error5x.html?name=$arg_name&path=$last_path;
}

I get a file not found error. But when I check the nginx log, I can see
that
it is fetching the $last_path variable. Just that it takes it as part of
the
file name and throws a 404 error.

Thanks.

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,260458,260498#msg-260498

For some reason, I’m getting it as null. This is my config file:

map $uri $last_path {
~*/(?[^/]+)/?$ $pathname;
}

server {
listen 80;
root /basefolder;

error_page 500 501 502 503 504 =
/error5x.html?name=$arg_name&path=$last_path;

location ~*/error5x.html? {
alias /Desktop/error5x.html;
}

location ~*/path1/ {

if (-f $document_root/error503.html) {
return 503;
}

}

However, if I update the location directive as below:

location ~*/error5x.html? {
alias ?Desktop/error5x.html?name=$arg_name&path=$last_path;
}

I get a file not found error. But when I check the nginx log, I can see
that
it is fetching the $last_path variable. Just that it takes it as part of
the
file name and throws a 404 error. So it’s something like file
/Desktop/error5x.html?name=abc&path=xyz not found.

Thanks.

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,260458,260525#msg-260525

I think I need to rephrase my question. Right now, my nginx.conf looks
like
this:

location ~*/path1/{
if (-f $document_root/error503.html) {
return 503;
}
}

error_page 503 <path_to_static_file>?arg1=$arg_queryparam1&arg2=

As you can see, if there is a 503 error, then I return a static file.
The
arguments to the static file are:

  1. the param from url whose name is queryparam1,
  2. the last path from the url

Now how do I fetch the last path from the url and pass it as a param to
the
static file? I’m new to Nginx, so I’m not sure if I’m doing this right.

Thanks.

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,260458,260479#msg-260479

For some reason, I’m getting it as null. This is my config file:

map $uri $last_path {
~*/(?[^/]+)/?$ $pathname;
}

server {
listen 80;
root /basefolder;

error_page 500 501 502 503 504 =
/error5x.html?name=$arg_name&path=$last_path;

location ~*/error5x.html? {
alias /Desktop/error5x.html;
}

location ~*/path1/ {

if (-f $document_root/error503.html) {
return 503;
}

}

However, if I update the location directive as below:

location ~*/error5x.html? {
alias /Desktop/error5x.html?name=$arg_name&path=$last_path;
}

I get a file not found error. But when I check the nginx log, I can see
that
it is fetching the $last_path variable. Just that it takes it as part of
the
file name and throws a 404 error. So it’s something like “open()
“/Desktop/error5x.html?name=abc&path=xyz” failed (2: No such file or
directory),”.

Thanks.

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,260458,260531#msg-260531

On Thu, Jul 23, 2015 at 03:32:26PM -0400, sudharshanr wrote:

Hi there?

For some reason, I’m getting it as null. This is my config file:

What, specifically, are you getting as null? What response do you see
that you do not want to see?

If you make a request for /path1/one?name=two, what response do you
want?

Is it http 503 with the content of the file /Desktop/error5x.html,
or something else?

Where does “one” and “two” come into the response that you want to get?

However, if I update the location directive as below:

location ~*/error5x.html? {
alias /Desktop/error5x.html?name=$arg_name&path=$last_path;
}

I get a file not found error.

Yes; that is what you have told nginx to do. (Unless you have a file
with
a name like /Desktop/error5x.html?name=two&path=one, which you probably
do not.)

If you can describe what you want to have happen, perhaps someone can
tell you how to tell nginx to get that to happen.

f

Francis D. [email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs