How to set up multiple Magento 2 websites / stores with Nginx?

September 21, 2016  |  6 Comments  |  by Raj (MagePsycho)  |  Magento 2, Nginx

Magento is so flexible that one instance of it can have multiple websites/stores with different languages, domain names, categories etc.
You can configure the websites, stores, and store views in the Magento Admin. You use the MAGE_RUN_TYPE and MAGE_RUN_CODE variables in entry point scripts(index.php), .htaccess or Nginx configuration files(depending upon the web server you are using) to start the Magento application using these websites or store views.

Usage of MAGE_RUN_TYPE and MAGE_RUN_CODE

The code (i.e. MAGE_RUN_CODE and MAGE_RUN_TYPE) checks two environmental variables

$_SERVER['MAGE_RUN_TYPE']
$_SERVER['MAGE_RUN_CODE']

and use them to start Magento with the right website/store which can be defined in Manage Stores section in Admin.

The value of MAGE_RUN_TYPE determines if MAGE_RUN_CODE should be considered the code of a website or a store.

  • If MAGE_RUN_TYPE = ‘website’ is used, MAGE_RUN_CODE should be the code of the website and default store for this website will be loaded in the frontend.
  • If MAGE_RUN_TYPE = ‘store’ is used, MAGE_RUN_CODE should be the code of any store view and that particular store will be loaded in the frontend.

Nginx Configuration Settings

As mentioned before, there are many ways to configure the MAGE_RUN_TYPE and MAGE_RUN_CODE environment variables. But setting environment variables using web servers would be the best way as it doesn’t involve any core code edits.
Since I am using MEMP Stack, I will be sharing on how to configure Nginx for multi-website/store environment.

Case 1: One Website, Multiple Store Views

For example, we have

  • mystore.com (store code: mystore_en)
  • mystore.de (store code: mystore_de)
  • mystore.es (store code: mystore_es)

Step 1. Edit your Nginx virtual host configuration file as

File: Usually located under /etc/nginx/sites-available/ or /usr/local/etc/nginx/sites-available/ or other depending upon the OS type.

map $http_host $MAGE_RUN_CODE {
    mystore.com mystore_en;
    mystore.de mystore_de;
    mystore.es mystore_es;
}
server {
    listen 80;
    server_name mystore.com mystore.de mystore.es;
    set $MAGE_ROOT /path/to/your/magento2;
    set $MAGE_MODE default;
    include /path/to/your/magento2/nginx.conf.sample;
}

Here you can see how the Nginx map block is used to set MAGE_RUN_CODE as per host. And server_name directive includes all the available domains.

Step 2. Send MAGE_RUN_CODE and MAGE_RUN_TYPE variables to the php-fpm server
File: include path from above, example: path/to/your/magento2/nginx.conf.sample

#...
# PHP entry point for main application
location ~ (index|get|static|report|404|503)\.php$ {
    try_files $uri =404;
    fastcgi_pass   fastcgi_backend;
    fastcgi_buffers 1024 4k;

    fastcgi_param  PHP_FLAG  "session.auto_start=off \n suhosin.session.cryptua=off";
    fastcgi_param  PHP_VALUE "memory_limit=768M \n max_execution_time=600";
    fastcgi_read_timeout 600s;
    fastcgi_connect_timeout 600s;
    
    #add here - start
    fastcgi_param  MAGE_RUN_TYPE store;
    fastcgi_param  MAGE_RUN_CODE $MAGE_RUN_CODE;
    #end

    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
}
#...

Here Nginx fastcgi_param‘s MAGE_RUN_TYPE & MAGE_RUN_CODE will create the enviroment varilables for PHP, so that the script can access the values via

$_SERVER['MAGE_RUN_TYPE']
$_SERVER['MAGE_RUN_CODE']

Case 2: Multiple Website, Multiple Store views

For example, we have

  • mystore1.com (website code: mystore1)
    • mystore1.com (store code: mystore1_en)
    • mystore1.de (store code: mystore1_de)
    • mystore1.es (store code: mystore1_es)
  • mystore2.com (website code: mystore2)
    • mystore2.com (store code: mystore2_en)
    • mystore2.de (store code: mystore2_de)
    • mystore2.es (store code: mystore2_es)

In order to configure for this case, you can use the similar settings as for Case 1. The only difference will be the store code values for Nginx map block and domain names for server_name directive.

Conclusion

If you are unsure about which value(website or store) should you use for MAGE_RUN_TYPE, go for the store value and use the corresponding store code for MAGE_RUN_CODE

Hope this helps you in configuring the Nginx for multi-store/website. In the case of any issues with the multi-store setup, please do comment below.

Posted in Magento 2, Nginx and tagged , , , . Bookmark the permalink.

About Raj (MagePsycho)

Raj, the guy behind MagePsycho is a Zend PHP5 Certified Engineer, Magento Certified Developer, Magento Moderator / Freelancer with specialization in web applications (CMS, E-Commerce, ERP etc.). Catch him on: Twitter: @magepsycho Skype: magentopycho
  • Franck

    Hello, how do you manage the multiwebsite/store if you want to keep the same domain but use code in URL such as : Website 1 : EN , Website 2 : FR and have this stores : en_en, en_fr, fr_fr, fr_en, fr_es, … With this kind of URL : http://domain.com/fr_fr

    • Magento Developer

      You just need to turn on the “Add Store Code to Urls” setting.

  • Hùng Nguyễn

    Hi, I setup as case 1, but I met problem with redirect loop. Can you help me to explain the situation. As I search from google, it may related to SSL configuration. But I haven’t enabled SSL yet

    • Magento Developer

      Try to print $_SERVER variable in pub/index.php and post the result here.

  • Hiren Saheb

    Thank you for sharing this blog.
    But how to configure multiple store with sub-directory i.e.
    – mywebsite.com/man
    – mywebsite.com/woman

    • Magento Developer

      May be you can make use of “Add Store Code to Urls” settings.