Is there a problem that needs an urgent fix?Urgent fix?Get emergency help →
Blog

Practical Magento 2 guides on upgrades, security, speed and migration, written by the MageBooster team.

Magento 2.4.9 multisite: three websites on three domains (Lesson 11)

Three Magento 2.4.9 websites on three domains: websites, stores and store views, a base URL per website, magento-vars.php through composer autoload files, the nginx map + fastcgi_param way, MAGE_RUN_TYPE store and Varnish for all domains.
Lesson 11MageBooster Lessons · Magento 2.4.9

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.

Updated October 2026Video about 8 minLevel Developers and server admins

Website, store, store view

LevelWhat it holdsIn this lesson
WebsiteOwn prices (with Catalog Price Scope = Website), payment and shipping settings, customer accounts (with Account Sharing per website)Main Website (base), Germany (germany), Netherlands (netherlands)
StoreInside a website; sets the root category (catalog tree and menu)Main Website Store, Germany Store, Netherlands Store, all with Default Category
Store viewA language or a look of a store: texts, theme, localedefault (+ 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:

  1. Create Website: Name Germany, Code germany (lowercase letters, numbers, underscore; starts with a letter). Save Web Site.
  2. Create Store: Web Site Germany, Name Germany Store, Code germany_store, Root Category Default Category (or your own root category for its own menu). Save Store.
  3. Create Store View: Store Germany Store, Name Deutsch, Code de_de, Status Enabled. Save Store View.
  4. The same for Netherlands / netherlands, Netherlands Store / netherlands_store, Nederlands / nl_nl.
Products show on a website only when they are assigned to it (Product in Websites, Lesson 10), and the website needs a stock (Stores › Inventory › Stocks) to sell them.

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:

multisite-config.shDownload

#!/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:

magento-vars.phpDownload

<?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:

magento-vars-autoload.shDownload

#!/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/):

multisite-map.confDownload

# 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:

nginx-multisite.shDownload

#!/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
Use one way, not both. In the video magento-vars.php is taken out of composer.json again after the nginx setup.

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.

With 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:

multisite-check.shDownload

# 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

SymptomCause and fix
de/nl redirect to wwwNo 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 happensOPcache with validate_timestamps=0: reload PHP-FPM. After composer dump-autoload too.
The include line in pub/index.php is goneA Magento update replaced pub/index.php. Use composer.json autoload files or nginx.
UNCACHEABLE on the other websitesMAGE_RUN_TYPE website: switch to store with the store view codes.
Wrong certificate on a domainThe certificate must list every domain (SAN), or use one server block per domain.
A website shows no productsAssign 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.

Running several shops on one Magento?We set up Magento and Mage-OS multisite with nginx, Varnish and clean store structures.

▶ Watch every MageBooster lesson on YouTube: all lessons in order · subscribe to the channel

Installation guides: Magento 2.4.9 on Ubuntu Mage-OS Hyvä
Spotted a mistake on this page? Tell us and we will fix it right away.

Free Site Audit

Send your store details. We check speed, security and extensions, then reply with three quick wins within 3 working days.

Let's talk

Get a quote

Tell us about your store and we’ll reply within one working day. Prefer to chat? Say hello from WhatsApp

MageBooster Book a UnoPim demo See UnoPim live with products like yours. A new PIM or a move from Akeneo: we reply with a plan, a timeline and a quote.
✓ Official UnoPim partner✓ AI agent set up for your team✓ Connected to Magento
MageBooster Start your Hyvä project From scratch or from your current store. We reply with a project plan, a timeline and a quote.
✓ Design and structure by us✓ Hyvä-optimised server✓ Bonus modules included

MageBooster

Let us get in touch with you

Leave your details and your store address. We look at your store first and contact you within one business day, by phone, WhatsApp or email, whichever you prefer.

Partner with us