Nginx Vhost shows the default index page and not the one that defined

debiannginx

I recently installed nginx on debian 8.8 server. I have registered a domain name and now I want to create a virtual host to serve it. (In the future I might add more virtual hosts for other domains.)

The problem is that every time I enter http://[my domain] I get the default nginx welcome page rather the one I created under /var/www/[my domain]/public_html.

Bellow I post the commands I typed to create a new vhost:

sudo mkdir -p /var/www/[my domain]/public_html
sudo chown -R www-data:www-data /var/www/[my domain]/public_html
sudo chmod -R 755 /var/www
sudo nano /var/www/[my domain]/public_html/index.html
sudo nano /etc/nginx/sites-available/[my domain]
sudo ln -s /etc/nginx/sites-available/[my domain] /etc/nginx/sites-enabled/[my domain]
sudo rm /etc/nginx/sites-enabled/default
sudo service nginx restart

in the vhost file I added the bellow lines:

server {
    listen         80;
    listen         [::]:80;
    server_name    [my domain] www.[my domain];
}

server {
    listen         80;

    default_type text/plain;
    root   /var/www/[my domain]/public_html;
    index  index.html index.htm;

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

I checked the configurations with sudo nginx -t and no error reported.

I run ufw firewall with the following configurations (in case that someone wants them):

ssh                        ALLOW       Anywhere
Nginx HTTP                 ALLOW       Anywhere
shhh                       ALLOW       Anywhere (v6)
Nginx HTTP (v6)            ALLOW       Anywhere (v6)

Best Answer

The issue is that your virtual host where you specify your domain names has no other content.

You need to delete the first server block and move the server_name directive to the second block.