Accept CNAME DNS requests thru nginx with multiple domains

Posted August 25, 2014 15.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;


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.

Submit an Answer
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.