Serve static phpBB resources from another server?

phpBB has a lot of static resources and serves them from a different server than the dynamic forum server can mean a noticeable increase in performance. We can set header expiration dates far into the future and possibly use CDNs in the future.

From the checks I've done so far, it seems that changing functions.php to point to 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 is not clear to me if there could be other consequences, such as making the downloads directory inaccessible, since the paths seem to be local to the server. Also, it doesn't allow dynamic CSS to be generated using styles.php

Is there a quick way to indicate changes to static resources without breaking phpBB code?

+3


source to share


2 answers


If at all possible, I would not touch the PhpBB code and rather resort to Apache's URL rewriting mechanism.

In PHPBB3, most of the static content (in size) comes from a subdirectory /assets/

.

So if:

  • you are using a rewriteable browser,
  • yes .htaccess

    or equivalent included
  • and mod_rewrite

    or equivalent,

in a file .htaccess

you can place,

Redirect permanent /assets http://mycdnhoster.com/collector/phpbb3/assets

      

This allows you to easily enable / disable CDNs and not worry about keeping your phpBB code modified.

The same goes for the static files "style" and "theme". There is a slight performance hit where the browser still hits your server just to be dropped, but with modern pipelined browsers, that isn't a real issue. In addition, in most cases the redirected resource will be "remembered" by the browser, which will no longer hit your server (at least for a while).

Another possibility offered by, for example, NginX is to rewrite the output HTML. You can make all links to your site / static go to another site / different path / static using HttpSubModule .



Beware that some files may contain both absolute links that can no longer work, and relative links that may "fall" outside the rewritable area and therefore do not work again. For example:

  • javascript files using lazy loading "or" incremental / conditional loading "for plugin and similar functionality.
  • CSS files containing links to fonts and background images ( url(../../../path/...)

    ).

Also, be careful as you pointed pagespeedout what mod_pagespeed

might be incompatible with this type of URL rewriting as it will parse the HTML and try to compress the resources mentioned in it. This way you can end up with all your heavy CSS paged out to the CDN and still being loaded from your server, embedded in an optimized, concise, and hard to recognize form in your only CSS reference article on mod_pagespeed.

Ie, 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 so that the static is loaded externally. If there are no further optimizations, this will happen. Instead, your client sees a page rewritten mod_pagespeed

that says

<link href="/app/small+big+big2.css?pagespeed&whatever" />

      

and it will never ask for anything in /static

, never redirect, but will instead request and download a compressed, optimized but still more than you'd like, merged CSS from your server.

+1


source


Given the date of your question, I am assuming that you are already using phpBB 3.1, in which case you can write an extension that does what you need, leaves the phpBB code unchanged and will not interfere with forum updates. If you don't, you will run into code modifications or other ways to fix your problem.

For 3.1, you will need to write a plugin that hooks to the "core.page_header_after" event, which will be executed at the end of the function page_header()

. This allows you to overwrite any template variables created in the header and add new ones if you want to use others in your templates.

In your case, you need to look at how 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 the normal phpbb url with the forum_static_url variable, which can be populated with the url of your domain from which you want to serve static files.

Hope this helps. Take a look at some of the extensions already freely available for phpBB3.1 to see how to implement this simple extension.

0


source







All Articles