- When deploying the project, 2 servers were used. The request was loaded with load balancing, resulting in YII2 static files (js,css ...) Reported 404, The reason is: the request for a page when a server to process, but the static resource is missing the request to the B server, B server's YII2 resource manager has not been generated in the Web\assets directory and static files.
- Take a closer look at the static resource Management code for YII2:
yii\web\AssetManager
(define resource packages), ' yii\web\AssetBundle
(file dependencies between resource bundles, load and create). There are several solutions:
-
Changes the naming of the build cache directory and maintains a consistent folder, with the disadvantage of not generating a static file directory when accessing one of the servers. You need to access it until 2 servers have the same directory.
//configuration main.php Components array under Assetmanager ' Assetmanager ' =>[ //cache the same file to the same folder ' hashcallback ' =>function ( $path ) {return hash ( Span class= "St", ' MD4 ' $path } //Add file timestamp ' appendtimestamp ' = true
Synchronizing the cache directory, this is relatively simple. is to keep 2 servers have the same static resources, manual synchronization is FTP upload, automatic synchronization is the 2 servers between the request to remain consistent. There are tools available: Linux rsync directory synchronization
Using a CDN, assign the ' sourcepath ' value to null instead of an empty string when configuring the CDN. The CDN address of each resource bundle can be found on the CDN platform, or it can be uploaded to the cloud space on its own. Personal recommendation this way.
' Assetmanager '=[' Bundles '=[ //jquery.js ' Yii\web\jqueryasset '=[ ' SourcePath '=NULL, ' BaseUrl '=' https://cdn.bootcss.com/jquery/2.2.4/', ], //yii.js ' Yii\web\yiiasset '=[ ' SourcePath '=NULL, ' BaseUrl '=' http://p8mo8fh1z.bkt.clouddn.com/', ], //bootstrap.css ' Yii\bootstrap\bootstrapasset '=[ ' SourcePath '=NULL, ' BaseUrl '=' https://cdn.bootcss.com/bootstrap/3.3.7/', ], //bootstrap.js ' Yii\bootstrap\bootstrappluginasset '=[ ' SourcePath '=NULL, ' BaseUrl '=' https://cdn.bootcss.com/bootstrap/3.3.7/', ],]],
Easydo.work
YII2 load balancer not found JS,CSS