0

I'm looking for clear solution without spikes, without creation any subfolders in pub with index files, without hardcoding cookies etc.

In other words I'd like to achive multistore in magento only using nginx configuration and magento stores/configuration.

I mostly achieved this, however I'm using two stubs, which I want to remove.

I've created some stores in admin area. Then I configured for all of them base_url in stores/configuration (also in admin area).

Let's have a look at nginx configuration. I've put into it one stub:

I added

map $http_host $MAGE_RUN_CODE {
   sub1.domain.com "sub1_magento";
   sub2.domain.com "sub2_magento";
   sub3.domain.com "sub3_magento";
}

Then inside section for php files I've added this:

    fastcgi_param  MAGE_RUN_TYPE website;
    fastcgi_param  MAGE_RUN_CODE $MAGE_RUN_CODE;

In $_SERVER variable MAGE_RUN_TYPE has expected value - 'website', however variable $MAGE_RUN_CODE doesn't have correct value.

For now I removed 'map' section and hardcoded value for it. If somebody knows how to solve this nginx issue - please tell me how.

Let's move to the main issue:

As I understand the main actions with store detection are happening inside

Magento\Store\Model\StoreResolver

If you put breakpoint inside constructor you may notice that $runMode and $scopeCode have expected values (from nginx config, as described above MAGE_RUN_CODE for me is hardcoded).

Then let's have a look at function:

/**
 * {@inheritdoc}
 */
public function getCurrentStoreId()
{
    list($stores, $defaultStoreId) = $this->getStoresData();

    $storeCode = $this->request->getParam(self::PARAM_NAME, $this->storeCookieManager->getStoreCodeFromCookie());

    if (is_array($storeCode)) {
        if (!isset($storeCode['_data']['code'])) {
            throw new \InvalidArgumentException(__('Invalid store parameter.'));
        }
        $storeCode = $storeCode['_data']['code'];
    }
    if ($storeCode) {
        try {
            $store = $this->getRequestedStoreByCode($storeCode);
        } catch (NoSuchEntityException $e) {
            $store = $this->getDefaultStoreById($defaultStoreId);
        }

        if (!in_array($store->getId(), $stores)) {
            $store = $this->getDefaultStoreById($defaultStoreId);
        }
    } else {
        $store = $this->getDefaultStoreById($defaultStoreId);
    }
    return $store->getId();
}

Store code is taking from one of two places: request or cookie.

In my opinion in case with request is when we type url like this: http://sub1.domain.com/?__store=blablabla. So it's not our case.

The second case (cookie) works only with store selector. (also not our case)

I hardcoded there necessary store name and I received expected result.

Did I miss anything? Is there any third case? How should magento detect which store/website to use?

1 Answer 1

0

I solved this issue. I had some troubles with MAGE_RUN_TYPE website; because of detection default website in one function. I will not go deep inside the details, here is working solution:

The difference between current variant and posted in first post are quotes. They shouldn't present.

map $http_host $MAGE_RUN_CODE {
   sub1.domain.com sub1_magento;
   sub2.domain.com sub2_magento;
   sub3.domain.com sub3_magento;
}

Where sub1_magento, sub2_magento, sub3_magento are store codes!

Let's move below in the config:

# PHP entry point for main application
location ~ (index|get|static|report|404|503)\.php$ {

    ...

    fastcgi_param  MAGE_RUN_TYPE store;
    fastcgi_param  MAGE_RUN_CODE $MAGE_RUN_CODE;

Another issue, described in first post was 3rd party theme issue. So out of the box it must work!

Don't forget to have a look at official tutorials, they should help to solve all other issues:

http://devdocs.magento.com/guides/v2.0/config-guide/multi-site/ms_over.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.