• Resolved nchambers

    (@nchambers)


    Hello! I’ve installed WordPress 5.1.1 locally onto my macbook, and it is being served by Nginx 1.15.8 out of the /wordpress/ subfolder. Initially, everything was working fine, until I tried to setup a subfolder multisite. The main sites still work. For example, the following links still load as expected:

    https://localhost/wordpress/ (main site)
    https://localhost/wordpress/blog/1/hello-world/ (blog post on the main site)
    https://localhost/wordpress/wp-admin/ (admin for main site) https://localhost/wordpress/wp-admin/network/ (multisite network admin)

    However my new multisite (named multisite) doesn’t load properly.
    https://localhost/wordpress/multisite/ loads but all of the resources 404, such as https://localhost/wordpress/multisite/wp-includes/css/dist/block-library/theme.min.css. https://localhost/wordpress/multisite/wp-admin/ gets caught in a redirect loop. I am sure it is a problem with my Nginx configuration:

    http {
      include mime.types;
      default_type application/octet-stream;
      sendfile on;
      keepalive_timeout 65;
      gzip on;
    
      server {
        listen 80;
        server_name localhost;
        return 301 https://$host$request_uri;
      }
    
      server {
        listen 443 ssl http2;
        server_name localhost;
    
        # ssl directives removed for brevity
    
        root /Users/nicholas.chambers;
    
        location ~ \.(js|css|png|jpg|jpeg|gif|ico|ttf|woff) {
          try_files $uri =404;
        }
    
        location ~ /wordpress/(.*)(\.php)?$ {
          index index.php index.html;
          try_files $uri $uri.php /wordpress/$1/index.php /wordpress/index.php =404;
          fastcgi_pass 127.0.0.1:9000;
          include fastcgi_params;
          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
          fastcgi_param SCRIPT_NAME $fastcgi_script_name;
        }
      }
    }

    I am just not sure what I need to do to fix it. I know pages like https://www.nginx.com/resources/wiki/start/topics/recipes/wordpress/ and https://codex.www.ads-software.com/Nginx exist, but these assume a blogs.dir folder, which my multisite doesn’t appear to have:

    
    nicholas-chambers:wordpress nicholas.chambers$ find . -name blogs.dir -type d -print
    nicholas-chambers:wordpress nicholas.chambers$
    

    If anyone could point me in the right direction, that would be great.

    • This topic was modified 5 years, 7 months ago by Jan Dembowski. Reason: Fixed formatting
