1
votes

I'm trying to set up a multisite environment in drupal using subdirectories. There's a very good set of instructions here: http://www.drupalcoder.com/blog/drupal-multisite-in-subfolders, but it's specific to apache and I'm working with Nginx.

how do I do these 2 things in Nginx:

quoting from link above:

Add alias your Apache configuration file

We want requests for the 3 subdirectories to go to the same Drupal instance. We can do this using Apache's Alias functionality.

Alias /subdir1 /var/www
Alias /subdir2 /var/www
Alias /subdir3 /var/www

I'm supposing here Drupal's codebase is hosted in /var/www on your machine.

Redirecting all requests to index.php

Now that we're serving all requests from one codebase we have to redirect all requests to index.php. This needs to be done since all Drupal requests are served from one endpoint, called index.php.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/subdir1/(.*)$
RewriteRule ^(.*)$ /subdir1/index.php?q=$1 [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/subdir2/(.*)$
RewriteRule ^(.*)$ /subdir2/index.php?q=$1 [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/subdir3/(.*)$
RewriteRule ^(.*)$ /subdir3/index.php?q=$1 [L,QSA]
2
I've tried both the below mentioned solutions but none of them worked for me. I'm struggling with the same issue!! - Rishi Kulshreshtha

2 Answers

0
votes

This should work (did not check) for alias:

location ~ ^/subdir\d+/(.*)$ {
    alias /var/www;
}

and something like this for rewrites & fastcgi-php:

location / {
    root   /var/www;
    index  index.php index.html;

    if (!-f $request_filename) {
        rewrite  ^(.*)$  /index.php?q=$1  last;
        break;
    }

    if (!-d $request_filename) {
        rewrite  ^(.*)$  /index.php?q=$1  last;
        break;
    }
}

location ~ .php$ {
    fastcgi_pass   127.0.0.1:9000;  
    fastcgi_index  index.php;

    fastcgi_param  SCRIPT_FILENAME  /var/www$fastcgi_script_name;
    fastcgi_param  QUERY_STRING     $query_string;
    fastcgi_param  REQUEST_METHOD   $request_method;
    fastcgi_param  CONTENT_TYPE     $content_type;
    fastcgi_param  CONTENT_LENGTH   $content_length;
}
0
votes

I would use a map to capture everything after /subdir as a variable at the http level. Then you can avoid regex locations which introduce position dependencies.

map $uri $params {
    ~^/[^/]+/(?<subpath>.*) $subpath;
}

server {
    root /var/www;

    location /subdir1/ {
        try_files $uri $uri/ /subdir1/index.php?q=$params
    }

    location /subdir2/ {
        try_files $uri $uri/ /subdir2/index.php?q=$params
    }

    location /subdir3/ {
        try_files $uri $uri/ /subdir3/index.php?q=$params
    }
}