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

**URL:** <https://www.ruby-forum.com/t/wildcard-domains-how-to-treat-ip-address-and-specific-domains-differently-from-failover-wildcard-d/227051>\
**Category:** NGINX\
**Created:** [March 1, 2013, 9:20pm UTC](https://www.ruby-forum.com/t/wildcard-domains-how-to-treat-ip-address-and-specific-domains-differently-from-failover-wildcard-d/227051 "2013-03-01T21:20:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@jonny](https://www.ruby-forum.com/u/jonny)\
**Post date:** [March 1, 2013, 9:20pm UTC](https://www.ruby-forum.com/t/wildcard-domains-how-to-treat-ip-address-and-specific-domains-differently-from-failover-wildcard-d/227051/1 "2013-03-01T21:20:45Z")

</div>

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](http://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.

---

<div class="post-metadata">

**Author:** ![jonny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@jonny](https://www.ruby-forum.com/u/jonny)\
**Post date:** [March 2, 2013, 1:44am UTC](https://www.ruby-forum.com/t/wildcard-domains-how-to-treat-ip-address-and-specific-domains-differently-from-failover-wildcard-d/227051/2 "2013-03-02T01:44:36Z")

</div>

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

Hi there,

> Requests for [example.com](http://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](http://nginx.org/en/docs/http/request_processing.html) ?

And [Module ngx\_http\_core\_module](http://nginx.org/r/listen) and [Module ngx\_http\_core\_module](http://nginx.org/r/server_name) ?

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](http://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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![jonny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@jonny](https://www.ruby-forum.com/u/jonny)\
**Post date:** [March 2, 2013, 7:34am UTC](https://www.ruby-forum.com/t/wildcard-domains-how-to-treat-ip-address-and-specific-domains-differently-from-failover-wildcard-d/227051/3 "2013-03-02T07:34:29Z")

</div>

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](http://example.com);  
# site A  
}

server {  
listen 80 default\_server;  
# site B  
}

“listen 80/server\_name [example.com](http://example.com)” route all requests to [example.com](http://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.

> **[F5 NGINX: Built for cloud-native apps. Ready for AI traffic.](https://www.f5.com/products/nginx)**
>
> Deliver modern applications with NGINX. Explore high-performance load balancing, API gateways, and web server solutions trusted by top global enterprises.

---

<div class="post-metadata">

**Author:** ![jonny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@jonny](https://www.ruby-forum.com/u/jonny)\
**Post date:** [March 3, 2013, 8:59pm UTC](https://www.ruby-forum.com/t/wildcard-domains-how-to-treat-ip-address-and-specific-domains-differently-from-failover-wildcard-d/227051/4 "2013-03-03T20:59:06Z")

</div>

> }
> 
> “listen 80/server\_name [example.com](http://example.com)” route all requests to [example.com](http://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.