Viewing 10 replies - 1 through 10 (of 10 total)
  • Moderator Jan Dembowski

    (@jdembowski)

    Forum Moderator and Brute Squad

    Try changing the try_files part here to something simpler. And get ready to change it back if it doesn’t work. ??

        location ~ /wordpress/(.*)(\.php)?$ {
          index index.php index.html;
          # try_files $uri $uri.php /wordpress/$1/index.php /wordpress/index.php =404;
          try_files $uri $uri/ /index.php?$args;
          fastcgi_pass 127.0.0.1:9000;
          include fastcgi_params;
          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
          fastcgi_param SCRIPT_NAME $fastcgi_script_name;
        }
    Thread Starter nchambers

    (@nchambers)

    The good news is it didn’t break anything (the same links I mentioned above still work), but https://localhost/wordpress/multisite/ and https://localhost/wordpress/multisite/wp-admin/ still show the same behavior :/. Thanks for simplying the try_files though! I was wondering how that could be more managable.

    That’s a bit complicated setup (for me). A multisite network on a sub-directory. There are multiple issues, as you mentioned. To fix 404 on sub-site and to fix redirect loop (that I never experienced), please try the following…

    
    # Rules for Multisite Sub-directory install
    
    # For sub-directory redirects
    # Only one would apply
    # If two needed to be applied, it'd throw a 404
    if (!-e $request_filename) {
        rewrite  ^/wordpress/[_0-9a-zA-Z-]+(/wp-(content|admin|includes).*) /wordpress$1 break;
        rewrite  ^/wordpress/[_0-9a-zA-Z-]+(/.*\.php)$ /wordpress$1 break;
    }
    

    Modified from my working config for a multisite network on root of the domain such as at example.com… https://github.com/pothi/wordpress-nginx/blob/master/globals/mu-dir.conf

    Also, I’d recommend to change the line that read…

    try_files $uri $uri.php /wordpress/$1/index.php /wordpress/index.php =404;

    to

    try_files $uri $uri/ /wordpress/index.php$is_args$args;

    I hope that helps to reduce the number of errors, especially the 404 errors for static resources.

    • This reply was modified 5 years, 7 months ago by Pothi Kalimuthu. Reason: formatting fix

    Oh, btw, blogs.dir has been retired now. Ref: https://codex.www.ads-software.com/Multisite_Network_Administration (please search for the term “blogs.dir” to quickly reach the relevant text).

    Thread Starter nchambers

    (@nchambers)

    Thanks Pothi! That is all great information. I didn’t realize it is indeed a complicated setup. I studied your repository (which is great by the way, thank you for linking that), and believe I have extracted the necessary parts from it for my setup, but I’m still seeing the same behavior as before. Would you mind taking a look at my updated config?

    
    http {
      include mime.types;
      default_type application/octet-stream;
      sendfile on;
      keepalive_timeout 65;
      gzip on;
    
      log_format compression '$time_local - "$uri" $status "$http_referer" $request_filename';
      access_log /Users/nicholas.chambers/log/nginx/access.log compression;
      error_log /Users/nicholas.chambers/log/nginx/error.log;
    
      upstream fpm {
        server 127.0.0.1:9000;
      }
    
      map $http_accept $webp_suffix {
        default "";
        "~*webp" ".webp";
      }
    
      server {
        listen 80;
        server_name localhost;
        return 301 https://$host$request_uri;
      }
    
      server {
        listen 443 ssl http2;
        server_name localhost;
     
        # ssl directives redacted
    
        root /Users/nicholas.chambers;
    
        location /dl {
          autoindex on;
          add_header X-location "static dl folder";
    
          types {
            text/plain sh;
            application/pdf pdf;
          }
        }
    
        location /wordpress {
          index index.php;
          rewrite_log on;
          add_header X-location "wordpress blogs";
    
          if ($uri ~ "files") {
            rewrite ^/wordpress/(?:.*/)?files/(.+) /wordpress/wp-includes/ms-files.php?file=$1;
          }
    
          if ( -e $request_filename ) {
            add_header X-debug "did rewrite";
            rewrite ^/wordpress/[_0-9a-zA-Z-]+_(/wp-(content|admin|includes).*) /wordpress$1 break;
            rewrite ^/wordpress/[_0-9a-zA-Z-]+(/.*\.php)$ /wordpress$1 break;
          }
    
          location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            include "fastcgi.conf";
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_index index.php;
            fastcgi_pass fpm;
          }
    
          location /wordpress {
            try_files $uri $uri/ /wordpress/index.php$is_args$args;
          }
    
          location ~ \.(?:css|js)$ {
            add_header X-thebug "got a resource";
            expires max;
          }
    
          location ~ \.(?:ttf|ttc|eot|woff|woff2|otf|svg)$ {
            expires max;
          }
    
          location ~ \.(?:gif|ico|webp)$ {
            valid_referers none blocked localhost;
    
            if ($invalid_referer) {
              return 403;
            }
    
            expires max;
          }
    
          location ~* ^.+\.(png|jpe?g)$ {
            valid_referers none blocked localhost;
    
            if ($invalid_referer) {
              return 403;
            }
    
            try_files $uri$webp_suffix $uri =404;
            expires max;
          }
    
          location ~ \.(?:rss|atom)$ {
            expires 600s;
          }
        }
    
        location ~ /w/(.*)(\.php)?$ {
          index index.php index.html;
          try_files $uri $uri.php /w/index.php =404;
          fastcgi_pass 127.0.0.1:9000;
          include fastcgi_params;
          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
          fastcgi_param SCRIPT_NAME $fastcgi_script_name;
        }
      }
    }
    

    The /w block is for a mediawiki install I am messing around with, and the /dl is for a directory index of a different folder. I don’t believe either are relevant, but included them just in case. The above configuration works fine with nginx:

    
    nicholas-chambers:~ nicholas.chambers$ nginx -t
    nginx: the configuration file /Users/nicholas.chambers/nginx/conf/nginx.conf syntax is ok
    nginx: configuration file /Users/nicholas.chambers/nginx/conf/nginx.conf test is successful
    nicholas-chambers:~ nicholas.chambers$ nginx -s reload
    nicholas-chambers:~ nicholas.chambers$
    

    However, whenever I try to curl one of the resources, I get an error:

    
    nicholas-chambers:~ nicholas.chambers$ curl -sSI https://localhost/wordpress/multisite/wp-content/plugins/ghostkit/assets/vendor/font-awesome/all.min.js
    HTTP/2 404
    server: nginx/1.15.8
    date: Tue, 16 Apr 2019 20:17:43 GMT
    content-type: text/html
    content-length: 153
    

    With this entry in my error.log

    
    2019/04/16 15:25:30 [error] 32009#0: *2988 open() "/Users/nicholas.chambers/wordpress/multisite/wp-content/plugins/ghostkit/assets/vendor/font-awesome/all.min.js" failed (2: No such file or directory), client: 127.0.0.1, server: localhost, request: "HEAD /wordpress/multisite/wp-content/plugins/ghostkit/assets/vendor/font-awesome/all.min.js HTTP/2.0", host: "localhost"
    

    I also don’t see the debug headers I added for the rewrite.

    At line…

    rewrite ^/wordpress/[_0-9a-zA-Z-]+_(/wp-(content|admin|includes).*) /wordpress$1 break;

    … there is an extra underscore. The corrected line is…

    rewrite ^/wordpress/[_0-9a-zA-Z-]+(/wp-(content|admin|includes).*) /wordpress$1 break;

    I also don’t see the debug headers I added for the rewrite.

    From what I know, there are two issue with add_header X-debug "did rewrite";

    1. Inside an “if” statement, 100% safe things are “return” and “rewrite” statements. Ref: https://www.nginx.com/resources/wiki/start/topics/depth/ifisevil/

    2. An “add_header” statement can only be valid for a particular request only if the requests ends in a “location” block that contains “add_header”. Since, “rewrite” statements that follow “add_header” transfers the execution to another “location” block, the request doesn’t pick-up the “add_header”. Here’s a similar discussion where “expires” header wasn’t set as expected… https://www.ads-software.com/support/topic/expires-headers-not-showing/ .

    I hope that helps.

    Thread Starter nchambers

    (@nchambers)

    Thanks for the information about the headers. I’ll fix that and check out the if article. I did indeed accidentally typo the _ in my config, but the issue is still occurring without it. New if block:

    
    if ( -e $request_filename ) {
      rewrite ^/wordpress/[_0-9a-zA-Z-]+(/wp-(content|admin|includes).*) /wordpress$1 break;
      rewrite ^/wordpress/[_0-9a-zA-Z-]+(/.*\.php)$ /wordpress$1 break;
    }
    

    I enabled the rewrite log, but don’t see any of the rewrites happening in my error log (which is why I attempted the add_header route actually). Thanks for the help, I’m really at a loss here as to what is going on.

    • This reply was modified 5 years, 7 months ago by nchambers.
    Thread Starter nchambers

    (@nchambers)

    Sorry, my current if block is actually:

    
    if ( !-e $request_filename ) {
      rewrite ^/wordpress/[_0-9a-zA-Z-]+(/wp-(content|admin|includes).*) /wordpress$1 break;
      rewrite ^/wordpress/[_0-9a-zA-Z-]+(/.*\.php)$ /wordpress$1 break;
    }
    

    I had changed it from !-e -> -e, just to confirm that wasn’t the problem, since I had exhausted everything else I could think of

    Please check if you have debug enabled in Nginx using the following command…

    nginx -V 2>&1 | sed 's/--/\'$'\n &/g'

    Ref: https://coderwall.com/p/gtgxww/intuitive-flags-information-of-nginx

    If debug is indeed enabled, then please activate debug log with the guidelines from https://nginx.org/en/docs/debugging_log.html .

    • This reply was modified 5 years, 7 months ago by Pothi Kalimuthu. Reason: Added link that explains the code
    Thread Starter nchambers

    (@nchambers)

    Hi Pothi, apologies for the late reply. It doesn’t look like I do have debug enabled, so I’ll get that done at some point (although it will have to be later now). Thanks for your help, I learned a lot even though I couldn’t get this resolved.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Local WordPress 5.1.1 multisite with Nginx’ is closed to new replies.