Nginx and Drupal7 site in subdirectory

David G - DrupalRecently I wanted to run a 2nd drupal project on an existing domain. I didn’t want to pay for 2nd domain name so I wanted to place the project in a directory of the parent domain. Usually I have little issue doing this using the Apache webserver as I do this all the time (it might take me all of 5 minutes). But, my personal website runs on the Nginx server and so I found it took me quite a bit of time to configure this drupal site to run within a subdirectory. Here are few steps it took to accomplish this deployment.

Drupal sites.php File Configuration

Drupal supports a multi-site installation. By default there is a naming convention that can route URLs to a sites/ directory to discover new website installations easily. In recent projects I’ve preferred to use non-url based sites/ directory layouts — as I name projects with a sane human name as opposed to simply a URL.

In my sites.php file I have:

$sites[''] = 'bigbangtheory';

This reads as resolve the url path to the drupal site installation of sites/bigbangtheory. This project is regarding the popular TV show The Big Bang Theory! 😀

By adding this line to sites.php Drupal is now aware of the sub-site. Unfortunately, Nginx sends all requests of to the default index.php of the drupal site installation directory.

Nginx Configuration File Additions

       location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                index index.php;
                try_files $uri $uri/ @rewrite;
                expires max;
                # Uncomment to enable naxsi on this location
                # include /etc/nginx/naxsi.rules

        // These 2 location entries make Nginx aware of drupal in a subdirectory.
        location /bbt {
         try_files $uri $uri/ /bbt/index.php?$args;
        location ~ /bbt/.+\.(jpg|jpeg|gif|css|png|js|ico|xml)$ {
                root /usr/share/nginx/html;

Now with the addition of these rules, and restarting Nginx our site will resolve. But if we look closely we’ll see some things will not work! 🙁

To fix these errors we must further tweak our system configuration. Drupal resolves the /bbt/* urls to the drupal subsite, but it cannot see some of the drupal core modules because the location entry in the Nginx configuration is a faux drupal root installation directory!

Nginx resolves our page but it appears broken, there are no images or working CSS.

Nginx resolves our page but it appears broken, there are no images or working CSS.

Example of misconfiguration errors by the system.

Example of misconfiguration errors by the system.

Drupal webroot symlink to fix Errors

In the above errors you’ll see that the /modules folder and css assets are not found by the system. This is because we created the /bbt folder as a fake drupal root installation folder. So a fix for this error is to symlink the bbt folder path on the server to the drupal root directory. Here is how that looks like from the command line:

# Add a symlink of bbt to effectively our self.
root@localhost:/usr/share/nginx/html# ln -s /usr/share/nginx/html bbt

# Assure that the bbt path has the same ownership rules as the
# rest of Drupal.
root@localhost:/usr/share/nginx/html# chown dgurba:www-data bbt

# Example LS output for clarity:
root@localhost:/usr/share/nginx/html# ls -lh
total 272K
-rw-r-----  1 dgurba www-data  537 Mar  4  2014 50x.html
-rw-r-----  1 dgurba www-data 6.5K Sep 20 21:34 authorize.php
lrwxrwxrwx  1 dgurba www-data  21 Sep 25 01:58 bbt -> /usr/share/nginx/html
-rw-r-----  1 dgurba www-data  97K Sep 20 21:34 CHANGELOG.txt
-rw-r-----  1 dgurba www-data 1.5K Sep 20 21:34 COPYRIGHT.txt
-rw-r-----  1 dgurba www-data  720 Sep 20 21:34 cron.php
drwxr-x---  4 dgurba www-data 4.0K Sep 20 21:30 includes
-rw-r-----  1 dgurba www-data  612 Mar  4  2014 index.html

Now our website appears as desired without errors with all this configuration!

The site fully works after these 3 steps.

The site fully works after these 3 steps.

The only reason we see Access Denied here from Drupal is that the site does not allow access to content to anyone except logged-in authenticated users.

Looking for quality web hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , | Posted under Drupal | RSS 2.0

Author Spotlight

David Gurba

I am a web programmer currently employed at UCSB. I have been developing web applications professionally for 8+ years now. For the last 5 years I’ve been actively developing websites primarily in PHP using Drupal. I have experience using LAMP and developing data driven websites for clients in aviation, higher education and e-commerce. If you’d like to contact me I can be reached at

Leave a Reply

Your email address will not be published. Required fields are marked *