Nginx location rule for Wordpress Multisite in subdirectories


#1

Hi everyone!

I currently have a wordpress blog located at blogname and, as per
threads
here, I had the following on my nginx.conf:

location ^~ /blogname {
try_files $uri /blogname/index.php?q=$uri;
location ~ .php$ {
fastcgi_pass 127.0.0.1:10004;

}
}

I’m actually adding a few more blogs and will be moving to the multisite
options in wordpress and running the various blogs in subdirectories
under
/blogs, e.g.:

/blogs/blog1
/blogs/blog2
etc.

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?

Thanks.

p.s. as I’ve said before, my nickname should be “Sucks at regex”


#2

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

Hi there,

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 {
fastcgi_pass 127.0.0.1:10004;
}
}

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.

f

Francis D. removed_email_address@domain.invalid


#3

On 27/08/2012 4:47 PM, Francis D. wrote:

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.

Thanks so much for the example, I’ll give it a try.

Is there a specific order the locations have to go in? /blogs before
/blogs/blog or vice-versa?


#4

On Tue, Aug 28, 2012 at 04:12:52PM -0400, Ian E. wrote:

On 27/08/2012 4:47 PM, Francis D. wrote:

Hi there,

nginx locations can be “regex” or “prefix”.

Is there a specific order the locations have to go in? /blogs before
/blogs/blog or vice-versa?

No.

“prefix” locations (location, location ^~, and location =; although
the last isn’t really a prefix) have the longest matching one chosen,
independent of the order in the config file.

That’s one of the reasons why it is good to avoid “regex” locations
if possible.

f

Francis D. removed_email_address@domain.invalid


#5

On Thu, August 30, 2012 5:27 pm, Francis D. wrote:

“prefix” locations (location, location ^~, and location =; although
the last isn’t really a prefix) have the longest matching one chosen,
independent of the order in the config file.

That’s one of the reasons why it is good to avoid “regex” locations
if possible.

Thanks for the info.