nginx subdomain configuration on virtual host

13,389

Sub-domain configuration starts with an entry in the DNS server of the parent domain and the lookup resolves the sub-domain to an IP address of the web server. The web server in turn delegates the requests based on its configuration for the sub-domain.

If you don't have a DNS setup in your sub-domain, then the admin at example.com needs to set up a CNAME alias. The alias points the subdomain to the same web server, which hosts the website for the parent domain. The canonical names (CNAMES) are added for each of the subdomains. Once the subdomain is resolved to the IP address of the web server, the web server can route the request to a different website.

Share:
13,389
clwen
Author by

clwen

A data geek and Python enthusiast.

Updated on June 04, 2022

Comments

  • clwen
    clwen almost 2 years

    There are several questions on SO about nginx subdomain configuration but didn't find one that exactly the same as mine.

    Say I got a virtual host some.example.com from higher-level net admin example.com at our organization. I want to use some.example.com as my primary site and use foo.some.example.com and bar.some.example.com for auxiliary usage (proxy, etc). I tried this simple configuration and put it under sites-enabled but didn't work:

    server {
        listen 80; 
        server_name some.example.com;
        root /home/me/public_html/some;
        index index.html index.htm;
    }
    
    server {
        listen 80; 
        server_name foo.some.example.com;
        root /home/me/public_html/foo;
        index index.html index.htm;
    }
    
    server {
        listen 80; 
        server_name bar.some.example.com;
        root /home/me/public_html/bar;
        index index.html index.htm;
    }
    

    In this setting some.example.com works fine, but for the other two browser return that could not find foo.some.example.com. I'm running it on a ubuntu server.

    Is there something wrong with this configuration? Or is it something I should talk to higher level net admin (make foo.some.example.com and bar.some.example.com to be registered)?

  • clwen
    clwen over 11 years
    Thanks for replying. So I have two options: 1) build a DNS at some.example.com by my own 2) ask admin at example.com to add CNAMEs for two sub-domain mentioned? If I go with the latter, I will have to ask the admin at example.com every time I want to add a sub-domain?
  • Brian Knight
    Brian Knight over 11 years
    Yes - that's correct. In lieu of a separate DNS, all DNS requests would go to the DNS servers at example.com.
  • clwen
    clwen over 11 years
    If I choose to setup a DNS server by myself, I can point all the three domains in the example to the same IP, and let nginx to figure out which "server" (parent or foo or bar) to use?