1

Im trying to configure a website as a sub location of another website. For more context, i have wp1 and wp2, both wordpress sites, and wp2 needs to be a sub location of wp1, like http://wp1.com/wp2 but i cant find the way to actually do it, i read the avaliable documentation but still cant find why this isnt actually working.

My nginx configuration file:

server {
listen 80;
listen [::]:80 ;

server_name 1.2.3.4; #(IP)

root /var/www/html/wp1;
index index.php;

client_max_body_size 200M;

satisfy any;
allow 1.2.3.4;
deny all;

auth_basic "Restricted";                    #For Basic Auth
auth_basic_user_file /etc/nginx/.htpasswd;  #For Basic Auth

location /wp2 {
    root /var/www/html/wp2;
    index index.php;
    try_files $uri $uri/ /index.php?$args;

    location ~ \.php$ {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME 
                $document_root$fastcgi_script_name;
                include fastcgi_params;
        }

}

location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ /index.php?$args;
    }

location ~ \.php$ {
           try_files $uri =404;
           fastcgi_split_path_info ^(.+\.php)(/.+)$;
           fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
           fastcgi_index index.php;
           fastcgi_param SCRIPT_FILENAME 
           $document_root$fastcgi_script_name;
           include fastcgi_params;
    }
}

1 Answers1

0

It looks like your location and root statements are wrong.

The location ~ \.php$ block in the server context will take precedence over the location /wp2 block. Use the ^~ modifier. See this document for details.

I presume that your files are located at /var/www/html/wp2/index.php, and not /var/www/html/wp2/wp2/index.php. See this document for details.

For example:

location ^~ /wp2 {
    root /var/www/html;
    ...
}
Richard Smith
  • 11,859
  • 2
  • 18
  • 26
  • Thank you this helped me get closer to fix the problem! But lets say i want to be using /foo as a endpoint to /wp2 do i need to make a rewrite rule to remove /foo and say the root is /var/www/html/wp2? Thanks! – Guillermo Teixeira Oct 09 '18 at 13:47
  • Use `alias` instead of `root`. See [my answer here](https://serverfault.com/questions/884981/nginx-multiple-for-now-two-php-projects-via-php-fpm-in-subdirectories-using-al/884993#884993). – Richard Smith Oct 09 '18 at 13:55