WordPress multisites subdirectories on nginx and not root folder not working properly

June 8, 2015 3.8k views
Nginx LEMP WordPress

Hi there,

First of all, I'm a new digitalocean customer, and fully happy by what you guys are offering us.

I have a WordPress multisites with nginx installed like this:

http://demo.example.com/wordpress/
http://demo.example.com/wordpress/wp-admin/
http://demo.example.com/wordpress/subsite1/
http://demo.example.com/wordpress/subsite2/

Accessing my main site @ http://demo.example.com/wordpress/ is working, and the wp-admin is working too.

When accessing a sub-site @ http://demo.example.com/wordpress/subsite1/ for example, the site is loading but every assets is giving a 404 errors due to a bad URL : http://demo.example.com/wordpress/subsite1/wp-content/themes/impala/js/bootstrap.min.js?ver=3.0.2
** instead of** http://demo.example.com/wordpress/wp-content/themes/impala/js/bootstrap.min.js?ver=3.0.2

It's always putting the name of the subsite when trying to load assets in a "subsite" site.

Here's my config, I'v tried tons of config that I found around but this is the closest thing that's almost working for me, I don't know why :(

map $uri $blogname{
        ~^(?<blogpath>/[^/]+/)files/(.*)        $blogpath ;
}

map $http_host $blogid {
        default -999;
        include /var/www/demo.example.com/htdocs/wordpress/wp-content/uploads/nginx-helper/map.conf;
}

server {
        listen 80;
        listen [::]:80;
        server_name demo.example.com;
        root /var/www/demo.example.com/htdocs/wordpress;
        index index.php index.html index.htm;

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

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

        # Directives to send expires headers and turn off 404 error logging.
        location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|$
                access_log off; log_not_found off; expires max;
        }

        #location ~ ^/[_0-9a-zA-Z-]+/files/(.*)$ {
        #       try_files /wp-content/blogs.dir/$blogid/files/$2 /wp-includes/ms-files.php?file=$2 ;
        #       access_log off; log_not_found off; expires max;
        #}

        location ~ ^(/[^/]+/)?files/(?<rt_file>.+) {
                try_files /wp-content/blogs.dir/$blogid/files/$rt_file /wp-includes/ms-files.php?file=$rt_file ;
                access_log off; log_not_found off; expires max;
        }

        #avoid php readfile()
        location ^~ /blogs.dir {
                internal;
                alias /var/www/demo.example.com/htdocs/wordpress/wp-content/blogs.dir ;
                access_log off; log_not_found off;  expires max;
        }


        # Rewrite multisite '.../wp-.*' and '.../*.php'.
        if (!-e $request_filename) {
                rewrite /wp-admin$ $scheme://$host$uri/ permanent;
                rewrite ^/[_0-9a-zA-Z-]+(/wp-.*) $1 last;
                rewrite ^/[_0-9a-zA-Z-]+(/.*\.php)$ $1 last;
        }


}

Am I missing something? I've read that MU in subdirectories can be a pain, but I don't know what to do right now :/

Thanks in advance, and sorry for making another thread about that topic!

3 Answers

I'm having the same problem, did you find a solution?
Thanks!

The last part to your sever block if (!-e $request_filename) { ... should do the trick. I was having the exact same problems, added that and everything started working, even the infinite redirect loop when accessing a subdirectory wp-admin.

This may seem super basic, but did you make sure you restarted Nginx after you updated your server block?

Have another answer? Share your knowledge.