Recommended subdomain multisite nginx vhost configuration with new web/ layout

What’s the proper configuration for an nginx vhost with the new directory structure?

Nothing has changed, you just need to add web to the root:

root       /srv/www/domain.com/web;

Sorry - I guess the problems I’m having are with a subdomain multisite installation. I can’t seem to get a working rewrite rule for the root domain wo /wp/ for stuff like wp-admin, wp-content, etc so the primary domain works properly. I know this is beyond the scope of bedrock, but any help would be appreciated. I’ve searched quite a bit but can’t seem to find a set of rules for this specific application.

For what it’s worth, I have the following so far:

location / {
  try_files $uri $uri/ /wp$uri/ /wp/index.php?q=$uri&$args;
}

location /wp {
  index index.php;
  try_files $uri $uri/ /wp/index.php;
}

location ~ ^/wp-content/cache/minify/(.+\.(css|js))$ {
  try_files $uri /wp/wp-content/plugins/w3-total-cache/pub/minify.php?file=$1;
}

location ~ \.php$ {
  try_files $uri /wp/index.php =404;
  include fastcgi_params;
  fastcgi_pass php;

  fastcgi_cache_bypass $skip_cache;
  fastcgi_no_cache $skip_cache;

  fastcgi_cache WORDPRESS;
}
1 Like

No one else uses bedrock with multisite on nginx :frowning:

Any pointers from nginx gurus would be much appreciated :slight_smile:

I don’t have much experience with multisite, but…

Your try_files in both / and ~ \.php$ shouldn’t be going to /wp/index.php.

You just want the normal index.php in your web root directory. Not sure you need that /wp location either.

There’s lot of resources written about this. Example: https://www.digitalocean.com/community/articles/how-to-configure-single-and-multiple-wordpress-site-settings-with-nginx

edit: was also confused why you had W3TC in your /wp/ dir instead of /app/plugin

location / {
  try_files $uri $uri/ /index.php?q=$uri&$args;
}

location ~ ^/wp-content/cache/minify/(.+\.(css|js))$ {
  try_files $uri /app/plugins/w3-total-cache/pub/minify.php?file=$1;
}

location ~ \.php$ {
  try_files $uri /index.php =404;
  include fastcgi_params;
  fastcgi_pass php;

  fastcgi_cache_bypass $skip_cache;
  fastcgi_no_cache $skip_cache;

  fastcgi_cache WORDPRESS;
}

I see your point re: / I was trying to reconcile the following line in the htaccess that worked for me:

RewriteRule ^(.*\.php)$ wp/$1 [L]

I guess the primary thing that’s left to contend with is this line:

RewriteRule ^(wp-(content|admin|includes).*) wp/$1 [L]

Isn’t that rewrite something that any of the installations has to deal with, even if not multisite? I really apologize for my ignorance in the realm of nginx. I usually rely on htaccess files to control it all, but read in a few locations where you guys relied on nginx and put everything in the vhost configuration so you didn’t have to worry about replicating the environments and symlinking the htaccess files.

Honestly I’m not really sure. I’d probably search for some resources like the Digital Ocean one I posted and start from scratch.

I tried that prior. These work for a standard multisite installation, but they don’t seem to work with the bedrock folder structure. I know I’m probably just being dense - but I can’t seem to find a set of rules that work. Below is pretty basic

server {

    # Uncomment The Following Line For Domain Mapping
    # listen 80 default_server;
    server_name **.do *.**.do;
    # Uncomment The Following Line For Domain Mapping
    #server_name_in_redirect off;
    access_log   /var/log/nginx/**.do.access.log rt_cache;
    error_log    /var/log/nginx/**.do.error.log;
    root /var/www/**.do/deployments/current/web;
    index index.php index.htm index.html;
    location / {
            try_files $uri $uri/ /index.php?$args;
    }
    location ~ \.php$ {
            try_files $uri =404;
            include fastcgi_params;
            fastcgi_pass php;
    }

}

1 Like
rewrite ^/(wp-.*.php)$ /wp/$1 last;
rewrite ^/(wp-(content|admin|includes).*) /wp/$1 last;

For those that reference this later - these seem to have worked for me. Note, you no longer have to set site_url to /wp/ when you use these rewrites.

6 Likes

It’s really helpful when people follow up with their own solution, so thanks for posting.

1 Like

Thanks for posting this. So just to rehash how to install Multisite with subdomains:

  1. Fresh Bedrock install
  2. Add define( 'WP_ALLOW_MULTISITE', true ); to config/application.php
  3. Go to WP Admin > Settings > Network Setup and go through the Multisite Install
  4. Add the required new define() codes to config/application.php, probably changing DOMAIN_CURRENT_SITE to getenv('WP_HOME')
  5. Remove /wp from WP_SITEURL in .env
  6. Add this to your nginx site config:
rewrite ^/(wp-.*.php)$ /wp/$1 last;
rewrite ^/(wp-(content|admin|includes).*) /wp/$1 last;

I was unable to log in after doing everything but removing /wp from WP_SITEURL, I was getting a “Cookies not set” error.

Seems like you’ll also want to go into your database and change in wp_options both siteurl and home to remove /wp from the URL. Even WP isn’t sure how to handle sub-directory installs…

Edit: debugging as I go as well, but /etc/hosts does not support wildcards, so you need to manually add any subdomain sites manually, or else I assume use some type of domain mapping plugin, or some type of dnsmasq

4 Likes

kalenjohnson, check out:
https://github.com/roots/bedrock-ansible/wiki/Multisite
Specifically @ Landrush Vagrant plugin

Hi there

We’re experiencing similar issues and have followed this thread to try and resolve but no success - we set everything up according to the wiki and have checked and rechecked everything including database after adding the additional site / subdomain. Everything seems to be set up correctly but still getting ERR_EMPTY_RESPONSE when trying to hit up the subdomain, in our case, mu.mu-test.blah - the root domain works great.

Any ideas :smiley: ?

What rewrites are you using?

I’ve got this working with the following (hat tip to another member here though don’t recall who pointed me in the right direction)

 # Rewrites for Bedrock Multi-Site Subdomain Setup
rewrite /wp-admin$ $scheme://$host$uri/ last;
rewrite ^/(wp-.*.php)$ /wp/$1 last;
rewrite ^/(wp-(content|admin|includes).*) /wp/$1 last;

Finally got this working. Thanks. My mistake was not doing:

service nginx reload

I had been restarting nginx but it wasn’t until I did reload that it picked up the new config. Schoolboy error.

Has anyone gotten this to work with Valet?