3

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?

Collector
  • 2,034
  • 4
  • 22
  • 39

2 Answers2

0

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 .htaccess or equivalent enabled
  • and mod_rewrite or 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.

Community
  • 1
  • 1
LSerni
  • 55,617
  • 10
  • 65
  • 107
  • Thank you for your time with this detailed answer but I'm afraid it's not very helpful and doesn't answer my question on where to make the changes in phpBB. We don't use Apache and 301 redirects are never better than providing the correct resource as you still make a server request. Rewriting the HTML with NginX might be a nice choice but as many resources are relative, I'll still to figure it all out myself so it's not a complete answer. CSS: best would have been to be able to combine them all into a single static one with modified filenames. So thanks, but there's no solution here. – Collector Oct 03 '14 at 00:30
  • I'm not so sure that a solution *exists* unless we simplify the setup and at least renounce pagespeed. I hadn't even considered other issues such as CORS. I'll mumble on this some more... – LSerni Oct 03 '14 at 06:49
  • Of course a solution exist and I've started implementing it by changing PhpBB code (as I wrote in my question). However, as I imagined someone else might have done that in the past, I thought a question such as here might save me (and others reading it later) a lot of time re-implementing the same thing. What does CORS have to do with it?!?! We simply change the resulting HTML to include static resources from another server. If a page from domain X includes a JavaScript from domain Y, it still operates inside domain X. – Collector Oct 03 '14 at 14:54
  • Normally yes. But a script might load additional code (plugins, etc.) and either not find it anymore, or seek it from a different domain and trigger same-domain issues. Most simple rewrites *will* work; but one should also consider that some might not or might require additional workarounds - that's all I was saying. – LSerni Oct 17 '15 at 11:40
0

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.

Steven De Groote
  • 2,187
  • 5
  • 32
  • 52