Nginx Multi-site hosting config

Events happening in the community are now at Drupal community events on www.drupal.org.
jmolinas's picture

Hello i just what to know how setup a multisite using Nginx, eg. www.example.com/site1, www.example.com/site2, etc. I know how to configure in one main root site which is www.example.com

So far www.example.com works but I cannot add additional sub sites

Here's my nginx conf file for reference;

server {
        server_name _;
        root /var/www/example; ## <-- Your only path reference.

        location = /favicon.ico {
                log_not_found off;
                access_log off;
        }
        location = /robots.txt {
                allow all;
                log_not_found off;
                access_log off;
        }

        # This matters if you use drush
        location = /backup {
                deny all;
        }

        # Very rarely should these ever be accessed outside of your lan
        location ~* .(txt|log)$ {
                allow 192.168.0.0/16;
                deny all;
        }

        location ~ ..*/.*.php$ {
                return 403;
        }

        location / {
                # This is cool because no php is touched for static content
                try_files $uri @rewrite;
        }

        location @rewrite {
                # Some modules enforce no slash (/) at the end of the URL
                # Else this rewrite block wouldn't be needed (GlobalRedirect)
                rewrite ^/(.*)$ /index.php?q=$1;
        }

        location ~ .php$ {
                fastcgi_split_path_info ^(.+.php)(/.+)$;
                #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_intercept_errors on;
                fastcgi_pass unix:/tmp/php.socket;
        }

        # Fighting with ImageCache? This little gem is amazing.
        location ~ ^/sites/.*/files/imagecache/ {
                try_files $uri @rewrite;
        }

        location ~* .(js|css|png|jpg|jpeg|gif|ico)$ {
                expires max;
                log_not_found off;
        }
}

Thanks

Comments

I think you need to create a

egomac's picture

I think you need to create a separate 'server' instance for each of the subsites too

server {
        server_name site1;
        root /var/www/example/site1;
.....
.....
}
server {
        server_name site2;
        root /var/www/example/site2;
.....
.....
}

Something like that.

Thank you all so very much.

jmolinas's picture

Thank you all so very much. It's working now.

I just need subfolder sites

jmolinas's picture

I just need subfolder sites /var/www/site1, /var/www/site2, etc. For those who would like to implement this interface here's my config file;

location /site1/ {
        root /var/www/site1;
        index index.php;
        try_files $uri @price;
        fastcgi_split_path_info ^(.+.php)(/.+)$;
       #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
       include fastcgi_params;
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
       fastcgi_intercept_errors on;
       fastcgi_pass unix:/tmp/php.socket;
        }
         location @site1 {
                 rewrite ^/site1/(.)$ /site1/index.php?q=$1;
        }
        location ~ ^/site1/sites/.
/files/imagecache/ {
        try_files $uri @site1;
        }

for those who want to implement multi-site in one nginx box, using subdir sites

Multisite - Sharing the same code base

shenzhuxi's picture

I can't make Multisite - Sharing the same code base with Nginx, like http://drupal.org/documentation/install/multi-site.
The sub domain names are always set to /var/www/drupal7/sites/default;

Only local images are allowed.
Only local images are allowed.

Multisite on Windows or Linux

mohrizmus's picture

If you use on Windows, I could share my knowledge here. I use Perosia's nginx.conf as default nginx setting. But you need to modify the line code for Windows.For drupal.conf, I used the above code because Perosia's drupal.conf is not suitable for Windows or a lot of line code to debug by yourself. Then create the separate virtual host files for every subsites. Edit Windows hosts file to include every subsites ip address. Your root document like this c:/nginx/home/example/drupal_7/, and copy all Drupal 7 files inside this folder, where this is your main Drupal 7 code base. Edit sites.php to create multisite. Create the subsite folder according to Drupal 7 multisite concept.

subsite1.conf:

server {
listen 127.0.0.1:80;
    server_name subsite1;## must tally with the windows hosts domain name

root home/example/public_html/drupal_7;## <-- Your only path reference..relative paths in Windows
       index index.php index.html;
        log_not_found off;
charset utf-8;
if (!-e $request_filename) {
       rewrite ^/(.)$ /index.php?q=$1 last;
      }
  ## Enable compression, this will help if you have for instance advagg module
        ## by serving Gzip versions of the files.
        gzip_static on;
        access_log logs/subsite1-access.log;

   location ~ /. { deny all; }
  
   location = /favicon.ico {
              log_not_found off;
             access_log off; }
  location = /robots.txt {
              allow all;
                log_not_found off;
                access_log off;}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        location ~ .php$ {
    if (!-e $document_root$document_uri){return 404;}
  fastcgi_pass localhost:9000;
   fastcgi_index index.php;
   fastcgi_param SCRIPT_FILENAME    $document_root$fastcgi_script_name;
   include fastcgi_params;
        }
include drupal.conf;
}

