fbpx

Magento / NGINX

Magento 1.x multi store NGINX configuration

by , , revisited on


We have by far the largest RPM repository with NGINX module packages and VMODs for Varnish. If you want to install NGINX, Varnish, and lots of useful performance/security software with smooth yum upgrades for production use, this is the repository for you.
Active subscription is required.

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!

Subdirectories setup

In rare cases, a subdirectory layout is used. This only requires setting up a mapping like so:

map $request_uri $MAGE_RUN_CODE {
  default    '';
  ~^/ru-ru/  ru_website;
}

map $request_uri $MAGE_RUN_TYPE {
  default    store;
  ~^/ru-ru/  website;
}

In the above example any URIs starting with /ru-ru/ will use the Magento website with code ru_website, otherwise the default.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

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