How To Keep Assets Folders Synchronized On Different Machines?

I deployed my php code to an app engine. Because of load balancing, requests sent to the app engine are automatically distributed to different servers. Since the assets folders on different servers are created automatically by Yii and their names are random and different, this causes asset folder mismatch problem. For example,

  1. There is a page request, after load balancing it goes to SERVER_1, and the corresponding jquery folder is /assets/aaaaaa/jquery.js;

  2. Then the browser send request to get the javascript file. However, this time the request goes to SERVER_2 after load balancing, but the file /assets/aaaaaa/jquery.js does not exist on SERVER_2 (for example, it might be /assets/bbbbbb/jquery.js).

Now I temporarily solved this problem by using URL write that redirects all requests sent to /assets/[random_number]/ to a static folder /assets/static/. I think this is not a good solution due to its lack of flexibility.

Is there any way to avoid this problem? Many thanks.

I have the same problem. I use Yii whith nodejs, mongodb on CentOS VMs.

Just brainstorming here, but wouldn’t it be possible to extend CAssetManager, override generatePath and then class map the new class to CAssetManager?

Hey ,

i have the same problem.

i am using load balancer with yii behind. what is the best solution ? rewrite the AssetManger ?

yii is not supporting a load balancer use ? really ? :(

One of the workarounds is to use Sticky sessions in load balancer.

Another workaround would be to use CDN for your assets.

Has anybody found a proper solution for this problem? I’m facing the same issue.

My idea was to extend CAssetManager and overwrite the hash function.




        /**

         * Generate a CRC32 hash for the directory path. Collisions are higher

         * than MD5 but generates a much smaller hash string.

         * @param string $path string to be hashed.

         * @return string hashed string.

         */

        protected function hash($path)

        {

                return sprintf('%x',crc32($path.Yii::getVersion()));

        }




Here $path takes the absolute server path - if we adapt the path to just use the relative path without server information, we could share the assets folder among different servers.

What do you think?

Hi,

my solution was to leave the assets , and use CDN.

cloudFront/ cloudFlare , you pick it.