15 Sep

How to setup Nginx for wordpress multisite with subdirectories..

Running WordPress on Nginx is always a good choice, but also have some complications in configurations. Here is the clear and precise way of how to setup Nginx for wordpress multisite with subdirectories.You can also configure Nginx with subdomain multisite and it all depends on your requirement .

You can setup wordpress multisite on Nginx like:

  1. site1.example.com, site2.example.com
  2. www.example.com/sub1, www.example.com/sub2

Here is the steps to setup  Nginx for wordpress multisite with subdirectories:


Install wordpress fresh on your system, and enable multisite option by adding a line to your wp-config.php file. Add this line just above where it says “/* That’s all, stop editing! Happy blogging. */”:
Add this line :
/* Multisite */
define( ‘WP_ALLOW_MULTISITE’, true );

Now you can see a new option will appear in the Admin panel of WordPress under Tools -> Network Setup


Now you need to choose the subdirectory install option and click on Install. Once it is done, now you’ll have to add some more lines in your wp-config.php file. These lines may varies based on your installation and you can get these lines after the install has been completed.

Now copy these new lines in wp-config.php .Now you will see a separate “Site” called Network Admin in the upper left of your Admin section.


Now you can create a new site that you want to install and also provide subdirectory installation path for the new website.Now we have done setting up new website in subdirectory and we still need to link it through URl.
Now you have to go Under the Network Admin section, install a new Plugin called Nginx Helper. Once it get installed and activated, visit the plugin settings page and check the box next to “Enable Nginx Map.” This will display a new section showing a file system path to your map file and the actual site mappings that are being generated.

Each new site in your network will have both a directory path as well as a blog id. The mapping links the subdirectory in the URL with the proper site in the database.


Now we are almost there in setting up are multisite on Nginx.Now you need to access terminal access to the Nginx vhost file through SSH.
SSH to the Nginx site configuration file, on Debian/Ubuntu it’s at /etc/nginx/sites-available.
Now you need to modify the .vhost file

At the start of the file, outside the server{} block add this :

map $http_host $blogid {
default 0;
include /var/www/pathtoyoursite/wp-content/uploads/nginx-helper/map.conf;

By this way it will let the plugin update the mappings file automatically as you add new sites and you won’t have to change your Nginx config again.

Now at atlast in your .vhost file add the following Nginx directives at the bottom of the server{} block, replacing any existing WordPress directives you’ve already defined, usually following the location @php{} directive:

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

location ~ ^/files/(.*)$ {
try_files /wp-content/blogs.dir/$blogid/$uri /wp-includes/ms-files.php?file=$1 ;
access_log off; log_not_found off; expires max;

location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires 24h;
log_not_found off;

location ^~ /blogs.dir {
alias /var/www/pathtoyoursite/web/wp-content/blogs.dir ;
access_log off; log_not_found off; expires max;

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;

Now the alias line in the second to last block, you’ll need to update that with the path to your wordpress install.


Restart Nginx and enjoy your multisite.

Share this

Comments (58)

Jayesh Kukreja
September 4, 2020 Reply

Nice post brother, I have been surfing online more than 3 hours today, yet I never found
any interesting article like yours. It is pretty worth
enough for me. In my view, if all web owners and bloggers made good content
as you did, the internet will be much more useful than ever before.
There is certainly a lot to know about this issue.

I love all of the points you’ve made. I am sure this post
has touched all the internet viewers, its really really good post on building up new weblog.
Gyan Hi Gyann

Leave a reply