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

Posted by Jonathan Vanasco (Guest)
on 2013-03-01 21:20
(Received via mailing list)
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.
Posted by Francis Daly (Guest)
on 2013-03-02 01:44
(Received via mailing list)
On Fri, Mar 01, 2013 at 03:20:10PM -0500, Jonathan Vanasco 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 http://nginx.org/en/docs/http/request_processing.html ?

And http://nginx.org/r/listen and 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, 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 Daly        francis@daoine.org
Posted by Igor Sysoev (Guest)
on 2013-03-02 07:34
(Received via mailing list)
On Mar 2, 2013, at 0:20 , Jonathan Vanasco 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 Sysoev
http://nginx.com/support.html
Posted by Jonathan Vanasco (Guest)
on 2013-03-03 20:59
(Received via mailing list)
> }
>
> "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.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.