Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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)

share|improve this question
1  
You should redeem using "solution" with if. See either serverfault.com/a/388660/67675 or serverfault.com/a/388709/67675 –  poige May 13 '12 at 19:06

3 Answers 3

up vote 3 down vote accepted

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;
share|improve this answer
    
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 '12 at 21:03
    
I should note that a ping to the same address from the same machine works fine –  user554005 May 12 '12 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 '12 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 '12 at 19:04
server_name ~^(?<subnum>[0-9]+)\.hostname\.com$;

proxy_pass http://192.168.56.$subnum;

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

share|improve this answer
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 !!! ;-)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.