Configure run codes and run types in Nginx

in Nginx configuration Tags: Nginx

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.

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.

After you configured the websites, stores, and store views in the Magento Admin, 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 specific domains to its particular storefront.
This sets the correct MAGE_RUN_CODE and MAGE_RUN_TYPE by for each used domain.

More information regarding Nginx mappings 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 

Testing storefronts

Serving specific storefronts for testing

If you want to test your storefronts before switching the DNS, you can easily switch storefronts by editing your http.magerunmaps.
To do this, add your appname to the mapping and configure a storefront for it:

map $host $storecode {
  hostnames;
  default default;
  appname.hypernode.io be_site;
}

Now if you visit your hypernode on appname.hypernode.io, the configured storefront (in this case the be_site) is shown.

Testing which storefront is selected for a domain

To test which storefront code is used for a particular domain, use the following snippet in your nginx config:

location /testing {
    add_header Content-Type text/plain;
    return 200 "#### $storecode ####";
}

Now if you request https://your.domain.com/testing to get the storecode shown in your browser.

Tips and tricks

  • 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.
  • 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 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 if the default configuration option is given. Otherwise a 404 will be returned.
  • Additionally as a helper tool, you can use n98-magerun hypernode:maps-generate to generate an example mapping based on your current storefront configuration.

1