I am slogging through getting two domains to properly served through NGINX, on digitalocean.com hosting (in a Ubuntu 20,04 Droplet). Let’s call them domain1.com (default) and domain2.com. domain1.com is working properly and has SSL (from Let’s Encrypt), domain2 is just http at this point.
The home page of domain2.com loads fine now, using this server block:
server {
listen *:80;
listen [::]:80;
root /home/domain2/;
index index.html;
server_name domain2.com www.domain2.com;
location / {
try_files $uri $uri/ =404;
}
}
However, when I navigate to a different page on the domain2.com site, it loads the default site domain1.com. Quite odd to see domain2.com in the Chrome address line with domain1.com web pages coming up.
I assume this has something to do with domain1.com being default but I don’t know why this is happening. I’m pretty novice at NGINX… what should I do to fix this?
The default file in /etc/nginx/sites-available has this content:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /home/domain1/server/public;
index index.js index.html index.htm index.nginx-debian.html;
server_name localhost;
location / {
proxy_pass http://localhost:5000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
server {
root /home/domain1/server/public;
index index.js index.html index.htm index.nginx-debian.html;
server_name domain1.com; # managed by Certbot
location / {
proxy_pass http://localhost:5000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/intraprem.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/intraprem.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = domain1.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80 ;
listen [::]:80 ;
server_name domain1.com;
return 404; # managed by Certbot
}
3
Answers
After a number of attempts to work from the previous nginx configuration I had that was based on a configuration that was working fine for domain1.com (i.e. trying to add domain2.com as an incremental change), I decided to clean the slate and start over with a fresh configuration that only uses port 80 for both domains (no SSL yet - see below). I also decided to simplify by just using a single .conf file rather than splitting server blocks into the sites-available and sites-enabled directories, which may have been part of the problem.
Here is the content of the new .conf file, which I placed in /etc/nginx/conf.d/sites.conf:
This is working properly both for the VueJS app that is served by NodeJS on port 5000 and the simple static site that domain2.com references. With this simple approach I am beginning to understand better how nginx works.
My plan is to add back SSL support later in development, for both sites, using Let's Encrypt.
When I get this done I will post the answer here, as I think it would be useful to nginx newbies (like me) who may be struggling with the multiple domains on a single server/port issue.
Are you sure you’re using http://domain2.com? From your configuration, it looks like https:/domain2.com would go to the /home/domain1/server/public web root. Could that be the issue?
You can use the below configuration which is a working one for asp.net core….. The same configuration you can create for domain2.com by replacing "domain1.com" with "domain2.com"