A Comprehensive WordPress Multisite Setup Tutorial for Nginx

by

Hey there! Some links on this page may be affiliate links which means that, if you choose to make a purchase, I may earn a small commission at no extra cost to you. I greatly appreciate your support!

In this tutorial, you will learn how to setup WordPress multisite on an Nginx web server. This tutorial is intended for new WordPress installs; however, the same concepts can also be used for existing websites.

While the official WordPress documentation and installation process for multisite is tailored to Apache, the Nginx documentation that I could find was quite outdated (i.e. blogs.dir was removed from WordPress back in version 3), so that’s why I decided to make this tutorial.

How to Enable WordPress Multisite

Wether you choose to setup your multisite installation with subdomains or subdirectories, the first thing you need to do is enable WordPress multisite. To do this, edit your wp-config.php file in the root of your web directory and add the following to allow multisite.

define( 'WP_ALLOW_MULTISITE', true );

/* That's all, stop editing! Happy publishing. */

This enables the Network Setup option under the Tools menu in the WordPress dashboard.

WordPress network setup

Next, depending on which option you want, you can jump to either subdomain or subdirectory below.

Nginx Subdomain Configuration for WordPress Multisite

You can configure WordPress multisite to create new sites at subdomains. Examples of WordPress multisite subdomain sites are:

  • like.example.com
  • comment.example.com
  • subscribe.example.com

1. Add a Wildcard DNS Record

Wherever you registered your domain name or wherever you’re DNS record settings exist, you will need to add a wildcard DNS A record. This simply is an A record with an asterisk.

Wildcard DNS A record

2. Enable Wildcard Subdomain in Nginx Conf

Next, you must enable the wildcard subdomain in your Nginx configuration file. The important change is highlighted below.

upstream php-handler {
        server unix:/var/run/php/php7.4-fpm.sock;
}

server {
        listen 80;
        server_name example.com *.example.com;
        root /var/www/wordpress;
        index index.php;

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

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass php-handler;
        }
}

3. Access the Network Setup Wizard

Because you enabled multisite in the previous step, you can now navigate to Tools > Network Setup to kick off the installation.

4. Choose the Subdomain Option

If not already selected, choose the Sub-domains option to install WordPress multisite in that way.

WordPress multisite subdomains

5. Configure Subdomain Install

As instructed by the setup wizard, the next step is to add additional configuration in your wp-config.php file.

define( 'WP_ALLOW_MULTISITE', true );
define('MULTISITE', true);
define('SUBDOMAIN_INSTALL', true);
define('DOMAIN_CURRENT_SITE', 'example.com');
define('PATH_CURRENT_SITE', '/');
define('SITE_ID_CURRENT_SITE', 1);
define('BLOG_ID_CURRENT_SITE', 1);

/* That's all, stop editing! Happy publishing. */

That’s all! After logging back into the WordPress dashboard, you now have a working network of sites that can be created an accessed by subdomains.

Nginx Subdirectory Configuration for WordPress Multisite

You can configure WordPress multisite to create new sites at subdirectories. Examples of WordPress multisite subdomain sites are:

  • example.com/like
  • example.com/comment
  • .example.com/subscribe

1. Access the Network Setup Wizard

Because you enabled WordPress multisite above, you can now navigate to Tools > Network Setup to kick off the WordPress multisite installation.

2. Choose the Subdirectory Option

If not already selected, choose the Sub-directory option to install WordPress multisite in that way.

WordPress multisite subdirectories

3. Configure Subdirectory Install

As instructed by the setup wizard, the next step is to add additional configuration in your wp-config.php file.

define( 'WP_ALLOW_MULTISITE', true );
define('MULTISITE', true);
define('SUBDOMAIN_INSTALL', false);
define('DOMAIN_CURRENT_SITE', 'example.com');
define('PATH_CURRENT_SITE', '/');
define('SITE_ID_CURRENT_SITE', 1);
define('BLOG_ID_CURRENT_SITE', 1);

/* That's all, stop editing! Happy publishing. */

4. Enable Wildcard Subdomain in Nginx Conf

Next, you must add rewrite rules to support WordPress multisite in your Nginx configuration file. The important change is highlighted below.

upstream php-handler {
        server unix:/var/run/php/php7.4-fpm.sock;
}
server {
        listen 80;
        server_name example.com www.example.com;
        root /var/www/wordpress;
        index index.php;

        # Rewrite requests to /wp-.* on subdirectory installs.
        if (!-e $request_filename) {
                rewrite /wp-admin$ $scheme://$host$uri/ permanent;
                rewrite ^/[_0-9a-zA-Z-]+(/wp-.*) $1 last;
                rewrite ^/[_0-9a-zA-Z-]+(/.*\.php)$ $1 last;
        }

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

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass php-handler;
        }
}

That’s all! After logging back into the WordPress dashboard, you now have a working network of sites that can be created an accessed by subdirectories.


Meet Tony

With a strong software engineering background, Tony is determined to help as many people as possible start their online busines. Discover why Tony quit his hedge fund job to pursue this mission. You can send Tony a message here.

Leave a Comment