0

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.

0

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?

  • Drupal by default support multi site, bcoz of which I'm using same code base. I can not create different vhost bcoz for multisite with subdirectory, I need same domain, like mentioned in question. – Yogesh Jul 5 '19 at 16:04
0

Following is working solution for Drupal multi-site with sub-directory.

# VHOST Config.
server {
  # Define server name and directory root.
  server_name site1.com;
  root /var/www/html/drupal;
  index index.php;

  # Listen to port
  listen 80;

  location / {
    try_files $uri $uri/ /index.php?$query_string;
  }
  location ^~ /subdirectory {
    try_files $uri $uri/ @nested;

    location ~ '\.php$|^/update.php' {
      # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
      # include snippets/fastcgi-php.conf;
      fastcgi_split_path_info ^(.+?\.php)(|/.*)$;
      include /etc/nginx/fastcgi_params;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      fastcgi_intercept_errors on;
      fastcgi_pass  127.0.0.1:9000;
    }
  }
  location @nested {
    try_files $uri $uri/ /subdirectory/index.php?$query_string;
  }

  location @rewrite {
    rewrite ^/(.*)$ /index.php?q=$1;
  }
  location ~ '\.php$|^/update.php' {
    # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
    # include snippets/fastcgi-php.conf;
    fastcgi_split_path_info ^(.+?\.php)(|/.*)$;
    include /etc/nginx/fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_intercept_errors on;
    fastcgi_pass  127.0.0.1:9000;
  }
  location ~ ^/sites/.*/files/styles/ {
    try_files $uri @rewrite;
  }
  location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
    expires max;
    log_not_found off;
    proxy_cache_bypass 1;
  }

}

NOTE :

  1. Above configuration works for both "site1.com" and "site1.com/subdirectory" to server pages using Drupal 8.
  2. Only issue pending is, Drupal updates doesn't work for "site1.com/subdirectory" with above config (site1.com/subdirectory/update.php works but pages after it doesn't work). For it to work, I've to make following change in vhost

      # Change try_files in 'location @nested' as below :
      location @nested {
        try_files $uri $uri/ /subdirectory/update.php?$query_string;
      }
    

Any help is appreciated, to fix above issue and get everything working without need to change in location @nested.

Hope this helps someone in need to get the Drupal 8 multi-site as sub-directory up and running.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.