Study on the CDN solution for Magento resources and magento resource cdn solution _ PHP Tutorial

Source: Internet
Author: User
Research on the CDN solution for Magento resources and the magento resource cdn solution. Magento resource issues CDN solution research, magento resource cdn solution through Magento understanding, found that Magento resource files are mainly distributed in the media, js, skin three folders, CDN solution research on Magento resources of media files and magento resource cdn solution

By understanding Magento, it is found that Magento resource files are mainly distributed in three folders: media, js, and skin, the media folder mainly includes all the resources involved in the WYSIWYG Editor (Static Blocks, Pages, Product Intro, and Product Images) and Magento self-generated media resources (including folders that allow users to upload files ); the skin folder is mainly the style, image, and js resources provided by the template (we usually change the template to the resources in this folder when it is transformed again ); js folders include Magento official prototype, varien, scriptaculous, mage, and other js libraries and ancillary resources (normally we will not move this folder ), all these three folders contain static resources (images, js, css, fonts, documents, etc.), that is, files that can be stored on CDN.

By observing the Magento source code, the website publication method getBaseUrl AT app/core/Mage/Core/Model/Store. php

public function getBaseUrl($type = self::URL_TYPE_LINK, $secure = null){    $cacheKey = $type . '/' . (is_null($secure) ? 'null' : ($secure ? 'true' : 'false'));    if (!isset($this->_baseUrlCache[$cacheKey])) {        switch ($type) {            case self::URL_TYPE_WEB:                $secure = is_null($secure) ? $this->isCurrentlySecure() : (bool)$secure;                $url = $this->getConfig('web/' . ($secure ? 'secure' : 'unsecure') . '/base_url');                break;            case self::URL_TYPE_LINK:                $secure = (bool) $secure;                $url = $this->getConfig('web/' . ($secure ? 'secure' : 'unsecure') . '/base_link_url');                $url = $this->_updatePathUseRewrites($url);                $url = $this->_updatePathUseStoreView($url);                break;            case self::URL_TYPE_DIRECT_LINK:                $secure = (bool) $secure;                $url = $this->getConfig('web/' . ($secure ? 'secure' : 'unsecure') . '/base_link_url');                $url = $this->_updatePathUseRewrites($url);                break;            case self::URL_TYPE_SKIN:            case self::URL_TYPE_JS:                $secure = is_null($secure) ? $this->isCurrentlySecure() : (bool) $secure;                $url = $this->getConfig('web/' . ($secure ? 'secure' : 'unsecure') . '/base_' . $type . '_url');                break;            case self::URL_TYPE_MEDIA:                $url = $this->_updateMediaPathUseRewrites($secure);                break;            default:                throw Mage::exception('Mage_Core', Mage::helper('core')->__('Invalid base url type'));        }        if (false !== strpos($url, '{{base_url}}')) {            $baseUrl = Mage::getConfig()->substDistroServerVars('{{base_url}}');            $url = str_replace('{{base_url}}', $baseUrl, $url);        }        $this->_baseUrlCache[$cacheKey] = rtrim($url, '/') . '/';    }    return $this->_baseUrlCache[$cacheKey];}

The URL_TYPE_MEDIA acquisition method is more complex. let's take a look at what we have written.

protected function _updateMediaPathUseRewrites($secure = null, $type = self::URL_TYPE_MEDIA){    $secure = is_null($secure) ? $this->isCurrentlySecure() : (bool) $secure;    $secureStringFlag = $secure ? 'secure' : 'unsecure';    $url = $this->getConfig('web/' . $secureStringFlag . '/base_' . $type . '_url');    if (!$this->getConfig(self::XML_PATH_USE_REWRITES)        && Mage::helper('core/file_storage_database')->checkDbUsage()    ) {        $urlStart = $this->getConfig('web/' . $secureStringFlag . '/base_url');        $url = str_replace($urlStart, $urlStart . self::MEDIA_REWRITE_SCRIPT, $url);    }    return $url;}

We can find that all $ type types are always obtained from getConfig (string configPath), and the obtained databases can be configured and modified in the background configuration-> general Web-> Unsecure & Secure.

Assume that Magento is used as the CDN source server, and then the BASE_MEDIA_URL, BASE_SKIN_URL, and BASE_JS_URL values are changed to the CDN address. is CDN processed for Magento resources, so I first installed Magento on the local machine to test it (as the saying goes, practice is the only way to test the truth). because CDN requires domain name resolution, we will not engage in CDN for the time being, however, we can redirect a new domain name to represent this CDN, for example, adding two domain names to hosts for testing.

  127.0.0.1       magento.yourdomain.com  127.0.0.1       mage-cdn.yourdomain.com

In Nginx, copy the original magento.yourdomain.com configuration and delete the php parsing section to ensure security. By the way, add as many resource types as possible to resources (you can add more resources later)

Restart nginx, then log onto the Magento background to BASE_MEDIA_URL, BASE_SKIN_URL, BASE_JS_URL in the original {region} are changed to http://mage-cdn.yourdomain.com/, {base_secure_url} are changed to https://mage-cdn.yourdomain.com/then save settings re-brush Magento cache OK success.

Open the http://magento.yourdomain.com/try, Ah suppose you use another template, with a lot of fonts inside, you may see the error message of cross-origin access:

Font from origin 'https://mage-cdn.yourdomain.com' has been blocked from loading by Cross-Origin Resource Sharing policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://magento.yourdomain.com' is therefore not allowed access.

He told you that the magento site wants to Access some resources of mage-cdn, but because there is no Access-Control-Allow-Origin header, so we add add_header Access-Control-Allow-Origin https://magento.yourdomain.com to the resource file Access in the nginx configuration mage-cdn site. why don't you use it? if you think that other websites can access your resources across domains, you can use it. * I suggest you bind the resources there.

Then, we opened the Magento site for various tests, registration, login, order upload, and various edits in the background. OK. this is really a success.

After adding the CDN, you can log on to the CDN service provider and directly set the source site to your magento.yourdomain.com. generally, CDN provides the CNAME service, then you can easily resolve a cdn.yourdomain.com CNAME to the CDN service provider to provide your address abcd.xxxx.com. Generally, the CDN service provider also provides headers. you can add headers for this CDN, is the cross-origin request we need. Then configure Magento and then blablabla, and finally try to make your crt certificate commercially available for CDN. OK. Now we can fix it.

Thanks to Magento, we found that Magento resource files are mainly distributed in three folders: media, js, and skin, media files...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.