1

I have a unique setup with a WordPress install in the main root and a second WordPress install (multisite) in a sub folder.

  • /sites/example.com/files/[WP Site example.com]
  • /sites/example.com/files/demo [WP Multisite example.com/demo]

The main site is working well but the multisite isn't.

When I request a multisite (example.com/demo/site1) it I get 404's in the console (example.com/demo/site1/style.css = 404)

Here is my conf.

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

map $blogname $blogid{
    default -999;
}

server {
    listen 443 ssl http2; 
    listen [::]:443 ssl http2; 

    server_name example.com; 

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

    root /sites/example.com/files/;

    index index.html index.php;

    access_log /sites/example.com/logs/access.log;
    error_log /sites/example.com/logs/error.log;

    # MIME sniffing prevention
    add_header X-Content-Type-Options "nosniff";

    # Enable cross-site scripting filter in supported browsers.
    add_header X-Xss-Protection "1; mode=block";
    
    # WP Multisite start
    location ~ ^(/[^/]+)?/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;
    }
    #avoid php readfile()
    location ^~ /blogs.dir {
        internal;
        alias /sites/example.com/files/demo/wp-content/blogs.dir ;
        access_log off;     log_not_found off; expires max;
    }

    if (!-e $request_filename) {
        rewrite /wp-admin$ $scheme://$host$uri/ permanent;
        rewrite ^(/[^/]+)?(/wp-.*) $2 last;
        rewrite ^(/[^/]+)?(/.*\.php) $2 last;
    }

    location /demo {
        #alias /sites/example.com/files/demo/;          
        try_files $uri $uri/ /demo/index.php?$args;
        }
    # WP Multisite end

    # Prevent access to hidden files
    location ~* /\.(?!well-known\/) {
        deny all;
    }

    # Prevent access to certain file extensions
    location ~\.(ini|log|conf|blade.php)$ {
        deny all;
    }
    location / {
        try_files $uri $uri/ /index.php?$args;
    }
    

    location ~ \.php$ {
        try_files $uri =404;
        include fastcgi.conf;
        fastcgi_pass unix:/run/php/php8.0-example.sock;
    }
}

Any advice would be greatly appreciated. I've been trying to solve this for about a week.

I've been using this as a guide: https://www.nginx.com/resources/wiki/start/topics/recipes/wordpress/#rewrite-rules-for-multisite

Thank you!

  • I found my answer here: https://wordpress.stackexchange.com/questions/186209/wordpress-multisite-with-nginx-subfolders-and-from-a-subfolder/406056#406056 – Ryan Labelle May 24 '22 at 19:14

1 Answers1

0

I commented above and left the link that was helpful in answering my question.

Here is my final nginx config that is working.

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

map $blogname $blogid{
    default -999;

    #Ref: http://wordpress.org/extend/plugins/nginx-helper/
    #include /var/www/wordpress/wp-content/plugins/nginx-helper/map.conf ;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    server_name example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

    root /sites/example.com/files/;

    index index.html index.php;

    access_log /sites/example.com/logs/access.log;
    error_log /sites/example.com/logs/error.log;

    # MIME sniffing prevention
    add_header X-Content-Type-Options "nosniff";

    # Enable cross-site scripting filter in supported browsers.
    add_header X-Xss-Protection "1; mode=block";

    # WP Multisite start
    location ~ ^(/[^/]+)?/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;
    }
    #avoid php readfile()
    location ^~ /blogs.dir {
        internal;
        alias /sites/example.com/files/demo/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 ^/demo(/[^/]+)?(/wp-.*) /demo$2 last;
        rewrite ^/demo(/[^/]+)?(/.*\.php)$ /demo$2 last;
    }

    location /demo {
        #alias /sites/example.com/files/demo/;
        try_files $uri $uri/ /demo/index.php?$args;
        }



    # WP Multisite end


    # Prevent access to hidden files
    location ~* /\.(?!well-known\/) {
        deny all;
    }

    # Prevent access to certain file extensions
    location ~\.(ini|log|conf|blade.php)$ {
        deny all;
    }
    location / {
        try_files $uri $uri/ /index.php?$args;
    }


    location ~ \.php$ {
        try_files $uri =404;
        include fastcgi.conf;
        fastcgi_pass unix:/run/php/php8.0-example.sock;
    }
}