Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
You should redeem using "solution" with if. See either or – poige May 13 '12 at 19:06
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 *;

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:, server: *, request: "GET /favicon.ico HTTP/1.1", upstream: "", host: "" 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 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;

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 *;

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


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

share|improve this answer

Your Answer


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.