phpBB has many static resources and serving them from a different server than the dynamic forum's server could mean a notable performance increase. We can set expires headers far into the future and possibly utilize a CDN in the future.
From checks I've done so far it seems that changing functions.php to indicate a different base directory might work. Code
$web_path = '//some.new.domain/path'/*(defined('PHPBB_USE_BOARD_URL_PATH') && PHPBB_USE_BOARD_URL_PATH) ? $board_url : $phpbb_root_path*/;
However, it's unclear to me if this may have other implications such as making the uploads directory inaccessible as paths seem local to the server. Also, it doesn't resolve the generation of dynamic CSS using styles.php
Is there a quick way to indicate changes to static resources without breaking phpBB code?
2 Answers 2
Considering the date of your question, I presume you are already using phpBB 3.1, in which case you can write an extension that does what you want, leaves phpBB code intact and will not interfere with forum upgrades. If you're not, you'll be stuck with code modification or other means of solving your issue.
For 3.1, you will need to write a plugin that plugs in to the event 'core.page_header_after' , which will be executed at the end of the page_header() function. This allows you to overwrite all template variables created in the header, and add new ones if you wish to use others in your templates.
In your case, you'll need to look to assign these variables.
'T_ASSETS_PATH' => "{$forum_static_url}assets",
'T_THEME_PATH' => "{$forum_static_url}styles/" . rawurlencode($this->user->style['style_path']) . '/theme',
'T_TEMPLATE_PATH' => "{$forum_static_url}styles/" . rawurlencode($this->user->style['style_path']) . '/template',
'T_SUPER_TEMPLATE_PATH' => "{$forum_static_url}styles/" . rawurlencode($this->user->style['style_path']) . '/template',
'T_IMAGES_PATH' => "{$forum_static_url}images/",
'T_SMILIES_PATH' => "{$forum_static_url}{$this->config['smilies_path']}/",
'T_AVATAR_PATH' => "{$forum_static_url}{$this->config['avatar_path']}/",
'T_AVATAR_GALLERY_PATH' => "{$forum_static_url}{$this->config['avatar_gallery_path']}/",
'T_ICONS_PATH' => "{$forum_static_url}{$this->config['icons_path']}/",
'T_RANKS_PATH' => "{$forum_static_url}{$this->config['ranks_path']}/",
Note that in the above example, I have already replaced phpbb's usual url with a forum_static_url variable, which could be filled with the URL of your domain from where you wish to serve the static files.
I hope this helps. Do have a look at some extensions already freely available for phpBB3.1 to find out how to implement this simple extension.
Comments
If at all possible, I would not touch PhpBB's code, and rather resort to Apache's URL rewriting engine.
In PHPBB3, most of the static contents (by size) comes from the /assets/ subdirectory.
So if:
- you're using a rewrite-capable browser,
- have
.htaccessor equivalent enabled - and
mod_rewriteor equivalent installed,
in the .htaccess file you can place,
Redirect permanent /assets http://mycdnhoster.com/collector/phpbb3/assets
This allows you to enable/disable CDN very easily, and not worry about maintaining a modification to PhpBB's code.
Same goes for the "style" and "theme" static files. There is a slight performance penalty when the browser still hits your server only to be bounced somewher else, but with modern pipelined browsers that's not a real issue. Also, in most cases the redirected resource will be "remembered" by the browser that won't hit your server again (for some time at least).
Another possibility offered e.g. by NginX is to rewrite the output HTML. You can make it so all references to yoursite/static go to anothersite/differentpath/static using HttpSubModule.
Beware that some files might contain both absolute links that may no longer work, or relative links that may "fall" outside the scope of the rewrite, and so again no longer work. For example:
- javascript files using "delayed load" or "incremental/conditional loading" for plugin and similar features.
- CSS files containing references to fonts and background images (
url(../../../path/...)).
Also beware, since you've specified pagespeed, that mod_pagespeed may be incompatible with this type of URL rewriting, since it will parse the HTML and try to compress the resources referenced therein. So you might end up with all your hefty CSS's offloaded to a CDN, and them still downloaded instead from your server, embedded into an optimized, compressed and hard-to-recognize form in your single mod_pagespeed'ed local CSS reference.
I.e., in your html you have
<link href="/app/small.css" ... />
<link href="/static/big.css" ... />
<link href="/static/big2.css" ... />
and you expect the rewrite to get the static's loaded from offsite. If there were no further optimizations that is what would happen. Instead, your client sees a page rewritten by mod_pagespeed that says
<link href="/app/small+big+big2.css?pagespeed&whatever" />
and he will never request anything in /static, will never get redirected, but will instead request and download a compressed, optimized, but still larger than you'd wish, merged CSS from your server.
4 Comments
Explore related questions
See similar questions with these tags.