Research on CDN solutions on Magento resource issues, magento resource cdn solution_PHP tutorial

WBOY
Release: 2016-07-12 08:58:31
Original
834 people have browsed it

Research on CDN solutions for Magento resource issues, magento resource cdn solutions

Through understanding of Magento, I found that Magento’s resource files are mainly distributed in three folders: media, js, and skin. , the media folder mainly includes the system’s built-in editor WYSIWYG Editor, all the resources involved in the editor (Static Blocks, Pages, Product Intro, Product Images) and the media resources independently generated by Magento (including the media resources that we allow users to upload files folder); the skin folder is mainly the styles, pictures, and js resources provided by the template (generally, when we re-modify the template, we will change the resources in this folder); the js folder includes Magento official prototype, varien, scriptaculous, mage and other js libraries and ancillary resources (generally we will not touch this folder). All these three folders contain static resources (pictures, js, css, fonts, documents, etc.), which means we can CDN File

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

<span>public</span> <span>function</span> getBaseUrl(<span>$type</span> = self::URL_TYPE_LINK, <span>$secure</span> = <span>null</span><span>)
{
    </span><span>$cacheKey</span> = <span>$type</span> . '/' . (<span>is_null</span>(<span>$secure</span>) ? 'null' : (<span>$secure</span> ? 'true' : 'false'<span>));
    </span><span>if</span> (!<span>isset</span>(<span>$this</span>->_baseUrlCache[<span>$cacheKey</span><span>])) {
        </span><span>switch</span> (<span>$type</span><span>) {
            </span><span>case</span> self::URL_TYPE_WEB:
                <span>$secure</span> = <span>is_null</span>(<span>$secure</span>) ? <span>$this</span>->isCurrentlySecure() : (bool)<span>$secure</span><span>;
                </span><span>$url</span> = <span>$this</span>->getConfig('web/' . (<span>$secure</span> ? 'secure' : 'unsecure') . '/base_url'<span>);
                </span><span>break</span><span>;

            </span><span>case</span> self::URL_TYPE_LINK:
                <span>$secure</span> = (bool) <span>$secure</span><span>;
                </span><span>$url</span> = <span>$this</span>->getConfig('web/' . (<span>$secure</span> ? 'secure' : 'unsecure') . '/base_link_url'<span>);
                </span><span>$url</span> = <span>$this</span>->_updatePathUseRewrites(<span>$url</span><span>);
                </span><span>$url</span> = <span>$this</span>->_updatePathUseStoreView(<span>$url</span><span>);
                </span><span>break</span><span>;

            </span><span>case</span> self::URL_TYPE_DIRECT_LINK:
                <span>$secure</span> = (bool) <span>$secure</span><span>;
                </span><span>$url</span> = <span>$this</span>->getConfig('web/' . (<span>$secure</span> ? 'secure' : 'unsecure') . '/base_link_url'<span>);
                </span><span>$url</span> = <span>$this</span>->_updatePathUseRewrites(<span>$url</span><span>);
                </span><span>break</span><span>;

            </span><span>case</span> self::URL_TYPE_SKIN:
            <span>case</span> self::URL_TYPE_JS:
                <span>$secure</span> = <span>is_null</span>(<span>$secure</span>) ? <span>$this</span>->isCurrentlySecure() : (bool) <span>$secure</span><span>;
                </span><span>$url</span> = <span>$this</span>->getConfig('web/' . (<span>$secure</span> ? 'secure' : 'unsecure') . '/base_' . <span>$type</span> . '_url'<span>);
                </span><span>break</span><span>;

            </span><span>case</span> self::URL_TYPE_MEDIA:
                <span>$url</span> = <span>$this</span>->_updateMediaPathUseRewrites(<span>$secure</span><span>);
                </span><span>break</span><span>;

            </span><span>default</span>:
                <span>throw</span> Mage::<span>exception</span>('Mage_Core', Mage::helper('core')->__('Invalid base url type'<span>));
        }

        </span><span>if</span> (<span>false</span> !== <span>strpos</span>(<span>$url</span>, '{{base_url}}'<span>)) {
            </span><span>$baseUrl</span> = Mage::getConfig()->substDistroServerVars('{{base_url}}'<span>);
            </span><span>$url</span> = <span>str_replace</span>('{{base_url}}', <span>$baseUrl</span>, <span>$url</span><span>);
        }

        </span><span>$this</span>->_baseUrlCache[<span>$cacheKey</span>] = <span>rtrim</span>(<span>$url</span>, '/') . '/'<span>;
    }

    </span><span>return</span> <span>$this</span>->_baseUrlCache[<span>$cacheKey</span><span>];
}</span>
Copy after login

The method of obtaining URL_TYPE_MEDIA is more complicated. Let’s also take a look at what is written

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

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

Assume that the entire Magento site is used as a CDN source server, and then the values ​​​​of BASE_MEDIA_URL, BASE_SKIN_URL, and BASE_JS_URL are changed to CDN addresses. Does this mean that the Magento resources are CDN processed? So I first changed the local machine Install Magento for testing (as the saying goes, practice is the only way to test truth). Since CDN requires domain name resolution, we will ignore it for the time being and will not engage in CDN. However, we can use redirection to a new domain name to represent this CDN. For example, add two domain names to hosts for testing

  <span>127.0</span>.<span>0.1</span><span>       magento.yourdomain.com
  </span><span>127.0</span>.<span>0.1</span>       mage-cdn.yourdomain.com
Copy after login

Copy the original magento.yourdomain.com configuration in Nginx and delete the PHP parsing section to ensure safety. By the way, add as many resource types as possible to the resources (you can add more later if not enough)

Restart nginx, then log in to the Magento backend and change the original {{base_unsecure_url}} in BASE_MEDIA_URL, BASE_SKIN_URL, and BASE_JS_URL to http://mage-cdn.yourdomain.com/ and {{base_secure_url}} to https: //mage-cdn.yourdomain.com/ Then save the settings and refresh the Magento cache. OK. You're done.

Open http://magento.yourdomain.com/ and try it. Suppose you use another template and add a lot of fonts to it. You may see an error message about cross-domain access:

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

He told you that the magento site wants to access some resources of mage-cdn, but since there is no Access-Control-Allow-Origin header, we add add_header Access-Control when configuring the resource files in the mage-cdn site in nginx. -Allow-Origin https://magento.yourdomain.com. Why not use *? If you generously want other websites to be able to access your resources across domains, then you can use *. I recommend using binding there. Where, hehe

Then we open the Magento site, conduct various tests, register, log in, place an order, upload and edit various things in the background. OK, this time we are really done.

The next step is to deploy the CDN. Log in to the CDN service provider and directly set the source site to your magento.yourdomain.com. Generally, CDNs provide CNAME services, so you can generously parse a cdn.yourdomain.com CNAME to provide CDN services. The address abcd.xxxx.com provided to you by the provider. Generally, CDN service providers also provide the addition of headers. You can add the required headers for this CDN, which is the cross-domain request we need. Then configure Magento and then blablabla. Finally, you find a way to let the CDN service provider use your crt certificate. OK It’s done now

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1102846.htmlTechArticleResearch on CDN solution for Magento resource issues, magento resource cdn solution Through understanding of Magento, I found that Magento’s resource files are mainly Distributed in three folders: media, js, skin, media files...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!