How to Configure WordPress Multisite with Subdirectories on Nginx?

I have a WordPress site that I want to convert into a multisite network with subdirectories. I am using Nginx as my web server and have already enabled the multisite feature in WordPress. However, I am having trouble configuring Nginx to handle the subdirectory requests. For example, when I try to access mysite.com/subsite1, I get a 404 error. How can I fix this? What are the steps to configure Nginx for WordPress multisite with subdirectories? Any help would be appreciated. Thank you.

To configure WordPress multisite with subdirectories on Nginx, you need to follow these steps:

  1. Enable the multisite feature in WordPress by adding the following line to your wp-config.php file:
define('WP_ALLOW_MULTISITE', true);
  1. Go to Tools > Network Setup in your WordPress dashboard and choose the Subdirectory installation option.

  2. Follow the instructions and add a few new lines to your wp-config.php files, similar to the lines below.

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 );
  1. Edit your Nginx configuration file for your WordPress site and add the following rules inside the server block:
location = /favicon.ico {
    log_not_found off;
    access_log off;
}
location = /robots.txt {
    allow all;
    try_files $uri /index.php?$args;
}

location / {
    try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
    include fastcgi_params;
    fastcgi_intercept_errors on;
    #fastcgi_pass 127.0.0.1:9000;
    fastcgi_pass unix:/tmp/php-cgi.socket;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
    expires max;
    log_not_found off;
}
if (!-e $request_filename) {
  rewrite /wp-admin$ $scheme://$host$uri/ permanent;
  rewrite ^(?!^/my-db-admin)(/[^/]+)?(/wp-.*) $2 last;
  rewrite ^(?!^/my-db-admin)(/[^/]+)?(/.*.php) $2 last;
}

These rules will handle the subdirectory requests and rewrite them to the WordPress index.php file.

  1. Restart Nginx and create new sites from your WordPress network admin dashboard. You should be able to access them using the subdirectory format, such as mysite.com/subsite1.

You can check the link below!