NGINX sub domain proxy pass

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)


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;

server_name ~^(?<subnum>[0-9]+)\.hostname\.com$;

proxy_pass http://192.168.56.$subnum;

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


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 !!! ;-)