Accept CNAME DNS requests thru nginx with multiple domains

August 25, 2014 8.5k views

Ok, The title sounds very problematic…
My current stack is a single server, with nginx as front server and a nodejs server in the back along side some apache virtual hosts.

The nginx has multiple virtual hosts per domain. everything works just fine.

This is where it gets complicated.
One of my hosted nodejs apps accepts subdomains as var
(i.e this is proxy_forward to http://nodejs:port/t/$subdomain which works great.

What i want todo is offer the option to let my customers put a DNS CNAME record on their DNS server that will take =>

When i did a test for this, the default first nginx virtual host loaded up and not the content.

What can i do?

This is the subdomains server block on nginx:

listen 80; 

    server_name   ~^([a-z0-9]+)\.pages\.bootlander\.com$;
    set $sub $1;
    location / {

    proxy_set_header X-Real-IP  $remote_addr;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header Host $http_host;
    proxy_pass http://nodejs:port/t/$sub;

1 Answer

You will need to set server_name to whatever record your customers use to reach the site, otherwise nginx will load the default site. Your config at the moment will only work if a user directly enters * in their browser.

  • But the domains change, how can i make that dynamic?

  • Old thread but thought I should comment.

    Not a direct answer but more of a workaround, but you can you all requests for the pages subdomain and then have node route to the specific page ID by parsing the original header request, maybe forward to a specific subdomain ie;

Have another answer? Share your knowledge.