1

I'm trying to separate document root of WordPress installations under subdirectories.

Document root of example.com : /var/www/example.com/

Document root of example.com/xyz : /var/www/example.com-xyz/

after a long research, i was able to resolve all the wordpress sites configured like above.

I used below configuration,

root /var/www/example.com/htdocs;
    location / {
                    try_files $uri $uri/ /index.php?$args;

      location ~ \.php$ {
        fastcgi_split_path_info ^(.*\.php)(.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
        fastcgi_pass 127.0.0.1:9000;
      }
    }


    location /xyz {

      alias /var/www/example.com-xyz/htdocs/;
      try_files $uri $uri/ @xyz;

           location ~ \.php$ {
                    fastcgi_split_path_info ^(.*\.php)(.*)$;
                    include fastcgi_params;
                    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                    fastcgi_param PATH_INFO $fastcgi_path_info;
                    fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
                    fastcgi_pass 127.0.0.1:9000;
            }

    }

    location @xyz {
            rewrite ^/xyz(.*) /xyz/index.php?q=$1;
    }

This will worked for long. But Problem is that it I couldn't create a page with name starting with above used subdirectory name. Like, http://example.com/xyzzz http://example.com/xyz-something

cannot be made since above request will match subdirectory location block :(

So, I tried location = /xyz { } but I couldnot include

location ~ .php$ { } inside that :( it will throw error like

nginx: [emerg] location "\.php$" cannot be inside the exact location "/xyz"

am I doing the configuration in wrong way ? is there any way to resolve this ?

0

You should use location /xyz/ to avoid problems with other URIs that begin with the same three letters. You can redirect /xyz to /xyz/ with a separate statement. For example:

location = /xyz { rewrite ^ /xyz/ last; }

location /xyz/ {
    alias /var/www/example.com-xyz/htdocs/;
    ...
}

See this document for details.

| improve this answer | |
  • I tried the same and got forbidden suddently and error log had following entry [error] 20518#0: *1363 directory index of "/var/www/example.com is forbidden – Anto May 29 '17 at 15:26
  • You should add index index.php; into your configuration. Near the top of the server block perhaps. – Richard Smith May 29 '17 at 15:32
  • and I tried with index index.php and not luck and when tried with location ~* ^/xyz/*$ location ~* /xyz/ and many more, the issue was , .jss, .css , images under /xyz/wp-content/.... and /xyz/wp-includes/ was throwing 404 error . – Anto May 29 '17 at 15:36
0

At last got solution!

changed the following.

mainsite root: /var/www/example.com/htdocs/

created following symlinks under mainsite root

site1 -> /var/www/example.com-xyz/htdocs 
site2 -> /var/www/example.com-site2/htdocs 

and changed the nginx location blocks as following

location / {
        try_files $uri $uri/ /index.php?$args;
      include conf.d/php7.conf;
}

location  /site1/ {

   try_files $uri $uri/ @site1;

   access_log /var/log/nginx/example.com-site1.access.log;
   error_log /var/log/nginx/example.com-site1.error.log;

    include conf.d/php7.conf;   #this filehave normal phpfpm block like "location ~ \.php$ {  } "

 }

 location @site1 {
   rewrite ^/site1(.*) /site1/index.php?q=$1;
 }

NB: location /something/ { alias /var/www/... } created nginx forbidden location issue. (can't use alias )

Here symlinks done all the jobs :) no complex configurations. Hope this may help someone.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.