Configure run codes and run types in Nginx

in Nginx configuration Tags: Nginx

Magento uses MAGE_RUN_CODE and MAGE_RUN_TYPE to serve specific storefronts and websites.

One Magento installation can serve multiple websites or store views that use different languages, domain names and categories.
This enables one Magento install and Admin to manage different stores on multiple domains.

You configure the websites, stores, and store views in the Magento Admin. After configuring, you can easily add these codes to the /data/web/nginx/http.magerunmaps file in the nginx config include directory of the hypernode to map domains to its particular storefront.

Normally MAGE_RUN_TYPE and MAGE_RUN_CODE are configured with different options and different domains.
For example, you can create one set of categories and products for one domain and another set for a domain that has different categories and products for a shop focussed on another language or country.

The webserver doesn’t set the environment variables MAGE_RUN_CODE and MAGE_RUN_TYPE by default.

More information regarding these Nginx settings can be found in this article

Set a different store_code per domain

Use a recipe like this to configure your store fronts en sites:

map $host $storecode {
  hostnames;
  default nl_store;
    .shoe.nl nl_store;
    .belgischeschoenen.com be_store;
    shoes.otherdomain.com be_store;
}

# run something.example.come in website mode (usually not required)
map $host $storetype {
  hostnames;
  default store;
    something.example.com website;
}

The values for $storecode and $storetype will be assigned to the environment variables MAGE_RUN_CODE and MAGE_RUN_TYPE.

Set a different store_code per subdirectory

If you want to serve a different store_code per subdirectory, this can be done using another map construction:

map $request_uri $storecode {
    default default;
    ~^/nl storecode_nl;
    ~^fr  storecode_fr;
    ~^be  storecode_be;
}

Additionally you need to create a symlink to the root dir for each subdirectory:

for SUBDIR in nl fr be en de; do 
    ln -fs /data/web/public /data/web/public/$SUBDIR
done 

Tips and tricks:

  • Because the first line of the map is hostnames, a string like .shoe.nl will match both www.shoe.nl and shoe.nl.
  • Don’t use wildcards as these will not work. Wildcards do not match on domain maps. instead use .shoe.nl as a wildcard for both www.shoe.nl and shoe.nl.
  • Don’t add port numbers for the staging environment, your domains will match the same domain name as the production site.
  • If no domain matches the one requested, the default storecode is used.

1