Imagine you need to run a Magento 2 installation from a sub-directory, using Nginx, but you don’t have ownership of the main site, just the sub-directory.
So we are not talking here about store view codes, redirect loops etc, we are talking about this:
Server 1 : www.website.domain
/shop/ -> IP Server 2
Server 2 : www.website.domain/shop/
The problem
Magento 2 has differences in terms of application structure and directories with Magento 1. (see image below)
The most important difference here is the endpoint. While in Magento 1 the endpoint is the index file of the Magento installation, “[magento]/index.php”, in Magento 2, the endpoint is inside the pub folder, “/[magento]/pub/index.php”.
Therefore all request for each store-view will go through this folder.
Create Subfolders
For each Base URL you have to add a sub-folder at the end, so if you need to have www.website.domain/shop You will need to create :
mkdir ~/[magento]/pub/shop
Create Symlinks
Then you need to create symlinks inside the sub-folder just created to the static content folders, like ‘media’, ‘static’… This is necessary because Magento 2 will go to gather this information inside the reference given by the endpoint, that is inside the “pub/shop” folder.
For example:
ln -s ../media media
ln -s ../static static
For the index.php, instead of symlink. You need to copy it and modify the relative path for the app/bootstrap script. Like follows:
Change:
require realpath(DIR) . ‘/../app/bootstrap.php’;
To:
require realpath(DIR) . ‘/../../app/bootstrap.php’;
Nginx Configuration
And finally, make sure that the entry point of the Nginx file points to the correct folder.
location /shop/ { try_files $uri $uri/ /shop/index.php$is_args$args; }
# PHP entry point for main application location ~ (index|get|static|errors/report|errors/404|errors/503|health_check|__info)\.php$ { try_files $uri =404; fastcgi_pass fastcgi_backend; fastcgi_buffers 16 16k; fastcgi_buffer_size 32k; fastcgi_param PHP_FLAG "session.auto_start=off \n suhosin.session.cryptua=off"; fastcgi_param PHP_VALUE "memory_limit=2048M \n max_execution_time=18000"; fastcgi_read_timeout 600s; fastcgi_connect_timeout 600s; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }