skip to Main Content

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


  1. Chosen as BEST ANSWER

    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:

    # Default server configuration
    #
    server {
        listen *:80 default_server;
        listen [::]:80 default_server;
        server_name domain1.com www.domain1.com;
        index index.js index.html index.htm;
    
            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 {
        listen 80;
        server_name domain2.com www.domain2.com;
            location / {
                root /home/domain2/;
                index index.html index.htm;
            }
    
    }
    

    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.


  2. 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?

    Login or Signup to reply.
  3. 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"

    server {
    
        server_name domain1.com www.domain1.com;
    
       location / {
         proxy_pass http://localhost:5000;
         proxy_http_version 1.1;
         proxy_set_header Upgrade $http_upgrade;
         proxy_set_header Connection keep-alive;
         proxy_set_header Host $host;
         proxy_cache_bypass $http_upgrade;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_set_header X-Forwarded-Proto $scheme;
        }
    
    listen [::]:443 ssl; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/domain1.com/fullchain`enter code here`.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/domain1.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 = www.domain1.com) {
        return 301 https://$server_name$request_uri;
    } # managed by Certbot
    
    
    if ($host = domain1.com) {
        return 301 https://$server_name$request_uri;
    } # managed by Certbot
    
    
        listen 80;
        listen [::]:80;
        server_name domain1.com www.domain1.com;
        
    return 404; # managed by Certbot
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search