Elxis CMS Forum
Support => Elxis 4.x/5.x DEV => Topic started by: GavinKou on October 16, 2013, 06:07:10
-
how does Elxis work with CDN?
the normal case is the css,js,img components used by some article/category/standalone page, http://www.example.net/sports.html , need to be serviced via CDN.
but the article/category/standalone page self doesn't need.
is there a way to do this?
now Elxis support to generate single domain, in securebase method, looks don't support cdn directly.
so i have to hack the elxisDocument::dispatch method, to replace the url from http://www.example.net/{js,css,img/} to http://{cdn_domain}.example.net/{js,css,img/}
-
Your English are bad and I can't understand all of what you say.
I guess you need a special component for this task that will load all the data remotely from CDN and you dont have to hack the elxisDocument library. Even without that component why you needed to modify elxis document? I dont understand that...
If you want to load single articles you could create a content plugin.
-
Your English are bad and I can't understand all of what you say.
I guess you need a special component for this task that will load all the data remotely from CDN and you dont have to hack the elxisDocument library. Even without that component why you needed to modify elxis document? I dont understand that...
If you want to load single articles you could create a content plugin.
hah, i guess i got your point, and looks so do you ;D ;D
this is what i want
to replace the domain for all urls of img,css,javascript from www.example.com to cdn.example.com on all pages.
the cdn.example.com is served by cdn vender, akamai etc. of course the original source of these img,css,javascript files of cdn.example.com locate on www.example.com.
this here is what i'm doing right now
just changing the elxisDocument::dispatch,
$this->setHeader('Pragma', 'no-cache'); // HTTP 1.0
if ($compress && !ini_get('zlib.output_compression') && ini_get('output_handler')!='ob_gzhandler') {
$html = $this->compress($html);
}
to
$this->setHeader('Pragma', 'no-cache'); // HTTP 1.0
require_once 'CDN.class.php';
$cdn = new CDN() ;
$html = $cdn->parse( $html ) ;
if ($compress && !ini_get('zlib.output_compression') && ini_get('output_handler')!='ob_gzhandler') {
$html = $this->compress($html);
}
the implementation looks ugly but it works well.
beside above hard coding, is there a better way to do this kind of job?
-
I don't know what your CDN class does but there is a very simple way to do that. I guess all you need is to re-write your local links to CDN.
Library: elxisDocument (includes/libraries/elxis/document.class.php)
Find method fetchHead
On line 416 you will find this:
foreach ($this->stylesheets as $href => $arr) { $links[] = $href; }
Add ABOVE it the following:
foreach ($this->stylesheets as $href => $arr) {
if (strpos($href, 'http://www.mysite.com') === 0) {
$newhref = str_replace('http://www.mysite.com', 'http://www.cdn.com/userX/myccss', $href);
unset($this->stylesheets[$href]);
$this->stylesheets[$newhref] = $arr;
}
}
The above will re-write ALL of your local CSS links (on domain example.com) to the ones on your CDN account (on domain cdn.com).
Do the same if you want for the js files. Dont change anything else on Elxis, it will work like a charm.
-
that make sence.
how to take care of the image url? especially the img used in article?
I don't know what your CDN class does but there is a very simple way to do that. I guess all you need is to re-write your local links to CDN.
Library: elxisDocument (includes/libraries/elxis/document.class.php)
Find method fetchHead
On line 416 you will find this:
foreach ($this->stylesheets as $href => $arr) { $links[] = $href; }
Add ABOVE it the following:
foreach ($this->stylesheets as $href => $arr) {
if (strpos($href, 'http://www.mysite.com') === 0) {
$newhref = str_replace('http://www.mysite.com', 'http://www.cdn.com/userX/myccss', $href);
unset($this->stylesheets[$href]);
$this->stylesheets[$newhref] = $arr;
}
}
The above will re-write ALL of your local CSS links (on domain example.com) to the ones on your CDN account (on domain cdn.com).
Do the same if you want for the js files. Dont change anything else on Elxis, it will work like a charm.
-
There are many ways to do that. One way is via url re-write in elxisDocument as I saw you for the css files.
An other option is with a content plugin.
An other, far easier, way is with javascript. In fact with javascript you can re-write anything without touching the elxis code or even the html. The html will be the same with the old links but javascript enabled browsers (almost 99% of your visitors) will get the remote file instead of the local one.
An other, also easy and probably the best, option is again without touching elxis code by using htaccess the re-write for links images, css, and js files. You can for example rewrite a whole local folder of media files (eg. folder "media/") to the remote folder in CDN with just one line in htaccess. Here is an example:
RewriteEngine on
RewriteRule ^media/(.*) http://www.cdn.com/userA/mediafiles/$1
There are really dozens of methods to do that.
-
hum
the rewrite rule method is awesome, if can avoid the extra 301/2 redirection which will be better.
-
now, i'm using the CDN class in elxisDocument::dispatch method to replace
1. src property of img tag
2. href of stylesheet link
3. src of script
from www.example.com to cdn.example.com.
monitoring for few weeks, looks it works well.
thanks so much datahell for your help. ;)
-
Good news. Which CDN provider do you use? We could implement such a system in Elxis core...