Setup drupal 8 multisite on nginx with different domain

How to setup Drupal 8 multisite on an Nginx web server using different domains? In this example, we are going to use three different domains for our Drupal-8 multi-site ex: drupal-main.com, drupal-first.com, and drupal-second.com. Here, drupal-main.com will be the main site pointing to our Drupal-8 installation, while two others will be pointing to the directories site1 and site2 respectively.

The directory structure will be as follows:

/var/www/drupal-main.com/public - This will contain drupal-8 installation
/var/www/drupal-main.com/public/sites/site1
/var/www/drupal-main.com/public/sites/site2

Create the above directory structure on your server. Now create a copy of sites/default/default.settings.php to sites/default/settings.php and also make a copy of example.sites.php to sites.php available in the sites folder and put the following site alias at the end of the file:

$sites['drupal-first.com'] = 'site1';
$sites['drupal-second.com'] = 'site2';

After that, make a copy of default.service.yml and default.settings.php as follows:

cp default/default.settings.php site1/settings.php
cp default/default.settings.php site2/settings.php
cp default/default.services.yml site1/services.yml
cp default/default.services.yml site2/services.yml 

Now, create a server block for the three separate sites as follows:

cd /etc/nginx.conf.d
sudo vi drupal-main.conf 

 and add the following configuration:

server {
  listen   80;
  server_name  www.drupal-main.com;
  rewrite ^/(.*) http://drupal-main.com/$1 permanent;
}
server {
  listen   80;
  server_name drupal-main.com;
  root   /var/www/drupal-main.com/public/;
  index  index.php;
  # For Drupal >= 7 clean url
  location / {
    try_files $uri /index.php?$query_string; 
  }

  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
  }
}

sudo vi drupal-first.conf and add the below text to it.

server {
  listen   80;
  server_name  www.drupal-first.com;
  # rewrite ^/(.*) http://drupal-first.com/$1 permanent;
}

server {
  listen   80;
  server_name drupal-first.com;

  root   /var/www/drupal-main.com/public/;
  index  index.php;

  # For Drupal >= 7 clean url
  location / {
    try_files $uri /index.php?$query_string; 
  }

  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
  }
}

sudo vi drupal-second.conf and add the below text to it.

server {
  listen   80;
  server_name  www.drupal-second.com;
  rewrite ^/(.*) http://drupal-second.com/$1 permanent;
}
server {
  listen   80;
  server_name drupal-second.com;

  root   /var/www/drupal-main.com/public/;
  index  index.php;

  # For Drupal >= 7 clean url
  location / {
    try_files $uri /index.php?$query_string; 
  }

  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
  }
}

Finally, add an entry to the /etc/hosts file as follows, and restart your NGINX server.

# drupal sites
127.0.1.1    drupal-main.com www.drupal-main.com
127.0.1.1    drupal-first.com www.drupal-first.com
127.0.1.1    drupal-second.com www.drupal-second.com
0/5