WildCard domains : how to treat IP Address and Specific Domains differently from Failover/Wildcard D

forgive me if this has been asked before – I couldn’t find this exact
question in my mailing list archives back to 2007

I am trying to deal with wildcard domains in a setup.

The intended result is to do this :

Requests for example.com
Serve Site A

All IP Address Requests :
Serve Site A

All other domains ( wildcard / failover )
Serve Site B

I’ve tried several combinations of listen + server name, but I can’t get
this right. I end up sending everything to site A or site B.

On Fri, Mar 01, 2013 at 03:20:10PM -0500, Jonathan V. wrote:

Hi there,

Requests for example.com
Serve Site A

All IP Address Requests :
Serve Site A

All other domains ( wildcard / failover )
Serve Site B

I’ve tried several combinations of listen + server name, but I can’t get this
right. I end up sending everything to site A or site B.

You’ve seen How nginx processes a request ?

And Module ngx_http_core_module and Module ngx_http_core_module ?

You need the same “listen” ip:port in all servers – simplest is to
leave it at the default.

The you need the correct “server_name” directives in the correct
server{}
blocks.

B should be the default, so put it first:

server {
  return 200 "site B\n";
}

A should match the exact hostname example.com, and anything that is just
numbers and dots:

server {
  server_name example.com;
  server_name ~^[0-9.]*$;
  return 200 "site A\n";
}

Because of the default value of server_name, a request with no “host”
will match B. You can make it match A easily enough.

f

Francis D. [email protected]

On Mar 2, 2013, at 0:20 , Jonathan V. wrote:

Serve Site A

All other domains ( wildcard / failover )
Serve Site B

I’ve tried several combinations of listen + server name, but I can’t get this
right. I end up sending everything to site A or site B.

server {
listen 80;
listen IP:80;
server_name example.com;
# site A
}

server {
listen 80 default_server;
# site B
}

“listen 80/server_name example.com” route all requests to example.com
to site A.
“listen IP:80” routes all requests to IP:80 to site A.
Anything else is routed to default server of 80 port, i.e. to site B.


Igor S.

}

“listen 80/server_name example.com” route all requests to example.com to site
A.
“listen IP:80” routes all requests to IP:80 to site A.
Anything else is routed to default server of 80 port, i.e. to site B.

Thank you Igor.

Unfortunately, that’s not what I needed. I don’t necessarily know the
IP address(es) on these machines. This is part of an automated
deployment.

Server A:
Specific Domain Name
any IPs

Server B
any domain names

Francis-

Thank you for this bit –

 server {
   server_name example.com;
   server_name ~^[0-9.]*$;
   return 200 "site A\n";
 }

i didn’t think of a regex-based server name. that works perfectly.