Nginx – Different storeviews or websites in subfolders

Nginx – Different storeviews or websites in subfolders

A client wants his multistore shop to be set up like this:

In this post, I will show you how to do this thing by using Nginx configuration.

Step 1: add the configuration mapping to Nginx’s conf file. /etc/nginx/config.d/mapping_store.conf

map $request_uri $MAGE_RUN_CODE {
  default    en_US;
  ~^/us/     en_US; #en_US is Store View 2's code 
  ~^/de/     de_DE;
  ~^/fr/     fr_FR;
  ~^/it/     it_IT;
}

map $request_uri $MAGE_RUN_TYPE {
  default    store;
  ~^/us/     store;
  ~^/de/     store;
  ~^/fr/     store;
  ~^/it/     store;
}

Step 2: Update the Magento’s nginx configuration

By default, Magento provides sample Nginx configuration in the website’s root directory. It doesn’t allow to run the PHP files which have a sub-folder in the request path by the line

location ~ ^/(index|get|static|errors/report|errors/404|errors/503|health_check)\.php$ {

You have to update this line to

location ~ (index|get|static|errors/report|errors/404|errors/503|health_check)\.php$ {

When you have done previous steps, Nginx can run the PHP script from a subfolder:
http://www.domain1.ch/subfolder/phpfile.php

Step 3: Config the fastcgi_param

Now if you have run the PHP script from a subdirectory on your website. Default Magento can read the fastcgi_param. It’s using for setting the current store view or website at starting the application.

The configuration should looks like this.

    fastcgi_param MAGE_RUN_TYPE $MAGE_RUN_TYPE;
    fastcgi_param MAGE_RUN_CODE $MAGE_RUN_CODE;

By adding two lines into the Nginx configuration, you are setting up the website/store-view for Magento application

# PHP entry point for main application
location ~ (index|get|static|errors/report|errors/404|errors/503|health_check)\.php$ {
    try_files $uri =404;
    fastcgi_pass   fastcgi_backend;
    fastcgi_buffers 1024 4k;

    fastcgi_param PHP_FLAG  "session.auto_start=off \n suhosin.session.cryptua=off";
    fastcgi_param PHP_VALUE "memory_limit=756M \n max_execution_time=18000";
    fastcgi_read_timeout 600s;
    fastcgi_connect_timeout 600s;

    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param DOCUMENT_ROOT    $document_root;
    fastcgi_param SERVER_PORT      $http_x_forwarded_port;

    fastcgi_param MAGE_RUN_TYPE $MAGE_RUN_TYPE;
    fastcgi_param MAGE_RUN_CODE $MAGE_RUN_CODE;

    include        fastcgi_params;
}

Step 4: Setting up the configuration for matching subdirectory

Those previous step to make sure you can run the PHP script includes subdirectory, now, you have to rewrite the index.php from Storeview’s directory, by adding those lines in to the Magento Nginx’s configuration file.

location ~ ^/(?(de|en|it|fr)) {
    try_files $uri $uri/
        /$uri_prefix/index.php?$args;
}

Step 5: Updating Magento Store View Base URL Configuration

My root website folder is /pub/

For Default config

For en

The Final Step: Adding index and subdirectory into the root folder

The subdirectory structure:

All done, Enjoy the new looks. Don’t forget to give me thanks 😀

Please follow and like us:

2 thoughts on “Nginx – Different storeviews or websites in subfolders

    1. This structure able to load difference info, you can edit the content per store view.

Leave a Reply

Your email address will not be published.