wodby / nginx

Nginx docker container image
https://wodby.com/stacks
MIT License
72 stars 51 forks source link

Drupal multisite as subfolder #95

Open heyyo-droid opened 3 weeks ago

heyyo-droid commented 3 weeks ago

I'm struggling to create multisites as subfolders in a Drupal 10.

For example my Drupal website has this url: http://d10.docker.localhost

And my multisites should be of this form:

So inside my sites.php I have:

<?php

$sites["d10.docker.localhost.site1"] = "site1";
$sites["d10.docker.localhost.site2"] = "site2";

And each multisites have their own folder under /var/www/html/web/sites with settings.php with their own database (for now empty)

According Drupal 7 documentation (Doc for Drupal 8/9/10/11 was not updated), we also need to create symlinks for each multisite in web/ folder pointing to web.

So from /var/www/htm/web I did

ln -s . site1
ln -s . site2
chown -h 1000:82 site1 # to get correct owner/group
chown -h 1000:82 site2

If I test drush status inside multisite folder, I can see correct multisite detected.

So for Drupal, multisite seems correctly declared.

But when I browse urls:

  1. http://d10.docker.localhost/site1 returns a 404 from Drupal.
  2. http://d10.docker.localhost/site1/core/install.php returns a 404 from nginx.
  3. http://d10.docker.localhost/site1/core/misc/druplicon.png I got a 200 response

So according 3., it seems nginx follows correctly the symlinks, but according 2. and 3. it seems there is something wrong with priority and symlinks.

heyyo-droid commented 3 weeks ago

If we replace nginx container with wodby apache, it works as expected.

heyyo-droid commented 3 weeks ago

@csandanov it looks like related to fastcgi_param SCRIPT_FILENAME $document_root/index.php; $document_root is always /var/www/html/web but for multisite we need /var/www/html/web/site1 for example.

it looks like the variable $realpath_root fixed this according this article: https://joshtronic.com/2019/07/29/symlinks-with-nginx-and-php-fpm/

https://nginx.org/en/docs/http/ngx_http_core_module.html#var_realpath_root

heyyo-droid commented 3 weeks ago

So in the meantime, it's fixed in wodby nginx itself, I tried to use custom preset to see if my theory is exact.

So I created a preset called multisite from a copy of drupal 10 preset file. And also a corresponding upstream config file.

By using this custom preset website is still working :-)

      NGINX_SERVER_ROOT: /var/www/html/web
      # NGINX_VHOST_PRESET: $NGINX_VHOST_PRESET
      NGINX_VHOST_PRESET: "multisite"
    volumes:
    - ./:/var/www/html:cached
    - ./nginx/multisite.conf.tmpl:/etc/gotpl/presets/multisite.conf.tmpl
    - ./nginx/upstream.multisite.conf.tmpl:/etc/gotpl/includes/upstream.multisite.conf.tmpl

Now it looks like replacing $document_root by $realpath_root is far from being enough...

For example those locations won't work for subfolders, so need to be also modified to allow any subfolder prefix, that could potentially open more security issues:

location = /index.php {
...
location = /core/install.php {
...
location = /core/rebuild.php {
...
location ~* ^/core/authorize.php {
...
location = /cron {
...
location ~* ^/update.php {
...