Nginx: How to setup multiple virtual hosts (server blocks) on different subdomains?

Viewed 585

EDIT:


Ok, very strange still. It seems that it does not work on my main browser. In incognito browsers or just a completely new chrome window the sites now do work. I guess it has something to do with browser caching?


So I am hosting my website on Digital Ocean and I want to host multiple 'websites' on 1 droplet/server. By multiple websites, I mean different subdomains of my main domain. So I want to host admin.domain.com, dev.domain.com and test.domain.com on the same server. I followed this tutorial, but it is not working like expected. Currently, only 1 subdomain of the 3 is working...

What have I tried so far?

First of, I created 3 A records in my DNS all pointing to the same server_ip droplet on Digital Ocean.

I've created a different a different folder for each subdomain in the /var/www folder, each containing a html folder with a simple index.html file and some html:

website directiories

The image above shows my /var/www folder.

I then used the following command sudo chmod -R 755 /var/www.

Next, I copied the default server block file and used this as the default for a new server block with the following command:

sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/admin.domain.nl

I changed the contents of the file in all 3 config files for the 3 subdomains to the following (obviously changing the root to the specific subdomain aswell as the server_name):

server {
        listen 80;
        listen [::]:80;

        root /var/www/admin.domain.nl/html;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        server_name admin.domain.nl;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }
}

I then used the following command: sudo ln -s /etc/nginx/sites-available/dev.domain.nl /etc/nginx/sites-enabled/ 3 times for the 3 different subdomains to 'enable' the server blocks.

This is my sites-enabled folder:

sites enabled

I've had no syntax errors and thus restarted nginx with: sudo systemctl restart nginx.

The problem

Now, for some very odd reason I do not understand, only the admin.domain.nl site is working. The other 2 subdomains simply display: This site can’t be reached.

What am I missing here?

1 Answers

IN /etc/nginx/nginx.conf

http {

    # ...........................others contents.....................................

    # in bottom
    
    server {
        listen 80;
        root /var/www/html/cmp/api;
        server_name "cmpapi.localhost";
        index index.html index.php;
        location / {
            try_files $uri $uri/ $uri.html $uri.php =404;
        }
    }

    server {
        listen 80;
        root /var/www/html/cmp/frontend;
        server_name "cmp.localhost";
        index index.html index.php;
        location / {
            try_files $uri $uri/ $uri.html $uri.php =404;
        }
    }

}
  1. Here my project name is cmp. here 2 project
    1. one is react frontend
    2. another is laravel api project
  2. Here 2 folder created in
    1. /var/www/html/cmp/api
    2. /var/www/html/cmp/frontend

cmd

sudo find /var/www/html/cmp/frontend/ -type f -exec chmod 644 {} \;  
sudo find /var/www/html/cmp/frontend/ -type d -exec chmod 755 {} \;  

sudo find /var/www/html/cmp/api/ -type f -exec chmod 644 {} \;
sudo find /var/www/html/cmp/api/ -type d -exec chmod 755 {} \;  

systemctl reload nginx 

Browser

Related