YII2 load balancer not found JS,CSS

Source: Internet
Author: User
Tags yii

    1. 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.
    2. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.