# Forwarding to upstream server at port specified in url query paramter

**URL:** <https://www.ruby-forum.com/t/forwarding-to-upstream-server-at-port-specified-in-url-query-paramter/227157>\
**Category:** NGINX\
**Created:** [March 6, 2013, 1:52am UTC](https://www.ruby-forum.com/t/forwarding-to-upstream-server-at-port-specified-in-url-query-paramter/227157 "2013-03-06T01:52:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dakun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7bcc69/32.png) [@dakun](https://www.ruby-forum.com/u/dakun)\
**Post date:** [March 6, 2013, 1:52am UTC](https://www.ruby-forum.com/t/forwarding-to-upstream-server-at-port-specified-in-url-query-paramter/227157/1 "2013-03-06T01:52:34Z")

</div>

I am trying to use the HttpProxyModule to forward traffic to an upstream  
server. The incoming request will be like:

```
http://foobar.com/path?p=1234

```

where p=1234 indicates the upstream port to use.

For this example I would want to forward to  
[http://upstreamserver.com:1234](http://upstreamserver.com:1234).  
How can I achieve this?

I have experimented with regex and rewrites but was unsuccessful mainly  
because regex on “location” directive does not apply to url parameters.  
And  
rewrites do not allow me to change the outgoing port number.

I had also tried to use

```
    if ($args ~ "p=(\d+)") {
          set $port $1;
          rewrite ^ ??????
    }

```

But I ended up with either a browser redirect which is not desirable, or  
I  
ended up sending traffic to local file which does not exist.

Unfortunately I cannot change the incoming request to include ‘1234’ as  
part  
of path, e.g. [http://foobar.com/p/1234/](http://foobar.com/p/1234/). I have no control of that.

I am using nginx 1.3.13 with websocket support.

Thanks in advance for any help.  
Dakun

Posted at Nginx Forum:

> **[Forwarding to upstream server at port specified in url query paramter](https://forum.nginx.org/read.php?2,236969,236969#msg-236969)**
>
> I am trying to use the HttpProxyModule to forward traffic to an upstream server. The incoming request will be like: http://foobar.com/path?p=1234 where p=1234 indicates the upstream port to use. For this example I would want to forward to http://u

---

<div class="post-metadata">

**Author:** ![dakun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7bcc69/32.png) [@dakun](https://www.ruby-forum.com/u/dakun)\
**Post date:** [March 6, 2013, 2:20am UTC](https://www.ruby-forum.com/t/forwarding-to-upstream-server-at-port-specified-in-url-query-paramter/227157/2 "2013-03-06T02:20:01Z")

</div>

On 6 March 2013 00:51, dakun [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am trying to use the HttpProxyModule to forward traffic to an upstream  
> server. The incoming request will be like:
> 
> ```
> http://foobar.com/path?p=1234
> 
> ```
> 
> where p=1234 indicates the upstream port to use.
> 
> For this example I would want to forward to [http://upstreamserver.com:1234](http://upstreamserver.com:1234).  
> How can I achieve this?

Is [GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/HttpCoreModule#.24arg_PARAMETER) of any use?

## Jonathan

Jonathan M. // Oxford, London, UK

> **[Jonathan Matthews :: Words Typed, Great Rates For Cash](https://www.jpluscplusm.com)**

---

<div class="post-metadata">

**Author:** ![dakun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7bcc69/32.png) [@dakun](https://www.ruby-forum.com/u/dakun)\
**Post date:** [March 6, 2013, 8:04am UTC](https://www.ruby-forum.com/t/forwarding-to-upstream-server-at-port-specified-in-url-query-paramter/227157/3 "2013-03-06T08:04:41Z")

</div>

Thanks for the advice on $arg\_PARAMETER. It allows me to retrieve the  
parameter. However I am not able to use it as a port number for  
proxy\_pass.

This shows that I can get the parameter and use it in rewrite:  
location /test {  
rewrite ^ [Google](http://www.google.com/?q=$arg_p);  
}

This does not work. Got error “no resolver defined to resolve  
[www.google.com](http://www.google.com)” in log.  
location /path {  
proxy\_pass [Google](http://www.google.com/?q=$arg_p);  
}

This shows that I can use the parameter in a rewrite prior to  
proxy\_pass:  
location /path {  
rewrite ^(.\*)$ /?q=$arg\_p break;  
proxy\_pass [http://www.google.com/](http://www.google.com/);  
}

Unfortunately I still can’t use the paramrter value as an upstream port  
number:  
location /path2 {  
rewrite ^(.\*)$ :$arg\_p break;  
proxy\_pass [http://www.google.com](http://www.google.com);  
}

Posted at Nginx Forum:

> **[Re: Forwarding to upstream server at port specified in url query paramter](https://forum.nginx.org/read.php?2,236969,236978#msg-236978)**
>
> I am trying to use the HttpProxyModule to forward traffic to an upstream server. The incoming request will be like: http://foobar.com/path?p=1234 where p=1234 indicates the upstream port to use. For this example I would want to forward to http://u

---

<div class="post-metadata">

**Author:** ![dakun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7bcc69/32.png) [@dakun](https://www.ruby-forum.com/u/dakun)\
**Post date:** [March 6, 2013, 7:37pm UTC](https://www.ruby-forum.com/t/forwarding-to-upstream-server-at-port-specified-in-url-query-paramter/227157/4 "2013-03-06T19:37:16Z")

</div>

Thanks! The Resolver did the trick.

Posted at Nginx Forum:

> **[Re: Forwarding to upstream server at port specified in url query paramter](https://forum.nginx.org/read.php?2,236969,237020#msg-237020)**
>
> I am trying to use the HttpProxyModule to forward traffic to an upstream server. The incoming request will be like: http://foobar.com/path?p=1234 where p=1234 indicates the upstream port to use. For this example I would want to forward to http://u

---

<div class="post-metadata">

**Author:** ![dakun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7bcc69/32.png) [@dakun](https://www.ruby-forum.com/u/dakun)\
**Post date:** [March 6, 2013, 10:54am UTC](https://www.ruby-forum.com/t/forwarding-to-upstream-server-at-port-specified-in-url-query-paramter/227157/5 "2013-03-06T10:54:44Z")

</div>

On 6 March 2013 07:04, dakun [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> location /path {  
> proxy\_pass [Google](http://www.google.com/?q=$arg_p);  
> }

Come on chap - try a bit harder!

> **[LMGTFY](https://lmgtfy.com/?q=%22no+resolver+defined%22+nginx)**
>
> Let Me Google That For You (LMGTFY): for all those people who find it more convenient to bother you with their question rather than search it for themselves.

> proxy\_pass [http://www.google.com](http://www.google.com);  
> }

No, you just haven’t configured it correctly.

You really need to read the proxy\_pass and rewrite documentation more  
carefully.  
Neither works exactly the way you seem to think they do.

## Jonathan

Jonathan M. // Oxford, London, UK

> **[Jonathan Matthews :: Words Typed, Great Rates For Cash](https://www.jpluscplusm.com)**
