Changing URL of WordPress Multisite Blog using Nginx – proxy_pass? Location with regular expression error

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;
}

Answer

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

Attribution
Source : Link , Question Author : Tim , Answer Author : Community

Leave a Comment