为Amazon CloudFront缓存配置WordPress?

时间:2010-08-11 作者:Brent Ozar

Amazon CloudFront 是一个内容分发网络(CDN),可以帮助您在短时间内承受巨大的负载。配置WordPress在S3/CloudFront上托管文件(媒体库、CSS、插件文件、主题)的最简单方法是什么?

(我目前正在使用W3 Total Cache来实现这一点。)

2 个回复
SO网友:User

我认为W3 Total Cache Plugin 可以帮助您。。

SO网友:bueltge

插件运行良好;或者,您可以使用自己的函数将bloginf()替换为您的CDN Url;例子:

替换内容中的url:

// replace content for CDN
if ( !function_exists(\'fb_add_static_content_url\') ) {
    function fb_add_static_content_url($content) {
            if ( is_admin() ) // eigentlich überflüssig
                return $content;

            $wpurl = get_bloginfo(\'wpurl\');

            $search = array(
                $wpurl . \'/wp-content/images/\',
                $wpurl . \'/wp-content/download/\',
            );

            $replace = array(
                \'http://cdn1.bueltge.de/\',
                \'http://cdn2.bueltge.de/\',
            );

            return str_replace( $search, $replace, $content );
    }
    add_filter( \'the_content\', \'fb_add_static_content_url\' );
}
替换stylesheet\\u directoy和其他:

// replace for CDN
if ( !function_exists(\'fb_add_static_wpurl\') ) {
    function fb_add_static_wpurl($info, $show) {

        if ( is_admin() )
            return $info;

        $keys = array(
            \'url\',
            \'wpurl\',
            \'stylesheet_url\',
            \'stylesheet_directory\',
            \'template_url\',
            \'template_directory\',
            );

        if ( in_array( $show, $keys ) ) {

            $wpurl = get_bloginfo(\'wpurl\');

            $search = array(
                $wpurl . \'/wp-content/images/\',
                $wpurl . \'/wp-content/download/\',
                $wpurl . \'/wp-content/themes/\',
                $wpurl . \'/wp-content/plugins/\',
            );

            $replace = array(
                \'http://cdn1.bueltge.de/\',
                \'http://cdn2.bueltge.de/\',
                \'http://cdn3.bueltge.de/\',
                \'http://cdn3.bueltge.de/\',
            );

            return str_replace( $search, $replace, $info );

        } else {
            return $info;
        }
    }
    add_filter( \'bloginfo_url\', \'fb_add_static_wpurl\', 9999, 2 );
}
替换template\\u目录和其他:

function fb_add_static_stylesheet_uri($uri) {

            if ( is_admin() )
                return $uri;

            $wpurl = get_bloginfo(\'wpurl\');

            $search = array(
                $wpurl . \'/wp-content/images/\',
                $wpurl . \'/wp-content/download/\',
                $wpurl . \'/wp-content/themes/\',
                $wpurl . \'/wp-content/plugins/\',
            );

            $replace = array(
                \'http://cdn1.bueltge.de/\',
                \'http://cdn2.bueltge.de/\',
                \'http://cdn3.bueltge.de/\',
                \'http://cdn3.bueltge.de/\',
            );
            return str_replace( $search, $replace, $uri );

}
add_filter ( \'template_directory_uri\', \'fb_add_static_stylesheet_uri\' );
add_filter ( \'stylesheet_uri\', \'fb_add_static_stylesheet_uri\' );
add_filter ( \'stylesheet_directory_uri\', \'fb_add_static_stylesheet_uri\' );

结束

相关推荐