Nginx location rule for Wordpress Multisite in subdirectories

Francis Daly francis at
Mon Aug 27 20:47:38 UTC 2012

On Sun, Aug 26, 2012 at 02:40:40PM -0400, Ian M. Evans wrote:

Hi there,

> I need to be able to catch the above in either /blog or its suddirs, since
> WP installs a main blog in the /blogs dir i.e.:
> location ^~ /blogs {
>   try_files $uri /blogs/index.php?q=$uri;
> and
> location ^~ /blogs/blog1 {
>   try_files $uri /blogs/blog1/index.php?q=$uri;
> I realize I could create locations specific to each dir, but is there a
> wildcard way to do this?

nginx locations can be "regex" or "prefix". If you have a url prefix which
matches all of the subdir blogs, and none of the non-subdir blogs, then
something like the following might work (I assume that all subdir blogs
start with /blogs/blog; and therefore that everything that does not start
with that pattern belongs to the /blogs location, which remains as-is.):

  if ($uri ~ (/blogs/[^/]*)) {
   set $blogname $1;

  include fastcgi.conf;

  location ^~ /blogs/blog {
    try_files $uri $blogname/index.php?q=$uri;
    location ~ php {

If you haven't got a clear prefix separation for the blogs, then you
might want to try a regex location; or perhaps something involving "map"
which will (probably) require enumeration all of the blogs.

Francis Daly        francis at

More information about the nginx mailing list