Coeus Blue Managed Web Hosting

866.847.8171

Nginx for Magento Multisite

I have been receiving some questions lately about multi site configuration when using nginx.  Normally there are a pair of variables set using apache's SetEnv in the .htaccess file not supported or used by nginx.  This method works for both Magento Community and Magento Enterprise.

The configuration overview found at MagentoCommerce is a reasonable start for single site configuration but punts on the multi site configuration question.  They point out:

The “MAGE_RUN_CODE” and “MAGE_RUN_TYPE” are for multi-store installations, each DOMAIN that represents a store should have that store code instead of “default” (line #53).

but not what needs to be done with it.  You could duplicate your configuration for each store code, but the easier way to deal with the store code is to build a map on it and switch on that map in the configuration.

 

 map $http_host $magesite { 
  www.site1.com site1storecode.com;
  www.site2.com site2storecode;
}
 server {
   <snip>
    location / {
                       
     if ($request_uri ~* "\.(ico|css|js|gif|jpe?g|png)$") {
      access_log   off;
      expires max;
     }

     try_files $uri $uri/ @magento;
     fastcgi_param  MAGE_RUN_TYPE  store;
     fastcgi_param MAGE_RUN_CODE $magesite;
     include fastcgi_params;
     fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
     fastcgi_param HTTPS $fastcgi_https;
     fastcgi_pass fastcgiUpstreamProvider;
}
   location @magento {

    # set expire headers
    if ($request_uri ~* "\.(ico|css|js|gif|jpe?g|png)$") {
     access_log   off;
     expires max;
    }

    include fastcgi_params;
    fastcgi_param MAGE_RUN_TYPE  store;
    fastcgi_param MAGE_RUN_CODE $magesite;
    fastcgi_param SCRIPT_FILENAME $document_root/index.php;
    fastcgi_param SCRIPT_NAME /index.php;
    fastcgi_param HTTPS $fastcgi_https;
    fastcgi_pass fastcgiUpstreamProvider;
   }
}

Another common question is how to avoid duplication of configuration for secure and insecure site sections.  In the above configuration the sections in the location blocks which specify fastcgi_param HTTPS $fastcgi_https; let fastcgi know if secure is on or not. Using those and switching on the scheme with:

       map $scheme $fastcgi_https {
            default off;
            https on;
        }

You can combine the http and https sections.  This change does require a recent version of nginx, at least newer than the version in the centOS base repo as I am writing this.   In the server section just modify your listen lines to be:

                listen       80;
                listen  443  default  ssl; 

and add the normal ssl_ configuration to the server block.  This saves you from having to duplicate much of your configuration. This technique can grant you more control of other things as well.  For many of our clients we switch the fastcgi_pass paramater to let us split out admin functions and front end functions.  You can then push the timeouts and resource limits for admin to the levels they need to be for large jobs without allowing the front end site to over tax your servers.