How to configure WordPress multisite with NGINX

WordPress multisites (also referred to as network sites) allow you to run multiple websites from the one codebase. This can reduce the management burden of having separate WordPress installations when you have similar sites.

To convert a WordPress site into a multisite, you need to add the configuration variable into your config file wp-config.php.

/** Multisite settings */
define( 'WP_ALLOW_MULTISITE', true );

Under the Tools menu, you’ll now see an extra menu called Network Setup.

This will present you with two main options, Sub-domains and Sub-directories. These are the two different ways the multisite installation will work.

  • The Sub-domains option has the sites separated by domain names.
  • The Sub-directories option means that the sites are separated by directories.

There’s no functional difference between the two, it’s simply an aesthetic choice. However, once you’ve made your choice, you cannot return to the previous state.

Once you’ve made the choice, it will then provide the additional code to add to your wp-config.php file.

Here’s the code for my example, which is subdirectory based.

define('MULTISITE', true);
define('SUBDOMAIN_INSTALL', true);
define('DOMAIN_CURRENT_SITE', 'site.local');
define('PATH_CURRENT_SITE', '/');
define('SITE_ID_CURRENT_SITE', 1);
define('BLOG_ID_CURRENT_SITE', 1);

Because NGINX doesn’t support .htaccess files, the second part of the WordPress instructions will not work. Instead, we need to modify the NGINX configuration to provide the rewrite rules ourselves.

In the existing /etc/nginx/conf.d/wordpress.conf file, you’ll need to add the following just after the location / directive.

if (!-e $request_filename) {
    rewrite /wp-admin$ $scheme://$host$uri/ permanent;  
    rewrite ^(/[^/]+)?(/wp-.*) $2 last;                     
    rewrite ^(/[^/]+)?(/.*.php) $2 last;                   
}

You’ll also need to modify the NGINX config as well to add in the wildcard for the server name.

server_name  *.site.local site.local

Reload NGINX to read the new configuration file.

sudo service nginx reload

You can now add in the additional sites, such as s1.site.local, and there won’t be any changes required for NGINX.

Add Comment