Home > front end >  Why does one domain shows another domain's html page after using Nginx in Ubuntu?
Why does one domain shows another domain's html page after using Nginx in Ubuntu?

Time:11-04

Okay so I have rather an unusual issue in my Ubuntu VPS. Visiting one domain shows the static html page of another domain. Let's look at the congif.

Suppose I have a site with domain: site1.com. This domain has 2 apps running perfectly in subdomains: app1.site1.com and app2.site1.com. The homepage doesn't have any apps but a simple html page which says:

'Welcome to Site 1. This site is currently under maintenance!'.

That is running perfectly as I wanted but the issue arose after I pointed a fresh domain (let's call him site2.com) from Namecheap to my VPS. Now when I visit site2.com, I can see:

'Welcome to Site 1. This site is currently under maintenance!'.

That is some real vodoo magic right there.

Here's how site1 nginx config file looks like:

server {
        root /var/www/html/site1.com;

        index index.html index.htm index.nginx-debian.html;

        server_name site1.com www.site1.com;

        location / {
                try_files $uri $uri/ =404;
        }
}

server {
        root /var/www/app1.site1.com;

        index index.html index.htm index.nginx-debian.html;

        server_name app1.site1.com;

        location / {
                #try_files $uri $uri/ =404;
                proxy_pass http://localhost:4000;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_set_header X-Forwarded-For $http_x_forwarded_for;
                proxy_cache_bypass $http_upgrade;
        }
}

server {
        root /var/www/app2.site1.com;

        index index.html index.htm index.nginx-debian.html;

        server_name app2.site1.com;

        location / {
                #try_files $uri $uri/ =404;
                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_set_header X-Forwarded-For $http_x_forwarded_for;
                proxy_cache_bypass $http_upgrade;
        }
}

The html file is in /var/www/html/site1.com/index.html

What could be the reason for the magic?

I have also tried by creating a new nginx file for the new domain and create a new server block and point to its own html file, but it just doesn't take show that html page but rather the site1 page.

It'd be really helpful if someone points me to the right direction.

CodePudding user response:

This usually happens when Nginx is unable to find a server block for the domain which is showing another page. So Nginx automatically serves the first available config file which is in your case is site1's page.

To avoid this situation, you can set up a default server in nginx.conf file with this config:

server {
    listen 80 default_server;
    listen 443 ssl default_server;
    server_name _;
    ssl_certificate <path to cert>
    ssl_certificate_key <path to key>
    return 404;
}

When Nginx does not find any server block which matches your domain, it will automatically serve a 404 page.

So do these steps:

  1. Add an html page in /var/www/html/site2.com.
  2. Create a config file for site2 and point it to your html page in server block.
  3. Install SSL.
  4. Restart Nginx.

Another reason your issue could be happening is because of SSL. So make sure you try again by reinstalling SSL to site2.

Refer to this and this for more information.

  • Related