Wordpress Multi-Site Converting Apache to Nginx

nrahl nginx-forum at nginx.us
Tue Apr 29 20:27:36 UTC 2014

I'm trying to move a working Apache2 config to Nginx and failing miserably.
The site has a main application, a custom CMS and a wordpress multi-site
with a few blogs. The CMS rules are setup and working, but I can't get the
wordpress rules to 1. Work and 2. Get executed ebcause the CMS rules are
being too greedy.

The structure is:
/ - root is the CMS else, not wordpress
/*Anything*/ - Pass to CMS controller if no other rules or file matches
/*Anything*/*Something*/ - Pass to CMS controller if no other rules or file
/wordpress/ - the real folder with wordpress files, should *not* be accessed
/about/ - a fake (rewrite) directory for one of the multi-sites
/blog/ a fake (rewrite) directory for one of the multi-sites

This is the Apache2 rule set for WordPress that works:

       RewriteRule ^/(wp-(content|admin|includes).*) /wordpress/$1 [L]

        # MultiSites
        RewriteRule ^/about/(wp-(content|admin|includes).*) /wordpress/$1
        RewriteRule ^/about/(.*\.php)$ /wordpress/$1 [L]
        RewriteRule ^/about/(.*) /wordpress/index.php [L]

        RewriteRule ^/blog/(wp-(content|admin|includes).*) /wordpress/$1
        RewriteRule ^/blog/(.*\.php)$ /wordpress/$1 [L]
        RewriteRule ^/blog/(.*) /wordpress/index.php [L]
        RewriteRule ^/blog/tag/(.*)/ /wordpress/index.php [L]

We hard code the first slug of the URL to prevent the CMS's regex rules from
trying to grab it.

On the new server, with my new config, I can go to /wordpress/ and get the
master blog, and can login to the master blog.

Some of the links want to go to a URL like /wp-admin/network/ and it should
silently redirect to /wordpress/wp-admin/network/. That's what the first
apache rule does. I've tried to create an Nginx rule:

location ^~ ^/(wp-(content|admin|includes).*) {
   try_files /wordpress/$1 =404;

But I get a 404. So its matching the pattern but not loading the redirected

I am also having trouble getting the URL aliases to do anything. /blog/
should silently be /wordpress/blog/, so that Wordpress sees it as if it were
/wordpress/blog/. At present, I can't get to  /wordpress/blog/ directly or
through the alias /blog/.

I've been trying things like:

location ^~ ^/blog/([a-zA-Z0-9\-\_]+)/ {
     try_files /wordpress/$1.html /wordpress/$1/;

.... without any sucess.

I also have some pattern matching functions that insist on matching URLs
they aren't supposed to. The ^~ is supposed to make the rule a higher
priority, but it does not work.

I have rules such as:

location ~ ^/([a-zA-Z0-9\-\_]+)/$ {
   try_files /cache/$1.html $uri $uri/ /Director.php?rt=$1;

that should match any single slug, like /something/ and pass it to a
non-wordpress site UNLESS it is already in another rule, like /blog/. It
shouldn't match /blog/ because /blog/ has its own rule that should take
priority. Right now, that is not the case and the pattern location block is
being too greedy.

These are all the location blocks I have on the server:

   index Director.php index.php;

   location ^~ ^/(wp-(content|admin|includes).*) {
      try_files /wordpress/$1 =404;

   location ^~ ^/blog/([a-zA-Z0-9\-\_]+)/ {
      try_files /wordpress/$1.html /wordpress/$1/;

   # Attempt to match Slugs for Director
   location ~ ^/([a-zA-Z0-9\-\_]+)/$ {
     try_files /cache/$1.html $uri $uri/ /Director.php?rt=$1;

   location ~ ^/([a-zA-Z0-9\-\_]+)/([a-zA-Z0-9\-\_]+)/$ {
      try_files $uri $uri/ /Director.php?rt=$1&action=$2;

   location / {
      try_files $uri $uri/ /index.html;

   # pass the PHP scripts to FastCGI server listening on
   location ~ \.php$ {
      # Zero-day exploit defense.
      # http://forum.nginx.org/read.php?2,88845,page=3
      try_files $uri =404;
      fastcgi_split_path_info ^(.+\.php)(/.+)$;
      fastcgi_index index.php;
      include fastcgi_params;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      fastcgi_pass unix:/var/run/php5-fpm.sock;

   error_page 404 /404.html;

   # redirect server error pages to the static page /50x.html
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
           root /usr/share/nginx/html;

   # deny access to .htaccess files
   location ~ /\.ht {
      deny all;

What am I doing wrong?

Posted at Nginx Forum: http://forum.nginx.org/read.php?2,249743,249743#msg-249743

More information about the nginx mailing list