0

I've set up a Wordpress Multisite (aka WPMU) instance on AWS to serve my four blogs, which are all currently single site installs. Three of the blogs run on the domain root, but one needs to be served from /blog/ as I have a custom written website on the root. I can't make WPMU serve the blog from a subdirectory due to limitations in the software, even though single site Wordpress can do it, so I'd like to see if I can do it using Nginx.

My fallback is a single site Wordpress install, which I'd prefer to avoid as it adds maintenance and backup overhead. Another fallback is putting the blog on a subdomain, which isn't as good for SEO, so I'd really prefer to avoid that as well.

I've very open to alternative solutions that get the blog into a subdirectory, running WPMU

It should look like this to the viewer

example.com  <- Viewer sees custom PHP application
example.com/blog/  <- Viewer sees WPMU blog
blog.example.com   <- hidden from viewers

This is how I think it could be set up

example.com <- Runs custom PHP written application
blog.example.com   <- WPMU runs on a subdomain, mapped to a subfolder of the main domain by Nginx

I think it should be possible to rewrite things to work with Wordpress Multisite using Nginx, but I can't quite work it out. proxy_pass worked just fine for a static site (can't post the config, my reputation is insufficient to post that many links), but I'm running into the error

"proxy_pass" cannot have URI part in location given by regular expression" (etc)

I've seen people do tricky things with rewrite in nginx that convinced it to work, but I can't get it right. Can anyone help with that?

I've posted some relevant location blocks below that work in my current config below - of course all static resources js/css/jpeg/etc need to be redirected, as WPMU will still generate html that refers to the subdomain

# We want all resources in the wp-content/uploads/20x and uploads/galleries   
# folders to redirect to wp-content/uploads/sites/$blogid/whatever
location ~* "wp-content\/uploads\/((\d{4,}\/\d{2,}|galleries).*)" {
  alias    /var/www/wordpress/wp-content/uploads/sites/$blogid/$1;
  return 301 $scheme://$host/wp-content/uploads/sites/$blogid/$1;
}

# Set caching headers for images
location ~*  \.(jpg|jpeg|png|gif|css|js)$ {
  log_not_found off; access_log off;
  add_header Cache-Control "public";
  expires 4h;
}

# Send HipHop and PHP requests to HHVM
location ~ \.(hh|php)$ {
  fastcgi_keep_conn on;
  fastcgi_intercept_errors on;
  fastcgi_pass   php;
  include        fastcgi_params;
  fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

If this was a simple block with no regular expression, as in my static test, something like this would probably do the job

# Send HipHop and PHP requests to HHVM
location ~ \.(hh|php)$ {
  proxy_pass http://example.com
  fastcgi_keep_conn on;
  fastcgi_intercept_errors on;
  fastcgi_pass   php;
  include        fastcgi_params;
  fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
0

Here's a step-by-step guide on how to do this:

https://yuji.wordpress.com/2010/03/08/nginx-wordpress-proxy-subdirectory-to-wordpress-subdomain/

In the end, the author advises against it. Where I would go differently to perhaps avoid some of his bad experiences, is the URL rewriting. It can be done with the nginx string sub module instead of in Wordpress:

http://nginx.org/en/docs/http/ngx_http_sub_module.html

This links has some further explanation on proxying vs rewriting:

https://stackoverflow.com/questions/9233368/nginx-server-configuration-subdomain-to-folder

And don't forget the trailing slash:

Reverse Proxy - Remove Subdirectory

  • Interesting information, thank you JMT. It may be that I have to give up on WPMU and use a single Wordpress install, which is trivial. They did a fairly simple implementation of this idea, with none of the complex rewrite rules I have, which do things like caching and changing headers. Rewriting all the URLs must have a fairly significant overhead, especially on a large site, that would have to be cached. I was really hoping for help with making proxy_pass work inside the regular expression location block. – Tim Jan 6 '16 at 19:12
  • You can make that part work, but the WP URL definitions are the problem as per that yuji article. I'd then attempt to make it work through Nginx rather than in PHP. Single installs though are always easier, even if it adds maintenance overhead. Though that can be mitigated with CLI scripts. – JayMcTee Jan 7 '16 at 11:38
  • I think I might just do a standalone install. The overhead isn't that much and this looks like a real headache. Thanks for the thoughts, if no-one comes up with a better idea in the next couple of days I'll accept your answer. – Tim Jan 7 '16 at 21:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.