Question
Multiple Node.js apps on NginX resolve to wrong subdomain
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?
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.
×