Magento – Magento Multiple stores with nginx

magento-1.9nginx

I am following tutorials about magento ,it use nginx as server ,I created 2 different stores and edit the file /etc/nginx/sites-available/magento as follows :

map $http_host $mage_run_code {
    default '';
    gizmo.com gizmo_com;
    widget.com widget_com;
}
map $http_host $mage_run_type {
    default store;
    gizmo.com webisite;
    widget.com website;
}
server {
    listen 80;

    root /var/www/magento;

    index index.php index.html index.htm;

    server_name magento.local gizmo.com www.gizmo.com widget.com www.widget.com;

    # Place PHP error logs in the Magento log folder
    set $php_log /var/www/magento/var/log/php_errors.log;

    # Replaces Apache rewrite rules
    location / {
        try_files $uri $uri/ @handler;
    }

    # Protect sensitive folders
    location /app/                { deny all; }
    location /includes/           { deny all; }
    location /lib/                { deny all; }
    location /media/downloadable/ { deny all; }
    location /pkginfo/            { deny all; }
    location /report/config.xml   { deny all; }
    location /var/                { deny all; }

    # Protect dotfiles (htaccess, svn, etc.)
    location /. { return 404; }

    location @handler {
        rewrite / /index.php;
    }

    # Remove trailing slashes from PHP files
    location ~ .php/ {
        rewrite ^(.*.php)/ $1 last;
    }

    # Pass PHP to a the PHP-FPM backend
    location ~ \.php$ {
        # Fix timeouts when installing Magento via web interface
        fastcgi_send_timeout 1800;
        fastcgi_read_timeout 1800;
        fastcgi_connect_timeout 1800;

        fastcgi_pass 127.0.0.1:9000;
        #fastcgi_param HTTPS $fastcgi_https;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        #fastcgi_param MAGE_IS_DEVELOPER_MODE on; # Turn on developer mode
        fastcgi_param MAGE_RUN_CODE $mage_run_code;
        fastcgi_param MAGE_RUN_TYPE $mage_run_type;
        fastcgi_param PHP_VALUE error_log=$php_log;
        include fastcgi_params;
    }
}

The strange thing is ,that when I open widget.com it is not giving me error,but redirect me to magento.local/ ,actually the url stays widget.com ,but I tried to change the html of widget.com through the admin site of magento and it doesn't change so I think it loads the default magento index page.When I try to open gizmo.com it shows me 404 error.I restarted nginx and did everything as in the tutorial ,but still same thing.

Best Answer

This 'map' config is far from ideal. I've seen a few examples use the 'map' technique but it is really short sighted and full of general issues.

Best way to get mutli store running, start with one store, etc/nginx/sites-available/widget.com.conf and use the very basic nginx examples to get it running and in your the location ~ .php$ { location to point to the store name and check that it's still running:

fastcgi_param MAGE_RUN_CODE widgets;
fastcgi_param MAGE_RUN_TYPE store;

When this is all working perfectly ? You can copy&past this config to /etc/nginx/sites-available/gizmo.com.conf

then change your store code to the other store in this config:

fastcgi_param MAGE_RUN_CODE gizmo;
fastcgi_param MAGE_RUN_TYPE store;

This will allow you to have a .config for each of your main stores. All based on a single proven store. With each having their own abilities, such as SSL/TLS and other really handy things for a webstore, like SiteMap locations etc. etc.)

Using this working example, you can add as many stores as you want by copying the working config and changing the store code in the php location block, each having their own .config file and each having their own settings where needed.

Here's a copy of my Nginx config to get you started:

Widgets.com.conf

