jbweb
By:
jbweb

Multiple Node.js apps on NginX resolve to wrong subdomain

March 18, 2015 3.4k views
Node.js Nginx DNS Ubuntu

I'm fairly new here, so bear with me.

I just created a Ubuntu 14.0.4 droplet and installed NginX and Node.js.
Two domains are configured with the D.O. nameservers, and two DNS entries are added on D.O.

$ORIGIN domain1.com.
$TTL 1800
domain1.com. IN SOA ns1.digitalocean.com. hostmaster.domain1.com. xxxxxxxxxx xxxxx xxxx xxxxxx xxxx
domain1.com. 1800 IN NS ns1.digitalocean.com.
domain1.com. 1800 IN NS ns2.digitalocean.com.
domain1.com. 1800 IN NS ns3.digitalocean.com.
domain1.com. 1800 IN A 1.2.3.4
*.domain1.com. 1800 IN CNAME domain1.com.
$ORIGIN domain2.com.
$TTL 1800
domain2.com. IN SOA ns1.digitalocean.com. hostmaster.domain2.com. xxxxxxxxxx xxxxx xxxx xxxxxx xxxx
domain2.com. 1800 IN NS ns1.digitalocean.com.
domain2.com. 1800 IN NS ns2.digitalocean.com.
domain2.com. 1800 IN NS ns3.digitalocean.com.
domain2.com. 1800 IN A 1.2.3.4
*.domain2.com. 1800 IN CNAME domain2.com.

Both domains have the * => @ CNAME wildcard.

On NginX there are two files (one for every domain) on sites-enabled (and of course sites-available):
domain1.com

upstream app_domain1 {
        server 127.0.0.1:3000;
}
server {
        listen 80;
        server_name domain1.com;
        access_log /var/log/nginx/domain1.log;
        location / {
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $http_host;
                proxy_set_header X-NginX-Proxy true;
                proxy_pass http://app_domain1;
                proxy_redirect off;
        }
}

domain2.com

upstream app_domain2 {
        server 127.0.0.1:4000;
}
server {
        listen 80;
        server_name domain2.com;
        access_log /var/log/nginx/domain2.log;
        location / {
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $http_host;
                proxy_set_header X-NginX-Proxy true;
                proxy_pass http://app_domain2;
                proxy_redirect off;
        }
}

Two simple app.js files with some "Hello World!" responses run with forever, listening on port 3000 & 4000 as stated in the upstream part of above NginX files.

When i browse to domain1.com or domain2.com, i get the response i would expect.
When i browse to www.domain2.com (the second domain) it's also the correct response.

But when i browse to www.domain1.com, i get the response of domain2.com.

What am i doing wrong?

3 Answers

If you have not explicitly defined a default_server the first server block is chosen. Since neither of your server blocks contain the www prefix the default server is used. You can capture the naked domain and the www explitly or you can capture naked and all subdomains like this:

server_name .domain1.com;
  • Thanks for the quick response.
    I added the dot on both NginX files and restarted.
    That did the trick!

    Thank you

The above answer was working for domain 1 and 2 redirecting to two nodejs services.
But now i redirected 2 new domains to my D.O. server (e.g. domain3.com & domain4.com).

These domains still only resolve to the first server block.

After adding a new DNS record for both new domains and adding the names to server_name part of NginX, still domain 3 and 4 did resolve to the domain 2 block.

p.s. domain1 should ad up to domain3 and domain2 to domain4

I tried the following server name configurations:

server_name .domain1.com .domain3.com;

and

server_name domain1.com domain3.com www.domain1.com www.domain3.com;

and finally created 2 server parts, going to the same app and listening to the same port.

Have another answer? Share your knowledge.