Home >php教程 >php手册 >Magento资源问题上CDN方案研究,magento资源cdn方案

Magento资源问题上CDN方案研究,magento资源cdn方案

WBOY
WBOYOriginal
2016-06-13 08:45:481303browse

Magento资源问题上CDN方案研究,magento资源cdn方案

  通过对Magento的了解,发现Magento的资源文件主要分布在media、js、skin三个文件夹里,media文件夹主要包括了系统自带编辑器WYSIWYG Editor 所有编辑器涉及到的资源(Static Blocks、Pages、Product Intro、Product Images)以及Magento自主生成的媒体资源  (包含了我们允许用户上传文件的文件夹);skin文件夹主要是模板提供的样式、图片、js资源(一般我们对模板再次改造时会改到这个文件夹里的资源);js文件夹包括Magento官方的prototype、varien、scriptaculous、mage等js库以及附属资源(一般我们不会去动这个文件夹),这三个文件夹所包含的全部都是静态资源(图片、js、css、字体、文档等) 也就是我们可以CDN的文件

  通过对Magento源码的观察,网站公开方法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>

 

URL_TYPE_MEDIA的获取方式更复杂些 我们也看看写了什么

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

 

我们可以发现所有$type 类型的 始终会从 getConfig(string configPath)  中获得,所获得保存数据库 在后台 configuration -> general Web -> Unsecure & Secure 里可以配置修改。

假设把Magento整站当做CDN源服务器,再把BASE_MEDIA_URL、BASE_SKIN_URL、BASE_JS_URL的值改成CDN的地址,是不是就对Magento的资源进行了CDN处理了呢,于是我就先把本地的机器装上了Magento来测试一番(俗话说实践是检验真理的唯一方法),由于CDN需要域名解析,我们暂时不理会也不去搞CDN,但是我们可以用重定向一个新域名来代表这个CDN,比如 hosts里加两个域名用来测试

  <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

Nginx里把原来magento.yourdomain.com的配置复制一份并删除php解析那一段保证安全。顺便在资源那加入尽可能多的资源类型(以后不够还可以再加)

重启nginx,然后登陆Magento后台 把 BASE_MEDIA_URL、BASE_SKIN_URL、BASE_JS_URL 中原来的 {{base_unsecure_url}} 都改成 http://mage-cdn.yourdomain.com/ 、{{base_secure_url}} 都改成 https://mage-cdn.yourdomain.com/ 然后保存设置  重新刷下Magento缓存 OK 大功告成。

打开http://magento.yourdomain.com/ 试试,诶 假设你用了别的模板,里面加了很多字体,你可能会看到有跨域访问的错误提示:

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.

他告诉你magento的站点想访问mage-cdn的一些资源但是由于没有Access-Control-Allow-Origin 头,所以我们在nginx配置mage-cdn站点里资源文件访问时统一加上 add_header Access-Control-Allow-Origin https://magento.yourdomain.com. 为什么不用*呢,假如你很大方的想别的网站也能跨域访问你的资源那你就使用*吧,我是建议那里用的绑哪里,呵呵

然后我们在打开Magento站点 进行各种测试 注册 登陆 下单 上传 后台各种编辑 OK 这次我们真的大功告成了。

加下来就是部署CDN了,登陆CDN服务商,直接将源站点设置为你的 magento.yourdomain.com  一般CDN都提供CNAME服务,那么你大方的解析一个cdn.yourdomain.com CNAME给 CDN服务商提供给你的地址 abcd.xxxx.com ,一般CDN服务商也同样提供headers 的添加 你可以为这个CDN添加需要headers,就是我们需要的跨域请求。再就是配置Magento 然后blablabla 最后你想办法让CDN服务商用你的crt证书。OK 这下可以搞定了

Statement:
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