Proxy Pass based on the first field in the URI

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Proxy Pass based on the first field in the URI

meir hazon
Hello,

I have tried to do the following but couldn't pull it out.

Maybe you will have a good idea as I am not an expert.

I would like to proxy pass to different proxies based on the first part of a URI.

I would also like to pass the message to the proxy without the first part of the URI but use the rest of it.

Please note that the length and the URI strings and number of fields might change 

example,

incoming to nginx,
if $1 == bla proxy pass to http://xxx.com/aa/er
if $1 == vv proxy pass to http://ccc.com/aa/er

Thanks so much,

Meir


_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: Proxy Pass based on the first field in the URI

Francis Daly
On Wed, Sep 11, 2019 at 04:45:21PM +0200, meir hazon wrote:

Hi there,

> I would also like to pass the message to the proxy without the first part
> of the URI but use the rest of it.

http://nginx.org/r/proxy_pass

includes the section:

"""
If the proxy_pass directive is specified with a URI, then when a request
is passed to the server, the part of a normalized request URI matching
the location is replaced by a URI specified in the directive:
  location /name/ {
      proxy_pass http://127.0.0.1/remote/;
  }
"""

With that sample config, you want two "location"s:

> https://yyy.com/bla/aa/er
> if $1 == bla proxy pass to http://xxx.com/aa/er
> if $1 == vv proxy pass to http://ccc.com/aa/er

"/name/" == "/bla/"; "/remote/" == "/".
"/name/" == "/vv/"; "/remote/" == "/".

Does that work for you?

        f
--
Francis Daly        [hidden email]
_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: Proxy Pass based on the first field in the URI

adrian.hilt
In reply to this post by meir hazon
Try something like this:

map $urlprefix $urlproxy {
  "foo" "https://foohost.foo.com";
  "bar" "http://barhost.blah.com";
  "fie" "https://fie.special.domain.com/blubb";
  default "https://standard.com";
}

[...]

location ~ "^/(?<urlprefix>[^/]+)(?<urlsuffix>/.*)$" {

  [...]
  proxy_pass "$urlproxy$urlsuffix$is_args$args";
  [...]
}

You can dynamically determine the host (including a URL piece, if necessary)
with the "map", and use the mapped proxy prefix then in the proxy_pass
directive. If whatever lies beyond the NGINX requires URL arguments, don't
forget the $is_args$args part.

--j.

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,285607,285609#msg-285609

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: Proxy Pass based on the first field in the URI

meir hazon
In reply to this post by Francis Daly
Hi,

I don't think so, it should always be the first field of the URI but it would be a random string. I have to proxy pass and rebuild the new URI based on location at the original URI only.

Could you think of a way to do this?

Thanks so much,

Meir


On Wed, Sep 11, 2019 at 5:46 PM Francis Daly <[hidden email]> wrote:
On Wed, Sep 11, 2019 at 04:45:21PM +0200, meir hazon wrote:

Hi there,

> I would also like to pass the message to the proxy without the first part
> of the URI but use the rest of it.

http://nginx.org/r/proxy_pass

includes the section:

"""
If the proxy_pass directive is specified with a URI, then when a request
is passed to the server, the part of a normalized request URI matching
the location is replaced by a URI specified in the directive:
  location /name/ {
      proxy_pass http://127.0.0.1/remote/;
  }
"""

With that sample config, you want two "location"s:

> https://yyy.com/bla/aa/er
> if $1 == bla proxy pass to http://xxx.com/aa/er
> if $1 == vv proxy pass to http://ccc.com/aa/er

"/name/" == "/bla/"; "/remote/" == "/".
"/name/" == "/vv/"; "/remote/" == "/".

Does that work for you?

        f
--
Francis Daly        [hidden email]
_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: Proxy Pass based on the first field in the URI

meir hazon
In reply to this post by adrian.hilt
Hi,

It looks great, I will try it out and let you know.

Thanks so much,

Meir

On Wed, Sep 11, 2019 at 5:56 PM j94305 <[hidden email]> wrote:
Try something like this:

map $urlprefix $urlproxy {
  "foo" "https://foohost.foo.com";
  "bar" "http://barhost.blah.com";
  "fie" "https://fie.special.domain.com/blubb";
  default "https://standard.com";
}

[...]

location ~ "^/(?<urlprefix>[^/]+)(?<urlsuffix>/.*)$" {

  [...]
  proxy_pass "$urlproxy$urlsuffix$is_args$args";
  [...]
}

You can dynamically determine the host (including a URL piece, if necessary)
with the "map", and use the mapped proxy prefix then in the proxy_pass
directive. If whatever lies beyond the NGINX requires URL arguments, don't
forget the $is_args$args part.

--j.

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,285607,285609#msg-285609

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

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: Proxy Pass based on the first field in the URI

Francis Daly
In reply to this post by meir hazon
On Wed, Sep 11, 2019 at 08:41:15PM +0200, meir hazon wrote:

Hi there,

> I don't think so, it should always be the first field of the URI but it
> would be a random string. I have to proxy pass and rebuild the new URI
> based on location at the original URI only.

I don't understand what your requirements are.

