korbax
7/25/2016 - 8:31 AM

Configure nginx for use multistore in Magent0

Configure nginx for use multistore in Magent0

map $http_host $mage_run_code {
    .site.com base;
    .second.com second_store;
}

server {
    server_name site.com;
    return 301 $scheme://www.site.com$request_uri;
}

server {
    server_name second.com;
    return 301 $scheme://www.second.com$request_uri;
}

server {
    listen	80 default;
    server_name www.site.com www.second.com;
    root /var/www/site.com;
    index index.php;
    autoindex off;

    client_max_body_size 15m;
    client_body_buffer_size 128k;
    
    expires 86400s;
    add_header Pragma public;
    add_header Cache-Control "max-age=86400, public, must-revalidate, proxy-revalidate";

    location ~ (^/(app/\|includes/\|lib/\|/pkginfo/\|var/\|report/config.xml)\|/\.svn/\|/\.git/\|/.hta.+) {
        deny all; #ensure sensitive files are not accessible
    }

    # Don't log robots.txt requests
    location = /robots.txt {
    
        if ($host = 'www.site.com') {
            rewrite ^/robots\.txt /seo/en/site.com/robots.txt last;
        }
        
        if ($host = 'www.second.com') {
            rewrite ^/robots\.txt /seo/en/second.com/robots.txt last;
        }
    
        allow all;
        log_not_found off;
        access_log off;
    }

    # Rewrite for versioned CSS+JS via filemtime
    location ~* ^.+\.(css|js)$ {
        rewrite ^(.+)\.(\d+)\.(css|js)$ $1.$3 last;
        expires 31536000s;
        access_log off;
        log_not_found off;
        add_header Pragma public;
        add_header Cache-Control "max-age=31536000, public";
    }

    # Aggressive caching for static files
    # If you alter static files often, please use 
    # add_header Cache-Control "max-age=31536000, public, must-revalidate, proxy-revalidate";
    location ~* \.(asf|asx|wax|wmv|wmx|avi|bmp|class|divx|doc|docx|eot|exe|gif|gz|gzip|ico|jpg|jpeg|jpe|mdb|mid|midi|mov|qt|mp3|m4a|mp4|m4v|mpeg|mpg|mpe|mpp|odb|odc|odf|odg|odp|ods|odt|ogg|ogv|otf|pdf|png|pot|pps|ppt|pptx|ra|ram|svg|svgz|swf|tar|t?gz|tif|tiff|ttf|wav|webm|wma|woff|wri|xla|xls|xlsx|xlt|xlw|zip)$ {
        expires 31536000s;
        access_log off;
        log_not_found off;
        add_header Pragma public;
        add_header Cache-Control "max-age=31536000, public";
    }

    location / {
        # Кешировать указанные коды ответов 5 минут
        proxy_cache_valid 200 301 302 304 5m;
        # Ключ по которому сохраняются и берутся данные из кеша
        proxy_cache_key "$request_method|$http_if_modified_since|$http_if_none_match|$host|$request_uri";
        # Защита от раздачи одинаковой куки в кешированном ответе
        proxy_hide_header "Set-Cookie";
        # Игнорировать параметры кеша заданные бекэндом
        proxy_ignore_headers "Cache-Control" "Expires";
        
        # Указывает в каких случаях клиенту можно отдать несвежий ответ из кеша
        proxy_cache_use_stale error timeout invalid_header http_500 http_502 http_503 http_504;
    
        # активировать зону кеширования pagecache
        proxy_cache	pagecache;
        
        try_files $uri $uri/ /index.php?$args; 		# make index.php handle requests for /
        access_log off; 							            # do not log access to static files
        expires max; 								              # cache static files aggressively
    }

    location @appserver {
        proxy_pass 
        http://127.0.0.1:8080;
    }

    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; }

    location /var/export/ { ## Allow admins only to view export folder
        auth_basic           "Restricted"; ## Message shown in login window
        auth_basic_user_file htpasswd; ## See /etc/nginx/htpassword
        autoindex            on;
    }
    
    location /\. {
        return 404;
    }

    location @proxy {
        fastcgi_pass phpsock;
    }

    location ~ .php$ {
        include /etc/nginx/conf.d/headers.conf;
        include /etc/nginx/fastcgi_params;
        fastcgi_pass phpsock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_send_timeout 180;
        fastcgi_read_timeout 180;
        fastcgi_connect_timeout 180;
        fastcgi_cache fcgi;
        fastcgi_cache_valid 200 60m;
        
        fastcgi_param MAGE_RUN_TYPE website;
        fastcgi_param MAGE_RUN_CODE $mage_run_code;
    }


    location @cache_miss { 
        # are we using a reverse proxy? proxy_set_header X-Real-IP $remote_addr; 
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
        proxy_set_header Host $http_host; proxy_redirect off; 
        proxy_max_temp_file_size 0; 
        
        #configure fastcgi 
        fastcgi_pass 127.0.0.1:9000; 
        fastcgi_send_timeout 5m; 
        fastcgi_read_timeout 5m; 
        fastcgi_connect_timeout 5m; 
        fastcgi_buffer_size 256k; 
        fastcgi_buffers 4 512k; 
        fastcgi_busy_buffers_size 768k; 
        fastcgi_param GEOIP_COUNTRY_CODE $geoip_country_code; 
        fastcgi_param GEOIP_COUNTRY_NAME $geoip_country_name; 
        fastcgi_param PHP_VALUE "memory_limit = 32M"; 
        fastcgi_param PHP_VALUE "max_execution_time = 18000"; 
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
        include fastcgi_params; 
    }
}
<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category   Mage
 * @package    Mage
 * @copyright  Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
 * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */

