Nginx Drupal Multisite – How to Configure Nginx Virtual Host for Drupal Multisite with Sub-directory

centos7drupal8nginxvirtualhost

We've Drupal multi-site (with different domain per site) running with single Drupal 8 code base and Nginx as web-server. Now we wanted to setup Drupal Milti-site with sub-directory for few websites in following format, and "subdirectory" should be case in-sensitive (i.e. site should be accessible with both "subdirectory" or "SubDirectory") :

  1. http://site1.com (Drupal multi-site, served from "sites/site1.com")
  2. http://site1.com/subdirectory (Drupal multi-site, served from "sites/site1.com.subdirectory")
  3. http://site2.com (Drupal multi-site, served from "sites/site2.com")
  4. http://site2.com/subdirectory (Drupal multi-site, served from "sites/site2.com.subdirectory")

We're running these websites on Nginx on CentOS. We've been able to get the sub-site working in following structure (but not with above structure) :

  1. http://site1.com (Static HTML page)
  2. http://site1.com/subdirectory (Drupal multi-site)

Following are nginx virtual host configuration we're using.

NOTE : I've updated nginx config to remove SSL.

# VHOST Config.
server {
  listen 80;
  server_name site1.com;

  root /var/www/html;
  index index.php index.html index.htm;

  # Prevent files from being accessed.
  location = /robots.txt {
    allow all;
    log_not_found off;
    access_log off;
  }
  location ~ /\.htaccess*$ {
    return 403;
  }
  location ~ (^|/)\. {
    return 403;
  }
  location ~ .*\.config$ {
    return 403;
  }
  location ~ /composer.*$ {
    return 403;
  }
  location ~ \..*/.*\.yml$ {
    return 403;
  }

  location / {
    try_files $uri $uri/ =404;
  }
  location @rewrite {
    try_files $uri /subdirectory/index.php?$query_string;
  }
  location /subdirectory/ {
    try_files $uri $uri/ @rewrite;
  }

  location ~ '\.php$|^/update.php' {
    include /etc/nginx/fastcgi_params;
    fastcgi_pass  127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_split_path_info ^(.+?\.php)(|/.*)$;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  }
  location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
    expires max;
    log_not_found off;
    proxy_cache_bypass 1;
  }
}

Any help is greatly appreciated.

Best Answer

Why don't you set the things differently? My idea on what it could work for you:

server {
  listen 80;
  server_name site1.com;

  root /var/www/sites/site1; #I suppose that in site1 it's the code for site1.com
  index index.php index.html index.htm;
.
.
.#simple configuration here, don't care about site1.com/subdomain
.
}

server {
  listen 80;
  server_name site2.com;

  root /var/www/sites/site2; #I suppose that in site2 it's the code for site2.com
  index index.php index.html index.htm;
.
.
.#simple configuration here, don't care about site2.com/subdomain
.
}

AND then , you go inside of /var/www/sites/site1 and /var/www/sites/site2 , and create a symlink to ../site1.com.subdomain ../site2.com.subdomain like this:

cd  /var/www/sites/site1
ln -s ../site1.com.subdomain subdomain
cd /var/www/sites/site2
ln -s ../site2.com.subdomain subdomain

Like this, you have the code for each Drupal inside /var/www/sites/site1 /var/www/sites/site2 /var/www/sites/site1.com.subdomain and /var/www/sites/site2.com.subdomain

But when site1 is accessed, it'll enter /var/www/sites/site1, if site1.com/subdomain is accessed, it'll enter /var/www/sites/site1/subdomain , which following the symlink, it'll bring the connection to /var/www/sites/site1.com.subdomain.

Isn't this what you need? Or I did read wrong the question?