Question

Exclude specific sub-domains from server where have wildcard server name

Posted March 1, 2017 4.5k views
NginxUbuntu 16.04

Hi,
is it possible to have 3 server blocks with following server name?
server block 1

server_name example.com www.example.com;

server block 2

server_name demo.example.com;

server block 3

server_name *.example.com;

reason i want to have like this, my main site will have server block 1, a demo will have server block 2, server block 3 will maintain the configuration for all other sub-domains that my client will create from my application.

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
2 answers

Yes, Nginx uses the wildcard on anything not specified.
http://nginx.org/en/docs/http/server_names.html

@newbie

That’s definitely possible :-). Just make sure you have an A entry for the WildCard in your DNS Zone otherwise it won’t work properly.

For example:

A          @          DROPLET_IP
A          *          DROPLET_IP
A          www        DROPLET_IP
A          demo       DROPLET_IP

Alternatively, you can use a CNAME for www:

A          @          DROPLET_IP
A          *          DROPLET_IP
CNAME      www        example.com
A          demo       DROPLET_IP

Though for completeness, I’d recommend:

A          @          DROPLET_IP
A          *          DROPLET_IP
A          www        DROPLET_IP
A          demo       DROPLET_IP
A          www.demo   DROPLET_IP

or

A          @          DROPLET_IP
A          *          DROPLET_IP
CNAME      www        example.com
A          demo       DROPLET_IP
CNAME      www.demo   demo.example.com

… and changing:

server_name demo.example.com;

to

server_name demo.example.com www.demo.example.com;
  • thank you very much @hansen & @jtittle . really appreciate your responses.

    @jtittle
    i was thinking to have DNS record as following because all this will be hosted on a single server at the moment.

    A       @       DROPLET_IP
    A       *       DROPLET_IP
    CNAME   www     example.com
    CNAME   demo      example.com
    

    or should i go for adding a records instaed. in case i need to change server setup and scale it horizontally in future, will having CNAME record could cause any problem?

    • As long as you keep the wildcard an A-record, then you’re good. By design DNS doesn’t allow @ to be anything other than an A-record.
      The rest could be CNAME.

    • @newbie

      If you’re creating a server block for demo.example.com, then I would use an A entry over a CNAME simply because by defining a server block, you’re intending on the sub-domain to resolve to a location whereas with a wildcard, you’re not and essentially stating that everything resolves to example.com (unless there’s a server block for that specific sub-domain).

Submit an Answer