Welcome! Log In Create A New Profile

Advanced

Re: Using map and proxy_pass

Francis Daly
May 02, 2011 11:36AM
On Mon, May 02, 2011 at 02:56:19PM +0200, Markus Jelsma wrote:

Hi there,

A general rule for most config confusions is "the debug log is your
friend".

> Anyone else having some thoughs on how to use the map in conjunction with
> proxy_pass to set different hosts based on the first URI segment?

It's not clear to me why you're happy to generate an include file of
mappings in an external program, but not happy to generate an include
file of locations in an external program.

And if you have a large number of locations, you'll probably be happier
having no top-level regex locations.

But all that aside...

"map $var1 $var2" sets $var2 based on a string match of $var1.

You wish to match based on the first uri segment. So, do just that:

===
http {
map $first_segment $value {
default 127.0.0.1;
/abc 127.0.0.2;
/def 127.0.0.3;
}
server {
if ($uri ~* ^(/[^/]*) ) {
set $first_segment $1;
}
location / {
proxy_pass http://$value:8080/$uri ;
}
}
}
===

You may want to play with the proxy_pass line depending on exactly what
you want to do, but the above seems to work for me.

You can also add a $second_segment (or $part2) in the if{} block, if
you want a separate map for that.

Good luck with it,

f
--
Francis Daly francis@daoine.org

_______________________________________________
nginx mailing list
nginx@nginx.org
http://nginx.org/mailman/listinfo/nginx
Subject Author Posted

Using map and proxy_pass

Markus Jelsma April 29, 2011 11:48AM

Re: Using map and proxy_pass

Markus Jelsma April 29, 2011 11:50AM

Re: Using map and proxy_pass

gregr401 April 29, 2011 11:40PM

Re: Using map and proxy_pass

Markus Jelsma April 30, 2011 06:54AM

Re: Using map and proxy_pass

Markus Jelsma May 02, 2011 08:58AM

Re: Using map and proxy_pass

Francis Daly May 02, 2011 11:36AM

Re: Using map and proxy_pass

Igor Sysoev May 03, 2011 05:12AM

Re: Using map and proxy_pass

Markus Jelsma May 03, 2011 07:04AM

Re: Using map and proxy_pass

Igor Sysoev May 03, 2011 08:08AM



Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 283
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready