1

Version: nginx/1.2.0 || (I know the risks, its for a internal server) How would I setup a system where 17.hostname.com would be put as proxy_pass http://192.168.56.17:80 (Where 17 would be replaced with what ever number was before the hostname)

1

3 Answers 3

3

Edit: The regular expression server_name and map directive solutions are better than this one, which uses the evil if directive.

The $host variable contains the hostname which the client requested, although you need to process this a bit to just get the part you want. It looks like the only way to achieve this is with the if and set directives from the Rewrite module, so try something like this:

server_name *.hostname.com;

if ($host ~* ^([0-9]+)\.hostname\.com$) {
    set $proxyhost 192.168.56.$1;
}

proxy_pass http://$proxyhost;
4
  • Im getting some errors when I try and use your method 2012/05/12 21:03:06 [error] 30314#0: *26 connect() failed (113: No route to host) while connecting to upstream, client: 69.171.162.3, server: *.vm.hostname.com, request: "GET /favicon.ico HTTP/1.1", upstream: "http://192.168.56.106:80/favicon.ico", host: "106.vm.hostname.com" Ive setup proxy_bind to point to my local interface and its still giving me that issue
    – user554005
    May 12, 2012 at 21:03
  • I should note that a ping to the same address from the same machine works fine
    – user554005
    May 12, 2012 at 21:06
  • nginx is selecting the correct upstream, so that part seems to be working. Does this happen for all upstreams? Does curl http://192.168.56.106:80/favicon.ico from the server running nginx work?
    – mgorven
    May 12, 2012 at 22:15
  • Blind if() using is plainly wrong, it's almost deprecated, it's not good practice (not even mentioning "best" one).
    – poige
    May 13, 2012 at 19:04
5
server_name ~^(?<subnum>[0-9]+)\.hostname\.com$;

proxy_pass http://192.168.56.$subnum;

http://nginx.org/en/docs/http/server_names.html

5
map $host $backend {

    default 1;
    ~*^(?P<number>[0-9]+)\.hostname\.com$        $number;

    # FIXME: [0-9]+ must be replaced to regex with accurate check 1..254 range
    # for example [1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4] or similar

}

server {
    server_name *.hostname.com;

    location / {
        proxy_pass http://192.168.56.$backend:80;
    }

}

And remember: If is evil !!! ;-)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .