Yii2 Global Assets in AWS Load Balanced Environment

I was trying to deploy an application developed using Yii2 to load balanced servers. After deploying the app to each servers (I was using AWS OpsWorks), problem arose when I do composer update to install the dependencies. The problem was that, filemtime for the assets files are surely different for each servers, and thus the hash for asset directories name for each server is different.

This problem caused the web app to be broken when accessed from the load balancer. When a request come, the load balancer could try to load the assets file from different servers, however since the asset directories name are different in each server, the request will fail with error file not found.


Since Yii 2.0.6, it’s possible to customize directory names. So, first thing I did was updating my composer.json and set "yiisoft/yii2": ">=2.0.6" inside require block.

Next, in web config file, I add an asset manager block inside components.

'assetManager' => [
    'class' => 'yii\web\AssetManager',
    'hashCallback' => function ($path) {
        $pattern = '/\/srv\/www\/my_yii_app\/releases\/\d+\//';
        $replacement = '';

        $subject = preg_replace($pattern, $replacement, $path);
        return hash('md4', $subject);

This block function is to replace the default hashing technique which uses CRC32 and based on filemtime. A little note about the block above is that I’m using regex to preprocess the path string before hashed. That is because when we deploy using OpsWorks, AWS will embed timestamp to the directory path. So to make sure the path-that-is-going-to-be-hashed is same in every server, I do a little preprocessing to remove the path prefixes.

After this, simply re-run composer update and everything works fine. Hope this might help someone.

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout /  Ubah )

Foto Google

You are commenting using your Google account. Logout /  Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout /  Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout /  Ubah )

Connecting to %s