subsite2.conf:
server {
listen 127.0.0.1:80;
    server_name subsite2;## must tally with the windows hosts domain name

root home/example/public_html/drupal_7;## <-- Your only path reference..relative paths in Windows
       index index.php index.html;
        log_not_found off;
charset utf-8;
if (!-e $request_filename) {
       rewrite ^/(.)$ /index.php?q=$1 last;
      }
  ## Enable compression, this will help if you have for instance advagg module
    ## by serving Gzip versions of the files.
        gzip_static on;
        access_log logs/subsite1-access.log;

   location ~ /. { deny all; }
  
   location = /favicon.ico {
              log_not_found off;
             access_log off; }
  location = /robots.txt {
              allow all;
                log_not_found off;
                access_log off;}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        location ~ .php$ {
   if (!-e $document_root$document_uri){return 404;}
  fastcgi_pass localhost:9000;
   fastcgi_index index.php;
   fastcgi_param SCRIPT_FILENAME    $document_root$fastcgi_script_name;
   include fastcgi_params;
        }
include drupal.conf;
}

drupal.conf:

## This matters if you use drush prior to 5.x
    ## After 5.x backups are stored outside the Drupal install.
    #location = /backup {
    #         deny all;
    #         }

    ## Very rarely should these ever be accessed outside of your lan
    location ~* .(txt|log)$ {
                #allow 127.0.0.1/80;
                deny all;
             }

    location ~ ../..php$ {
                return 403;
             }

    ## No no for private
    location ~ ^/sites/./private/ {
                return 403;
              }      

    ## Block access to "hidden" files and directories whose names begin with a
    ## period. This includes directories used by version control systems such
    ## as Subversion or Git to store control files.
    location ~ (^|/). {
                return 403;
             } 
    
  ## A basic replacement for the typical mod_rewrite  style file/directory directory-method 1
    ## Clean URLs
  location / {
    ## This is cool because no php is touched for static content
                try_files $uri @rewrite;
             }
  location @rewrite {
    ## You have 2 options here:
    ## For D7 and above:
    ## Clean URLs are handled in drupal_environment_initialize().
                rewrite ^ /index.php;
          
    ## For Drupal 6 and below:
    ## Some modules enforce no slash (/) at the end of the URL
    ## Else this rewrite block wouldn't be needed (GlobalRedirect)
#           rewrite ^/(.
)$ /index.php?q=$1;
               }  
     
    ## A basic replacement for the typical mod_rewrite  style file/directory-method 2
  ## Clean URLs
  ## For Drupal 6 or Drupal 7
    #location / {
  #           try_files $uri $uri/ /index.php?q=$uri&args;
  ## A better version for Drupal 7           
   #           try_files $uri $uri/ /index.php?args;
  #           }
   
    ## Fighting with Styles? This little gem is amazing.
    ## This is for D6
    #location ~ ^/sites/./files/imagecache/ {
    ## This is for D7 and D8
    location ~ ^/sites/.
/files/styles/ {
                try_files $uri @rewrite;
              }
  location ~* .(js|css|png|jpg|jpeg|gif|ico)$ {
                expires max;
                log_not_found off;
             }
 

perosio nginx.conf for windows:
# -- mode: nginx; mode: flyspell-prog;  ispell-local-dictionary: "american" --

# Generic startup file.
# This is equivalent to /etc/nginx/nginx.conf (or /etc/nginx/conf/nginx.conf if you're using Arch Linux).
#user www-data;

## If you're using an Nginx version below 1.3.8 or 1.2. then uncomment
## the line below and set it to the number of cores of the
## server. Otherwise nginx will determine it automatically.
#worker_processes 4;

##If you're using Nginx Windows remove the "/" to refer the other directory
error_log logs/error.log;
pid logs/nginx.pid;

worker_rlimit_nofile 8192;

events {
    worker_connections 4096;
    ## Accept as many connections as possible.
    multi_accept on;
}

