# Fetching a string by parsing URL

**URL:** <https://www.ruby-forum.com/t/fetching-a-string-by-parsing-url/242726>\
**Category:** NGINX\
**Created:** [July 22, 2015, 6:43am UTC](https://www.ruby-forum.com/t/fetching-a-string-by-parsing-url/242726 "2015-07-22T06:43:47Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sadaf\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@Sadaf\_N](https://www.ruby-forum.com/u/Sadaf_N)\
**Post date:** [July 22, 2015, 6:43am UTC](https://www.ruby-forum.com/t/fetching-a-string-by-parsing-url/242726/1 "2015-07-22T06:43:47Z")

</div>

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](http://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:

> **[Fetching a string by parsing URL](https://forum.nginx.org/read.php?2,260458,260458#msg-260458)**
>
> 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...

---

<div class="post-metadata">

**Author:** ![sudharshanr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f05b48/32.png) [@sudharshanr](https://www.ruby-forum.com/u/sudharshanr)\
**Post date:** [July 22, 2015, 6:56am UTC](https://www.ruby-forum.com/t/fetching-a-string-by-parsing-url/242726/2 "2015-07-22T06:56:03Z")

</div>

On Jul 22, 2015 11:43, “sudharshanr” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](http://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](http://forum.nginx.org/read.php?2,260458,260458#msg-260458)

---

<div class="post-metadata">

**Author:** ![sudharshanr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f05b48/32.png) [@sudharshanr](https://www.ruby-forum.com/u/sudharshanr)\
**Post date:** [July 22, 2015, 2:22pm UTC](https://www.ruby-forum.com/t/fetching-a-string-by-parsing-url/242726/3 "2015-07-22T14:22:23Z")

</div>

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](http://forum.nginx.org/read.php?2,260458,260458#msg-260458)
> 
> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://mailman.nginx.org/mailman/listinfo/nginx)

Regex match Might work here?

---

<div class="post-metadata">

**Author:** ![sudharshanr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f05b48/32.png) [@sudharshanr](https://www.ruby-forum.com/u/sudharshanr)\
**Post date:** [July 22, 2015, 8:18am UTC](https://www.ruby-forum.com/t/fetching-a-string-by-parsing-url/242726/4 "2015-07-22T08:18:25Z")

</div>

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:

> **[Re: Fetching a string by parsing URL](https://forum.nginx.org/read.php?2,260458,260461#msg-260461)**
>
> 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...

---

<div class="post-metadata">

**Author:** ![sudharshanr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f05b48/32.png) [@sudharshanr](https://www.ruby-forum.com/u/sudharshanr)\
**Post date:** [July 22, 2015, 8:12pm UTC](https://www.ruby-forum.com/t/fetching-a-string-by-parsing-url/242726/5 "2015-07-22T20:12:42Z")

</div>

On Thu, Jul 23, 2015 at 2:08 AM, sudharshanr [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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.

---

<div class="post-metadata">

**Author:** ![sudharshanr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f05b48/32.png) [@sudharshanr](https://www.ruby-forum.com/u/sudharshanr)\
**Post date:** [July 23, 2015, 1:32am UTC](https://www.ruby-forum.com/t/fetching-a-string-by-parsing-url/242726/6 "2015-07-23T01:32:16Z")

</div>

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:

> **[Re: Fetching a string by parsing URL](https://forum.nginx.org/read.php?2,260458,260498#msg-260498)**
>
> 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...

---

<div class="post-metadata">

**Author:** ![sudharshanr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f05b48/32.png) [@sudharshanr](https://www.ruby-forum.com/u/sudharshanr)\
**Post date:** [July 23, 2015, 8:36pm UTC](https://www.ruby-forum.com/t/fetching-a-string-by-parsing-url/242726/7 "2015-07-23T20:36:49Z")

</div>

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:

> **[Re: Fetching a string by parsing URL](https://forum.nginx.org/read.php?2,260458,260525#msg-260525)**
>
> 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...

---

<div class="post-metadata">

**Author:** ![sudharshanr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f05b48/32.png) [@sudharshanr](https://www.ruby-forum.com/u/sudharshanr)\
**Post date:** [July 22, 2015, 7:09pm UTC](https://www.ruby-forum.com/t/fetching-a-string-by-parsing-url/242726/8 "2015-07-22T19:09:47Z")

</div>

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:

> **[Re: Fetching a string by parsing URL](https://forum.nginx.org/read.php?2,260458,260479#msg-260479)**
>
> 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...

---

<div class="post-metadata">

**Author:** ![sudharshanr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f05b48/32.png) [@sudharshanr](https://www.ruby-forum.com/u/sudharshanr)\
**Post date:** [July 23, 2015, 9:32pm UTC](https://www.ruby-forum.com/t/fetching-a-string-by-parsing-url/242726/9 "2015-07-23T21:32:54Z")

</div>

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:

> **[Re: Fetching a string by parsing URL](https://forum.nginx.org/read.php?2,260458,260531#msg-260531)**
>
> 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...

---

<div class="post-metadata">

**Author:** ![sudharshanr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f05b48/32.png) [@sudharshanr](https://www.ruby-forum.com/u/sudharshanr)\
**Post date:** [July 25, 2015, 12:54am UTC](https://www.ruby-forum.com/t/fetching-a-string-by-parsing-url/242726/10 "2015-07-25T00:54:24Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