> > > https://yyy.com/bla/aa/er
> > > if $1 == bla proxy pass to http://xxx.com/aa/er
> > > if $1 == vv proxy pass to http://ccc.com/aa/er

"bla" is not random; it is a thing that you know that maps to xxx.com.

"vv" is not random; it is a thing that you know that maps to ccc.com.

So, somewhere, you have a list of the uri prefix / upstream server
mappings, no?

Use that list to make the nginx config.

> Could you think of a way to do this?

Not without me understanding what "this" is.

Good luck with it,

        f
--
Francis Daly        [hidden email]
_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: Proxy Pass based on the first field in the URI

meir hazon
Dear Francis and friends,

I am sorry if I wasn't clear enough, I appreciate your help and advice dearly. 

I was thinking on creating environments dynamically, Meaning,

1. the source "host" of the URL is random

2. The first field of the URI includes a random string with the environment ID.

3. Based on this ID a proxy pass is added (I will add DNS records etc.)

4. The proxy pass points to a static "host" and the URI includes the original URI excluding the first filed (env ID field)

Meaning that I will need a good rewrite and an "if" statement for each one of the environments (IDs)

Could you please 

On Wed, Sep 11, 2019 at 11:48 PM Francis Daly <[hidden email]> wrote:
On Wed, Sep 11, 2019 at 08:41:15PM +0200, meir hazon wrote:

Hi there,

> I don't think so, it should always be the first field of the URI but it
> would be a random string. I have to proxy pass and rebuild the new URI
> based on location at the original URI only.

I don't understand what your requirements are.

> > > https://yyy.com/bla/aa/er
> > > if $1 == bla proxy pass to http://xxx.com/aa/er
> > > if $1 == vv proxy pass to http://ccc.com/aa/er

"bla" is not random; it is a thing that you know that maps to xxx.com.

"vv" is not random; it is a thing that you know that maps to ccc.com.

So, somewhere, you have a list of the uri prefix / upstream server
mappings, no?

Use that list to make the nginx config.

> Could you think of a way to do this?

Not without me understanding what "this" is.

Good luck with it,

        f
--
Francis Daly        [hidden email]
_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: Proxy Pass based on the first field in the URI

meir hazon
Sorry, the only random is the first part of the URI,

Following is the right description,
Dear Francis and friends,

I am sorry if I wasn't clear enough, I appreciate your help and advice dearly. 

I was thinking on creating environments dynamically, Meaning,

1. the source "host" of the URL is static

2. The first field of the URI includes a random string with the environment ID.

3. Based on this ID a proxy pass is added to the nginx conf 

4. The proxy pass points to a static NEW "host" and the URI includes the original URI excluding the first filed (env ID field)

   a DNS record would be added etc based on the env ID

Meaning that I will need a good rewrite and an "if" statement for each one of the environments ( based on the env IDs ) 

Could you please share your thoughts with me?

Thanks so much,

Meir



On Thu, Sep 12, 2019 at 9:34 AM meir hazon <[hidden email]> wrote:
Dear Francis and friends,

I am sorry if I wasn't clear enough, I appreciate your help and advice dearly. 

I was thinking on creating environments dynamically, Meaning,

1. the source "host" of the URL is random

2. The first field of the URI includes a random string with the environment ID.

3. Based on this ID a proxy pass is added (I will add DNS records etc.)

4. The proxy pass points to a static "host" and the URI includes the original URI excluding the first filed (env ID field)

Meaning that I will need a good rewrite and an "if" statement for each one of the environments (IDs)

Could you please 

On Wed, Sep 11, 2019 at 11:48 PM Francis Daly <[hidden email]> wrote:
On Wed, Sep 11, 2019 at 08:41:15PM +0200, meir hazon wrote:

Hi there,

> I don't think so, it should always be the first field of the URI but it
> would be a random string. I have to proxy pass and rebuild the new URI
> based on location at the original URI only.

I don't understand what your requirements are.

> > > https://yyy.com/bla/aa/er
> > > if $1 == bla proxy pass to http://xxx.com/aa/er
> > > if $1 == vv proxy pass to http://ccc.com/aa/er

"bla" is not random; it is a thing that you know that maps to xxx.com.

"vv" is not random; it is a thing that you know that maps to ccc.com.

So, somewhere, you have a list of the uri prefix / upstream server
mappings, no?

Use that list to make the nginx config.

> Could you think of a way to do this?

Not without me understanding what "this" is.

Good luck with it,

        f
--
Francis Daly        [hidden email]
_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: Proxy Pass based on the first field in the URI

adrian.hilt
In reply to this post by meir hazon
PS: If, like you mentioned in the other reply message, want to create
environments dynamically, you could use the map directive with an include
file that is dynamically updated by the deployment process of such an
environment (and then do nginx -s reload), but even more elegant would be
the replacement of the (static) map by a (dynamic) keyval map. The keyval
mappings can be maintained via the NGINX API, so you won't need reloads of
NGINX in the event of updates. The use of the keyval map is identical to the
map directive, except the definition would use a keyval zone.

Cheers,
--j.

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,285607,285623#msg-285623

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