http{
    ## MIME types.
    include mime.types;
    default_type application/octet-stream;

    ## FastCGI.
    include fastcgi.conf;

    ## Default log and error files.
    access_log logs/access.log;
    error_log logs/error.log;

    ## Use sendfile() syscall to speed up I/O operations and speed up
    ## static file serving.
    sendfile on;
    ## Handling of IPs in proxied and load balancing situations.
    set_real_ip_from 0.0.0.0/32; # all addresses get a real IP.
    real_ip_header X-Forwarded-For; # the ip is forwarded from the load balancer/proxy

    ## Define a zone for limiting the number of simultaneous
    ## connections nginx accepts. 1m means 32000 simultaneous
    ## sessions. We need to define for each server the limit_conn
    ## value refering to this or other zones.
    ## ** This syntax requires nginx version >=
    ## ** 1.1.8. Cf. http://nginx.org/en/CHANGES. If using an older
    ## ** version then use the limit_zone directive below
    ## ** instead. Comment out this
    ## ** one if not using nginx version >= 1.1.8.
    #limit_conn_zone $binary_remote_addr zone=arbeit:10m;

    ## Define a zone for limiting the number of simultaneous
    ## connections nginx accepts. 1m means 32000 simultaneous
    ## sessions. We need to define for each server the limit_conn
    ## value refering to this or other zones.
    ## ** Use this directive for nginx versions below 1.1.8. Uncomment the line below.
    #limit_zone arbeit $binary_remote_addr 10m;

    ## Timeouts.
    client_body_timeout 60;
    client_header_timeout 60;
    keepalive_timeout 10 10;
    send_timeout 60;

    ## Reset lingering timed out connections. Deflect DDoS.
    reset_timedout_connection on;

    ## Body size.
    client_max_body_size 10m;

    ## TCP options.
    tcp_nodelay on;
    ## Optimization of socket handling when using sendfile.
    tcp_nopush on;

    ## Compression.
    gzip on;
    gzip_buffers 16 8k;
    gzip_comp_level 1;
    gzip_http_version 1.1;
    gzip_min_length 10;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript image/x-icon application/vnd.ms-fontobject font/opentype application/x-font-ttf;
    gzip_vary on;
    gzip_proxied any; # Compression for all requests.
    ## No need for regexps. See
    ## http://wiki.nginx.org/NginxHttpGzipModule#gzip_disable
    gzip_disable msie6;

    ## Serve already compressed files directly, bypassing on-the-fly
    ## compression.
    ##
    # Usually you don't make much use of this. It's better to just
    # enable gzip_static on the locations you need it.
    # gzip_static on;

    ## Hide the Nginx version number.
    server_tokens off;

    ## Use a SSL/TLS cache for SSL session resume. This needs to be
    ## here (in this context, for session resumption to work. See this
    ## thread on the Nginx mailing list:
    ## http://nginx.org/pipermail/nginx/2010-November/023736.html.
    #ssl_session_cache shared:SSL:30m;
    #ssl_session_timeout 1d;

    ## The server dictates the choice of cipher suites.
    #ssl_prefer_server_ciphers on;

    ## Use only Perfect Forward Secrecy Ciphers. Fallback on non ECDH
    ## for crufty clients.
    #ssl_ciphers ECDH+aRSA+AESGCM:ECDH+aRSA+SHA384:ECDH+aRSA+SHA256:ECDH:EDH+CAMELLIA:EDH+aRSA:+CAMELLIA256:+AES256:+CAMELLIA128:+AES128:+SSLv3:!aNULL:!eNULL:!LOW:!3DES:!MD5:!EXP:!PSK:!SRP:!DSS:!RC4:!SEED:!ECDSA:CAMELLIA256-SHA:AES256-SHA:CAMELLIA128-SHA:AES128-SHA;

    ## No SSL2 support. Legacy support of SSLv3.
    #ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;

    ## Pregenerated Diffie-Hellman parameters.
    #ssl_dhparam /etc/nginx/dh_param.pem;

    ## Curve to use for ECDH.
    #ssl_ecdh_curve secp521r1;

    ## Enable OCSP stapling. A better way to revocate server certificates.
    #ssl_stapling on;
    ## Fill in with your own resolver.
    #resolver 8.8.8.8;

    ## Uncomment to increase map_hash_bucket_size. If start getting
    ## [emerg]: could not build the map_hash, you should increase
    ## map_hash_bucket_size: 64 in your
    ## logs. Cf. http://wiki.nginx.org/NginxOptimizations.
    #map_hash_bucket_size 192;

    ## Uncomment one of the lines below if you start getting this message:
    ## "[emerg] could not build the variables_hash, you should increase
    ## either variables_hash_max_size: 512 or variables_hash_bucket_size: 64"
    ## You only need to increase one. Increasing variables_hash_max_size to 1024
    ## was recommended in nginx forum by developers.
    ## See this forum topic and responses
    ## http://forum.nginx.org/read.php?2,192277,192286#msg-192286
    ## See http://wiki.nginx.org/HttpCoreModule#variables_hash_bucket_size
    ## The line variables_hash_bucket_size was added for completeness but not
    ## changed from default.
    #variables_hash_max_size 1024; # default 512
    #variables_hash_bucket_size 64; # default is 64

    ## For the filefield_nginx_progress module to work. From the
    ## README. Reserve 1MB under the name 'uploads' to track uploads.
    #upload_progress uploads 1m;

    ## Enable the builtin cross-site scripting (XSS) filter available
    ## in modern browsers.  Usually enabled by default we just
    ## reinstate in case it has been somehow disabled for this
    ## particular server instance.
    ## https://www.owasp.org/index.php/List_of_useful_HTTP_headers.
    #add_header X-XSS-Protection '1; mode=block';

    ## Enable clickjacking protection in modern browsers. Available in
    ## IE8 also. See
    ## https://developer.mozilla.org/en/The_X-FRAME-OPTIONS_response_header
    ## This may conflicts with pseudo streaming (at least with Nginx version 1.0.12).
    ## Uncomment the line below if you're not using media streaming.
    ## For sites being framing on the same domqin uncomment the line below.
    #add_header X-Frame-Options SAMEORIGIN;
    ## For sites accepting to be framed in any context comment the
    ## line below.
    #add_header X-Frame-Options DENY;

    ## Block MIME type sniffing on IE.
    add_header X-Content-Options nosniff;

    ## Include the upstream servers for PHP FastCGI handling config.
    ## This one uses the FCGI process listening on TCP sockets.
    #include upstream_phpcgi_tcp.conf;

    ## Include the upstream servers for PHP FastCGI handling
    ## configuration. This setup uses UNIX sockets for talking with the
    ## upstream.
    #include upstream_phpcgi_unix.conf;

    ## Include the map to block HTTP methods.
    #include map_block_http_methods.conf;

    ## If using Nginx version >= 1.1.11 then there's a $https variable
    ## that has the value 'on' if the used scheme is https and '' if not.
    ## See: http://trac.nginx.org/nginx/changeset/4380/nginx
    ## http://trac.nginx.org/nginx/changeset/4333/nginx and
    ## http://trac.nginx.org/nginx/changeset/4334/nginx. If using a
    ## previous version then uncomment out the line below.
    #include map_https_fcgi.conf;

    # Support the X-Forwarded-Proto header for fastcgi.
    map $http_x_forwarded_proto $fastcgi_https {
      default $https;
      http '';
      https on;
    }
     
   ## Include the upstream servers for Apache handling the PHP
    ## processes. In this case Nginx functions as a reverse proxy.
    #include reverse_proxy.conf;
    #include upstream_phpapache.conf;

    ## Include the php-fpm status allowed hosts configuration block.
    ## Uncomment to enable if you're running php-fpm.
    #include php_fpm_status_allowed_hosts.conf;

    ## Include the Nginx stub status allowed hosts configuration block.
    #include nginx_status_allowed_hosts.conf;

    ## If you want to run cron using Drupal cron.php. i.e., you're not
    ## using drush then uncomment the line below. Specify in
    ## cron_allowed_hosts.conf which hosts can invole cron.
    # include apps/drupal/cron_allowed_hosts.conf;

    ## Include blacklist for bad bot and referer blocking.
    #include blacklist.conf;

    ## Include the caching setup. Needed for using Drupal with an external cache.
    #include apps/drupal/map_cache.conf;

    ## Microcache zone definition for FastCGI.
    #include fastcgi_microcache_zone.conf;

    ## If you're using Apache for handling PHP then comment the line
    ## above and uncomment the line below.
    #include proxy_microcache_zone.conf

    ## Include all vhosts.
   ## The correct function using wildcard .
  include sites-enabled/
.conf;
    #include sites-enabled/localhost.conf;
   include phpmyadmin.conf;
   include adminer.conf;
  include memcachedadmin.conf;
   
}

Restart nginx.
Install each subsite ie http:\subsite1, http:\subsite2.

Philippines

Group organizers

Group categories

Post category

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: