cancel
Showing results for 
Search instead for 
Did you mean: 

Magento 2.X + NGINX Complete Multistore Guide and Setup

Magento 2.X + NGINX Complete Multistore Guide and Setup

Hi to all, i'm starting this discussione because there is a lack of information about this topic.

I've read undred of question, blog post, article and KB post, but in 2021 there is a lack of a detailed and complete guide to setup a multistore with magento 2.4 running under nginx.

 

So i will try to do my best to organize this discussion, hoping someone can post important information.

 

TODO:

Can't access store view by url (www.2domain.com/en

 

The first step is to clearly identify the objectives:


A) Having different websites on different domains (e.g. 1domain.com and 2domain.com)

B) Manage languages ​​via url (eg www.1domain.com/en - www.1domain.com/es)

or

C) Manage languages ​​by domain
(e.g. www.2domain.en - www.2domain.es)

 

Configuration

 

1) Create store view - store - website from admin

https://devdocs.magento.com/guides/v2.4/config-guide/multi-site/ms_websites.html#step-2-create-websi...

 

WEBSITE 1

Main Website -> Code: base (base url = www.1domain.com) 

STORE

Main website Store -> Code: main_website_store

STOREVIEW

Default Store View -> Code: default

 

 

WEBSITE 2

2domain -> Code: 2domain (base url = www.2domain.com) 

STORE

2domain -> Code: 2domain

STOREVIEW

2domain_it -> Code: 2domain_it (base url = www.2domain.com/it/) 

2domain_en -> Code: 2domain_en (base url = www.2domain.com/en/) 

 

 

2) Configure Nginx

https://devdocs.magento.com/guides/v2.4/config-guide/multi-site/ms_nginx.html

 

Simple approach is to handle a single virtualhost config file for nginx:

 

 

map $http_host $MAGE_RUN_CODE {
    default '';
    www.1domain.com default;
    www.2domain.com 2domain;
}

upstream fastcgi_backend {
  server  unix:/run/php/php7.4-fpm.sock;
}

server {

  listen 80;
  server_name www.1domain.com www.2domain.com;
  set $MAGE_ROOT /var/www/html/magento2;
  set $MAGE_MODE developer;
  set $MAGE_RUN_TYPE website; #$MAGE_RUN_TYPE to store or website;
  include /var/www/html/magento2/nginx.conf;
}

 

Now edit the default nginx.conf in the magento root folder and add

 

        fastcgi_param MAGE_RUN_TYPE $MAGE_RUN_TYPE;
        fastcgi_param MAGE_RUN_CODE $MAGE_RUN_CODE;

Just before the 

 

       include        fastcgi_params;
    }

In the 

# PHP entry point for main application

Section.

 

ATTENTION

I found a first error in official documentation, that miss this foundamental step:

 

Remember to disable the default config "Auto-redirect to Base URL" or your secondo website url will redirect to primary one!

 

Go to Stores > Settings > Configuration > General > Web and set "No" to Auto-redirect to Base URL

 

 

Testing (and problem)

 

 

www.1domain.com work with no problem, is the dafault website/store/storeview

 

www.2domain.com work with default language of the  2domain_it store view but with this problem:

 

1) Logo URL point to www.1domain.com

2) Every url on the store point to www.1domain.com

3) Modification on logo not take effect on 2domain.com (1domain.com luma default logo are used)

4) Welcome message are not shown regardless of what we put in the custom setup of the 2domain.com store view

5) try to access the 2domain.com /it and /en base url (that are configured in the respective store view for base url) get a 404 error

5) Other language mapped on store view are not accessible

 

So what is wrong? What was miss on magento documentation? Some random question:

 

The mage run code has a reference to run website or store view code, so its impossible to run both and have different website with same storeview language code?

 

a.it -> storeview code it

a.en -> storeview code en

a.de -> storeview code de

 

b.it -> storeview code it

b.com -> storeview code en

b.de -> storeview code de

 

I think the answer is NO, but there is no clear information on that.

If answer is NO, the right way to have different website with same language is to use a custom store view code?

 

Something like

 

a.it -> storeview code a_it

a.en -> storeview code a_en

a.de -> storeview code a_de

 

b.it -> storeview code b_it

b.com -> storeview code b_en

b.de -> storeview code b_de

 

So if i setup a configuration like this i can run mage_run_type store and call the store view unique code to reach the right store view for every domain?

 

Ok, but how if i need to use it with a same domain?

 

a.it

-> storeview code a_it

-> storeview code a_en

-> storeview code a_de

 

b.it

-> storeview code a_it

-> storeview code a_en

-> storeview code a_de

 

I cant use the 

 

map $http_host $MAGE_RUN_CODE {
    default '';
    french.mysite.mg french;
    german.mysite.mg german;
}

to map a different store view becasue the http_host of all this language is the same (a.it and b.it).

So the right way is to use url path, but correct me if im wrong, this is not supported by magento without custom modification, right?

 

a.it

a.it/it -> storeview code a_it

a.it/en -> storeview code a_en

a.it/de -> storeview code a_de

 

b.it

b.it/it -> storeview code a_it

b.it/en -> storeview code a_en

b.it/de -> storeview code a_de

 

Wich are the right way tho handle this? I have not found any information in 10 years magento post to adress this. Need to handle in nginx location ?

 

something like

server a.it {

$MAGE_RUN_TYPE = store

location /it {

$MAGE_RUN_CODE = a_it

}

location /en {

$MAGE_RUN_CODE = a_en

}

location /de {

$MAGE_RUN_CODE = a_de

}

}
server b.it {

$MAGE_RUN_TYPE = store

location /it {

$MAGE_RUN_CODE = b_it

}

location /en {

$MAGE_RUN_CODE = b_en

}

location /de {

$MAGE_RUN_CODE = b_de

}

}

??

Or i'm forced to use the "include store code in url" to adress this and create a subdirectory for each language with links to root magento folder? 

 

or a mix of both solution, so handle the code assignation from nginx configuration, but create the "fake" subfolder to avoid the 404 error on language url?

 

Any help appreciate.

 

 

4 REPLIES 4

Re: Magento 2.X + NGINX Complete Multistore Guide and Setup

Ok .. 0 reply , maybe this is not an hot thread? 

Re: Magento 2.X + NGINX Complete Multistore Guide and Setup

Hi,

 

I'm looking for a solution to managing all the sub-directories stuff directly in Nginx configuration, without using pub/[sub-folder] (which is basically this approach: https://www.zealousweb.com/nginx-integration-in-magento-2-for-multi-websites/).

 

Maybe you're not looking for a solution to this anymore, but if I figure out something useful I'll share here for further readers.

 

Cheers!!

Re: Magento 2.X + NGINX Complete Multistore Guide and Setup

have you found a solution?

Re: Magento 2.X + NGINX Complete Multistore Guide and Setup

Was there a solution for this?