server {
        listen 443 ssl;
        listen 80;
        server_name www.widgets.com widgets.com;
        keepalive_timeout       70;
        root /home/mysite/public_html/;
        index index.php;
        try_files $uri $uri/ @handler;
        client_max_body_size 150M;
        client_body_buffer_size 80M;
        client_header_timeout 360;
        client_body_timeout 360;
        server_tokens off;
        send_timeout 360;
        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AES:RSA+3DES:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!AESGCM;
        ssl_prefer_server_ciphers on;
        ssl_session_cache shared:SSL:10m;
        ssl_session_timeout 5m;
        ssl_certificate /home/ssl/widgets/combied.crt;
        ssl_certificate_key /home/ssl/widgets/widgets.com.key;
        add_header 'Strict-Transport-Security' 'max-age=31536000; preload;';
        add_header 'Access-Control-Allow-Origin' '*';

       location @ourcleanurls {
                rewrite ^/(.*) /catalogsearch/advanced/result/?name=$1 last;
        }

    location ^~ /report/config.xml   { deny all; }
    location ^~ /var/                { deny all; }
    location ^~ /admin/                { deny all; }
    location ^~ /shell/                { deny all; }
    location ^~ /app/                { deny all; }
    location ^~ /includes/           { deny all; }
    location ^~ /lib/                { deny all; }
    location ^~ /media/downloadable/ { deny all; }
    location ^~ /pkginfo/            { deny all; }
    location @handler {
    rewrite / /index.php;
    }
      location ~ \.php$ {
        expires off; ## Do not cache dynamic content
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param  MAGE_RUN_CODE widgets;
        fastcgi_param  MAGE_RUN_TYPE store;
        fastcgi_read_timeout 9000;
        fastcgi_send_timeout 9000;
        include fastcgi_params;
        proxy_read_timeout 9000;
        proxy_connect_timeout 9000;
        send_timeout 9000;
        fastcgi_pass widgets-socket;
        add_header 'Strict-Transport-Security' 'max-age=31536000; preload;';
        add_header 'Alternate-Protocol'  '443:npn-spdy/3';
        }

gizmo.com.conf

server {
        listen 443 ssl;
        listen 80;
        server_name www.gizmo.com gizmo.com;
        keepalive_timeout       70;
        root /home/mysite/public_html/;
        index index.php;
        try_files $uri $uri/ @handler;
        client_max_body_size 150M;
        client_body_buffer_size 80M;
        client_header_timeout 360;
        client_body_timeout 360;
        server_tokens off;
        send_timeout 360;
        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AES:RSA+3DES:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!AESGCM;
        ssl_prefer_server_ciphers on;
        ssl_session_cache shared:SSL:10m;
        ssl_session_timeout 5m;
        ssl_certificate /home/ssl/gizmo/combied.crt;
        ssl_certificate_key /home/ssl/gizmo/gizmo.com.key;
        add_header 'Strict-Transport-Security' 'max-age=31536000; preload;';
        add_header 'Access-Control-Allow-Origin' '*';

       location @ourcleanurls {
                rewrite ^/(.*) /catalogsearch/advanced/result/?name=$1 last;
        }

    location ^~ /report/config.xml   { deny all; }
    location ^~ /var/                { deny all; }
    location ^~ /admin/                { deny all; }
    location ^~ /shell/                { deny all; }
    location ^~ /app/                { deny all; }
    location ^~ /includes/           { deny all; }
    location ^~ /lib/                { deny all; }
    location ^~ /media/downloadable/ { deny all; }
    location ^~ /pkginfo/            { deny all; }
    location @handler {
    rewrite / /index.php;
    }
      location ~ \.php$ {
        expires off; ## Do not cache dynamic content
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param  MAGE_RUN_CODE gizmo;
        fastcgi_param  MAGE_RUN_TYPE store;
        fastcgi_read_timeout 9000;
        fastcgi_send_timeout 9000;
        include fastcgi_params;
        proxy_read_timeout 9000;
        proxy_connect_timeout 9000;
        send_timeout 9000;
        fastcgi_pass gizmo-socket;
        add_header 'Strict-Transport-Security' 'max-age=31536000; preload;';
        add_header 'Alternate-Protocol'  '443:npn-spdy/3';
        }

You can see, by copying the config to a new file you now have full control over things like SSL/TLS and many other things you really should have for a eCommerce store.

In the above configs i have choose to block all access attempts to /admin , if you are copying this exactly without reading it all first( you shouldn't ), you must make sure you have chosen a custom admin path, or comment out that line.