Magento 2 – How to Configure Nginx for Multiple Websites with Sub-Folder

magento2multi-websitenginx

We would like to create multiple websites in Magento 2. There is an article about this topic in the Official Magento 2 Documentation, but the way they describe is not suitable for our case.

They are suggesting to use sub-domains in order to determine different websites, such as

  • website1.magento-site.com
  • website2.magento-site.com

We would like to use sub-folders instead of sub-domains. To give an example,

  • magento-site.com/website1
  • magento-site.com/website2

How can we overcome this issue on Nginx web server?

My Configuration

I am using Ubuntu 16.04. I have installed Nginx, and have not changed any Nginx core configuration. I have created a file magento-site.com.conf inside /etc/nginx/sites-enabled/magento-site.com.conf.

/etc/nginx/sites-enabled/magento-site.com.conf

server {
    listen 8080;
    server_name magento-site.com;

    set $MAGE_RUN_CODE website1;
    set $MAGE_ROOT /var/www/magento-site.com;
    include /var/www/magento-site.com/nginx.conf;
}

EDIT 1: (2017-10-23)

I have multiple stores for each website.

Best Answer

What about a pure nginx solution with this double map ?

First, for the web site (thanks @MagenX)

map $request_uri $MAGE_RUN_CODE {
    default website1;
    ~^/website1/.*  website1;
    ~^/website2/.*  website2;
}

A second for the new request uri

map $request_uri $REQUEST_URI {
    default  $request_uri;
    "~*^/(website[0-9])(?<p>.*)" $p;
}

And finally, dont forget to set the new computed REQUEST_URI

location ~ \.php$ {
(...)
   include fastcgi_params;
   fastcgi_param MAGE_RUN_CODE $MAGE_RUN_CODE;
   fastcgi_param REQUEST_URI $REQUEST_URI;
(...)
}