Magento multisite: three websites on three domains
Three Magento 2.4.9 websites, each on its own domain, on one installation behind one Varnish: the websites, stores and store views in the admin, a base URL per website, and the two ways to tell Magento which store a domain belongs to: magento-vars.php and nginx. Plus the one setting that decides whether Varnish caches the other websites for first-time visitors.
Website, store, store view
| Level | What it holds | In this lesson |
|---|---|---|
| Website | Own prices (with Catalog Price Scope = Website), payment and shipping settings, customer accounts (with Account Sharing per website) | Main Website (base), Germany (germany), Netherlands (netherlands) |
| Store | Inside a website; sets the root category (catalog tree and menu) | Main Website Store, Germany Store, Netherlands Store, all with Default Category |
| Store view | A language or a look of a store: texts, theme, locale | default (+ de from Lesson 4), de_de, nl_nl |
Domains in the video: www.example.com, de.example.com, nl.example.com. In real life they point to your server in DNS; on a test server use the hosts file. The certificate must cover all of them.
Three websites
Stores › All Stores:
- Create Website: Name
Germany, Codegermany(lowercase letters, numbers, underscore; starts with a letter). Save Web Site. - Create Store: Web Site Germany, Name
Germany Store, Codegermany_store, Root Category Default Category (or your own root category for its own menu). Save Store. - Create Store View: Store Germany Store, Name
Deutsch, Codede_de, Status Enabled. Save Store View. - The same for Netherlands /
netherlands,Netherlands Store/netherlands_store,Nederlands/nl_nl.
A base URL per website
Stores › Configuration › General › Web, scope Germany: Base URLs › Base URL and Base URLs (Secure) › Secure Base URL = https://de.example.com/ (untick Use Default). The link, static and media URLs default to placeholders based on these. The same from the command line, with a welcome text per website so you can tell them apart:
#!/bin/bash # MageBooster Lesson 11: base URL and welcome text per website with bin/magento (run on the server, in the Magento folder) cd /var/www/magento for p in unsecure secure; do sudo -u magento bin/magento config:set --scope=websites --scope-code=germany web/$p/base_url https://de.example.com/ sudo -u magento bin/magento config:set --scope=websites --scope-code=netherlands web/$p/base_url https://nl.example.com/ done sudo -u magento bin/magento config:set --scope=websites --scope-code=base design/header/welcome "Welcome to MageBooster UK" sudo -u magento bin/magento config:set --scope=websites --scope-code=germany design/header/welcome "Willkommen bei MageBooster Deutschland" sudo -u magento bin/magento config:set --scope=websites --scope-code=netherlands design/header/welcome "Welkom bij MageBooster Nederland" sudo -u magento bin/magento cache:flush
Without a run code, Magento runs the default website for every domain and redirects de.example.com and nl.example.com to www.example.com (its base URL).
magento-vars.php
Magento picks the store from two server variables: MAGE_RUN_TYPE (website or store) and MAGE_RUN_CODE (the matching code). magento-vars.php in the Magento root sets them per domain:
<?php
// magento-vars.php: open the right store view (and so the right website) for each domain
$stores = [
'www.example.com' => 'default',
'de.example.com' => 'de_de',
'nl.example.com' => 'nl_nl',
];
$host = $_SERVER['HTTP_HOST'] ?? '';
if (isset($stores[$host])) {
$_SERVER['MAGE_RUN_CODE'] = $stores[$host];
$_SERVER['MAGE_RUN_TYPE'] = 'store';
}
This file is known from Adobe’s guides for Adobe Commerce on cloud infrastructure. On a self-hosted Magento 2.4.9 nothing loads it: we searched pub/index.php, app/bootstrap.php, app/autoload.php and vendor/magento/framework. Some shops add include __DIR__ . '/../magento-vars.php'; to pub/index.php, but the magento/magento2-base package replaces that file on every update, and the line is lost. Load it through Composer instead:
#!/bin/bash # MageBooster Lesson 11: load magento-vars.php through Composer (use this OR the nginx way, not both) cd /var/www/magento sed -i 's#"app/etc/NonComposerComponentRegistration.php"#&,\n "magento-vars.php"#' composer.json sudo -u magento composer dump-autoload systemctl reload php8.5-fpm # needed when opcache.validate_timestamps=0
The nginx way (the one we keep)
No PHP file: nginx sets the same variables. A map in the http block (any file in /etc/nginx/conf.d/):
# domain -> Magento store view code
map $http_host $MAGE_RUN_CODE {
default default;
www.example.com default;
de.example.com de_de;
nl.example.com nl_nl;
}
nginx must pass them to PHP in the PHP entry point block (location ~ ^/(index|get|static|...)\.php$). fastcgi_param lines in the server block are not inherited by a location that has its own fastcgi_param lines, so they go inside it. nginx.conf.sample is also replaced on updates, so copy it to /etc/nginx/ first. The two lines:
fastcgi_param MAGE_RUN_TYPE store; fastcgi_param MAGE_RUN_CODE $MAGE_RUN_CODE;
All domains in server_name, the include pointing to the copy, then nginx -t and a reload. The whole step as a script:
#!/bin/bash
# MageBooster Lesson 11: nginx picks the Magento store view per domain (run as root, in the Magento folder)
set -e
cd /var/www/magento
cat > /etc/nginx/conf.d/multisite-map.conf <<'EOF'
# domain -> Magento store view code
map $http_host $MAGE_RUN_CODE {
default default;
www.example.com default;
de.example.com de_de;
nl.example.com nl_nl;
}
EOF
# a copy of nginx.conf.sample (Magento updates replace the original) with MAGE_RUN_TYPE / MAGE_RUN_CODE in the PHP entry point block
cp nginx.conf.sample /etc/nginx/magento-multisite.conf
sed -i '/^location ~ ^\/(index|get|static/,/^}/ s#^ include fastcgi_params;#&\n fastcgi_param MAGE_RUN_TYPE store;\n fastcgi_param MAGE_RUN_CODE $MAGE_RUN_CODE;#' /etc/nginx/magento-multisite.conf
# all domains in server_name, include the copy (adjust to your own server blocks)
sed -i 's#server_name www.example.com;#server_name www.example.com de.example.com nl.example.com;#; s#include /var/www/magento/nginx.conf.sample;#include /etc/nginx/magento-multisite.conf;#' /etc/nginx/conf.d/magento.conf
nginx -t && systemctl reload nginx
Why MAGE_RUN_TYPE store, not website
With website, Magento compares each page with the default store view of the default website. For the other websites the context differs, so Magento sets the X-Magento-Vary cookie, and the Magento VCL does not cache a response that sets this cookie when the request has none. Result: every first visit to de.example.com and nl.example.com goes to PHP (X-Magento-Cache-Debug: UNCACHEABLE); a browser does not get the cookie from the home page alone. With store and the store view code, that store view is the default for its domain, and first visits are cached.
store, Magento accepts every active store view code in the ___store parameter, also one of another website. The store switcher of a website (e.g. English/German on www) keeps working.Varnish for all domains
All domains go through the same Varnish (Lesson 2). Varnish keeps a separate copy per domain; one bin/magento cache:flush full_page sends a purge with a tag pattern and clears every domain. Check:
# which store opens on each domain, then Varnish: a MISS and a HIT per domain
for d in www de nl; do echo "$d.example.com $(curl -s https://$d.example.com/ | grep -oE "(Welcome|Willkommen|Welkom) [^']+" | head -1)"; done
for d in www de nl; do for i in 1 2; do curl -so /dev/null -w "$d.example.com %header{x-magento-cache-debug}\n" https://$d.example.com/; done; done
Expected: MISS, then HIT for each domain.
Downloads
Troubleshooting
| Symptom | Cause and fix |
|---|---|
| de/nl redirect to www | No run code reaches PHP: check the map, the fastcgi_param lines inside the PHP location, and nginx -t; or magento-vars.php is not loaded. |
| magento-vars.php changed, nothing happens | OPcache with validate_timestamps=0: reload PHP-FPM. After composer dump-autoload too. |
| The include line in pub/index.php is gone | A Magento update replaced pub/index.php. Use composer.json autoload files or nginx. |
| UNCACHEABLE on the other websites | MAGE_RUN_TYPE website: switch to store with the store view codes. |
| Wrong certificate on a domain | The certificate must list every domain (SAN), or use one server block per domain. |
| A website shows no products | Assign products to the website and give the website a stock (MSI). |
Next lesson
Lesson 12: VAT and tax settings, for a single site and for multisite.
▶ Watch every MageBooster lesson on YouTube: all lessons in order · subscribe to the channel