if (version_compare(phpversion(), '5.2.0', '<')===true) {
    echo  '<div style="font:12px/1.35em arial, helvetica, sans-serif;">
<div style="margin:0 0 25px 0; border-bottom:1px solid #ccc;">
<h3 style="margin:0; font-size:1.7em; font-weight:normal; text-transform:none; text-align:left; color:#2f2f2f;">
Whoops, it looks like you have an invalid PHP version.</h3></div><p>Magento supports PHP 5.2.0 or newer.
<a href="http://www.magentocommerce.com/install" target="">Find out</a> how to install</a>
 Magento using PHP-CGI as a work-around.</p></div>';
    exit;
}

/**
 * Error reporting
 */
error_reporting(E_ALL | E_STRICT);

/**
 * Compilation includes configuration file
 */
define('MAGENTO_ROOT', getcwd());

$compilerConfig = MAGENTO_ROOT . '/includes/config.php';
if (file_exists($compilerConfig)) {
    include $compilerConfig;
}

$mageFilename = MAGENTO_ROOT . '/app/Mage.php';
$maintenanceFile = 'maintenance.flag';

if (!file_exists($mageFilename)) {
    if (is_dir('downloader')) {
        header("Location: downloader");
    } else {
        echo $mageFilename." was not found";
    }
    exit;
}

if (file_exists($maintenanceFile)) {
    include_once dirname(__FILE__) . '/errors/503.php';
    exit;
}

require_once $mageFilename;

#Varien_Profiler::enable();

if (isset($_SERVER['MAGE_IS_DEVELOPER_MODE'])) {
    Mage::setIsDeveloperMode(true);
}

ini_set('display_errors', 0);

umask(0);

/* Store or website code */
$mageRunCode = isset($_SERVER['MAGE_RUN_CODE']) ? $_SERVER['MAGE_RUN_CODE'] : '';

/* Run store or run website */
$mageRunType = isset($_SERVER['MAGE_RUN_TYPE']) ? $_SERVER['MAGE_RUN_TYPE'] : 'store';

switch($_SERVER['HTTP_HOST']){
	case 'site.com':
	case 'www.site.com':
		Mage::run('base', 'website');
	break;
	case 'second.com':
	case 'www.second.com':
		Mage::run('second_store', 'website');
	break;
	default:
        Mage::run($mageRunCode, $mageRunType);
    break;
}

//Mage::run($mageRunCode, $mageRunType);