Magento / Nginx

Magento 1.x multi store Nginx configuration

by , , revisited on


We have by far the largest RPM repository with dynamic stable NGINX modules and VMODs for Varnish 4.1 and 6.0 LTS. If you want to install nginx, Varnish and lots of useful modules for them, this is your one stop repository to get all performance related software.
You have to maintain an active subscription in order to be able to use the repository!

Multiple websites. Single Magento

Magento 1.x (same with Magento 2) has a great feature – multiple store views and websites.

Let’s assume you have setup 2 websites inside your Magento:

  • aaa.example.com with code base
  • bbb.example.com with code bbb

Both of the websites use single Magento install at, e.g. /var/www/html.

It’s great. You will need to apply patches just once, you can reuse configuration between two websites, etc. etc.

But how do you serve different robots.txt for each website? And in general: what is the proper way of configuring multiple websites served by single Magento installation?

The following are few quick steps to get you up and running. We will leverage the Nginx features at its full. No changes to Magento PHP files required.

Define host mapping

Open up your /etc/nginx/nginx.conf and add under http {} block:

map $http_host $MAGE_RUN_CODE {
    "^(www\.)?aaa.example.com" base;
    "^(www\.)?bbb.example.com" bbb;
}

This tells Nginx to assign environment variable $MAGE_RUN_CODE to “base” if request is for a page with host equal to either aaa.example.com or www.aaa.example.com.
Subsequently $MAGE_RUN_CODE will get value of bbb if request is for bbb.example.com or www.bbb.example.com.

Magento PHP code can read that environment variable and load proper website.

Setup Nginx server blocks

If you haven’t done that already, setup Nginx server blocks (aka virtual hosts).

Create separate configuration file for each Magento website, i.e. /etc/nginx/sites-available/aaa.example.com.conf and /etc/nginx/sites-available/bbb.example.com.conf and put the same configuration as you would if it was single site with respective domain name. Both files will have same root directive, i.e.:

root /var/www/html;

Note: with Nginx host map in place you can actually have one server block for all Magento websites, but having separate Nginx server block for each Magento website is the proper way to go in case you have different SSL certificate for each website (most common).

Serve different robots.txt for each website

Now that you have setup Nginx server blocks for each Magento website, how do you serve different robots.txt? Both sites map to the same /var/www/html!

Simple, you can leverage the Nginx variable we have created earlier. Paste to each server block:

location = /robots.txt {
    try_files /$MAGE_RUN_CODE.robots.txt $uri;
}

Now, in your Magento root directory, create a text file named bbb.robots.txt. When bbb.example.com/robots.txt is accessed, Nginx will actually load bbb.robots.txt. In general it will load website-code.robots.txt.

As you see, our approach is very flexible:

  • You specify Magento store codes only once in Nginx configuration
  • No changes to / extra PHP files required – secure and fast!

Also published on Medium.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.