Nginx + WordPress Multisite 3.4.2 + subdirectories + static pages and permalinks

UrkoM asked:

I am trying to setup WordPress Multisite, using subdirectories, with Nginx, php5-fpm, APC, and Batcache.
As many other people, I am getting stuck in the rewrite rules for permalinks.

I have followed these two guides, which seem to be as official as you can get:

It is partially working:

But other permalinks, like these two to a post or to a static page, don’t work:

They either take you to a 404 error, or to some other blog!

Here is my configuration:

server {
    listen       80 default_server;
    root         /var/www;

    access_log   /var/log/nginx/blog-access.log;
    error_log    /var/log/nginx/blog-error.log;

    location / {
        index index.php;
        try_files $uri $uri/ /index.php?$args;


    # Add trailing slash to */wp-admin requests.
    rewrite /wp-admin$ $scheme://$host$uri/ permanent;

    # Add trailing slash to */username requests
    rewrite ^/[_0-9a-zA-Z-]+$ $scheme://$host$uri/ permanent;

    # Directives to send expires headers and turn off 404 error logging.
    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires 24h;
        log_not_found off;

    # this prevents hidden files (beginning with a period) from being served
      location ~ /\.          { access_log off; log_not_found off; deny all; }

    # Pass uploaded files to wp-includes/ms-files.php.
    rewrite /files/$ /index.php last;

    if ($uri !~ wp-content/plugins) {
        rewrite /files/(.+)$ /wp-includes/ms-files.php?file=$1 last;

    # Rewrite multisite '.../wp-.*' and '.../*.php'.
    if (!-e $request_filename) {

        rewrite ^/[_0-9a-zA-Z-]+(/wp-.*) $1 last;
        rewrite ^/[_0-9a-zA-Z-]+.*(/wp-admin/.*\.php)$ $1 last;
        rewrite ^/[_0-9a-zA-Z-]+(/.*\.php)$ $1 last;


    location ~ \.php$ {
    # Forbid PHP on upload dirs
        if ($uri ~ "uploads") {
            return 403;

        client_max_body_size 25M;
        try_files      $uri =404;
        fastcgi_pass   unix:/var/run/php5-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        /etc/nginx/fastcgi_params;

Any ideas are welcome! Have I done something wrong?
I have disabled Batcache to see if it makes any difference, but still no go.

My answer:

Wow, you’ve got a real mess on your hands.

Overall the nginx configuration looks good. The only thing that struck me on first reading as being possibly “wrong” was the try_files statement. In my production WordPress multisite, I have:

try_files $uri $uri/ index.php;

It’s not necessary to append the args, as WordPress picks them up from REQUEST_URI when it is available, (almost always) and PATH_INFO when they aren’t present in the query string.

Yes, I’m aware that the statement you are using is what’s “recommended” on the WordPress site. Of course it’s also a wiki that anybody can edit, so it has to be taken with a grain of salt, just like Wikipedia.

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.