search
HomeBackend DevelopmentPHP TutorialResearch on CDN solutions on Magento resource issues, magento resource cdn solution_PHP tutorial

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>

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>

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

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...
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
What is the difference between unset() and session_destroy()?What is the difference between unset() and session_destroy()?May 04, 2025 am 12:19 AM

Thedifferencebetweenunset()andsession_destroy()isthatunset()clearsspecificsessionvariableswhilekeepingthesessionactive,whereassession_destroy()terminatestheentiresession.1)Useunset()toremovespecificsessionvariableswithoutaffectingthesession'soveralls

What is sticky sessions (session affinity) in the context of load balancing?What is sticky sessions (session affinity) in the context of load balancing?May 04, 2025 am 12:16 AM

Stickysessionsensureuserrequestsareroutedtothesameserverforsessiondataconsistency.1)SessionIdentificationassignsuserstoserversusingcookiesorURLmodifications.2)ConsistentRoutingdirectssubsequentrequeststothesameserver.3)LoadBalancingdistributesnewuser

What are the different session save handlers available in PHP?What are the different session save handlers available in PHP?May 04, 2025 am 12:14 AM

PHPoffersvarioussessionsavehandlers:1)Files:Default,simplebutmaybottleneckonhigh-trafficsites.2)Memcached:High-performance,idealforspeed-criticalapplications.3)Redis:SimilartoMemcached,withaddedpersistence.4)Databases:Offerscontrol,usefulforintegrati

What is a session in PHP, and why are they used?What is a session in PHP, and why are they used?May 04, 2025 am 12:12 AM

Session in PHP is a mechanism for saving user data on the server side to maintain state between multiple requests. Specifically, 1) the session is started by the session_start() function, and data is stored and read through the $_SESSION super global array; 2) the session data is stored in the server's temporary files by default, but can be optimized through database or memory storage; 3) the session can be used to realize user login status tracking and shopping cart management functions; 4) Pay attention to the secure transmission and performance optimization of the session to ensure the security and efficiency of the application.

Explain the lifecycle of a PHP session.Explain the lifecycle of a PHP session.May 04, 2025 am 12:04 AM

PHPsessionsstartwithsession_start(),whichgeneratesauniqueIDandcreatesaserverfile;theypersistacrossrequestsandcanbemanuallyendedwithsession_destroy().1)Sessionsbeginwhensession_start()iscalled,creatingauniqueIDandserverfile.2)Theycontinueasdataisloade

What is the difference between absolute and idle session timeouts?What is the difference between absolute and idle session timeouts?May 03, 2025 am 12:21 AM

Absolute session timeout starts at the time of session creation, while an idle session timeout starts at the time of user's no operation. Absolute session timeout is suitable for scenarios where strict control of the session life cycle is required, such as financial applications; idle session timeout is suitable for applications that want users to keep their session active for a long time, such as social media.

What steps would you take if sessions aren't working on your server?What steps would you take if sessions aren't working on your server?May 03, 2025 am 12:19 AM

The server session failure can be solved through the following steps: 1. Check the server configuration to ensure that the session is set correctly. 2. Verify client cookies, confirm that the browser supports it and send it correctly. 3. Check session storage services, such as Redis, to ensure that they are running normally. 4. Review the application code to ensure the correct session logic. Through these steps, conversation problems can be effectively diagnosed and repaired and user experience can be improved.

What is the significance of the session_start() function?What is the significance of the session_start() function?May 03, 2025 am 12:18 AM

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function