Forwarding to upstream server at port specified in url query paramter

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.
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/. 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:
http://forum.nginx.org/read.php?2,236969,236969#msg-236969

On 6 March 2013 00:51, dakun [email protected] 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.
How can I achieve this?

Is http://wiki.nginx.org/HttpCoreModule#.24arg_PARAMETER of any use?

Jonathan

Jonathan M. // Oxford, London, UK
http://www.jpluscplusm.com/contact.html

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 ^ http://www.google.com/?q=$arg_p;
}

This does not work. Got error “no resolver defined to resolve
www.google.com” in log.
location /path {
proxy_pass 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/;
}

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;
}

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

Thanks! The Resolver did the trick.

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

On 6 March 2013 07:04, dakun [email protected] wrote:

location /path {
proxy_pass http://www.google.com/?q=$arg_p;
}

Come on chap - try a bit harder!
http://bit.ly/12tikWj

proxy_pass 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
http://www.jpluscplusm.com/contact.html

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