2016-11-11 403 views
4

我正在开发PHP Yii2应用程序。我有一个yii2奇怪的问题,这是我的问题。yii2和负载均衡

因为我们知道,有在Yii2项目的web \资产文件夹,它会加载它自己的资产包,但在配置负载均衡的时候,我发现Chrome的开发者工具控制台抛出下面的一些错误:

http://firekylin.eastasia.cloudapp.azure.com/assets/fde0fce1/css/bootstrap.css Failed to load resource: the server responded with a status of 404 (Not Found) 
http://firekylin.eastasia.cloudapp.azure.com/assets/ebd0699a/css/AdminLTE.min.css Failed to load resource: the server responded with a status of 404 (Not Found) 
http://firekylin.eastasia.cloudapp.azure.com/assets/5e97633a/jquery.js Failed to load resource: the server responded with a status of 404 (Not Found) 
yii.js:464 Uncaught ReferenceError: jQuery is not defined(…) 
http://firekylin.eastasia.cloudapp.azure.com/assets/14e563af/yii.validation.js Failed to load resource: the server responded with a status of 404 (Not Found) 
http://firekylin.eastasia.cloudapp.azure.com/assets/fde0fce1/js/bootstrap.js Failed to load resource: the server responded with a status of 404 (Not Found) 
yii.activeForm.js:14 Uncaught TypeError: Cannot read property 'fn' of undefined(…) 
http://firekylin.eastasia.cloudapp.azure.com/assets/fde0fce1/js/bootstrap.js Failed to load resource: the server responded with a status of 404 (Not Found) 
app.min.js:13 Uncaught Error: AdminLTE requires jQuery(…) 
index.php:555 Uncaught ReferenceError: jQuery is not defined(…) 
http://firekylin.eastasia.cloudapp.azure.com/assets/88d3a068/css/font-awesome.min.css Failed to load resource: the server responded with a status of 404 (Not Found) 
http://firekylin.eastasia.cloudapp.azure.com/assets/ebd0699a/css/skins/_all-skins.min.css Failed to load resource: the server responded with a status of 404 (Not Found) 

使用负载均衡ip访问时无法找到js,css文件,但是一旦我使用运行Yii2应用程序的服务器的ip,网页将成功加载js,css文件:

left one is using load balance server to visit and right one is using Yii2 server to visit

有两台服务器恰好运行Yii2,他们都有自己的网站\资产文件夹,并有不同的缓存:

web\assets folders in two Yii2 servers

我发现,一旦我停止Yii2服务器之一,负载均衡服务器将工作因此,我猜可能是Yii2的web \ assets文件夹导致的问题。从上面的两张图片我们可以发现Yii2使用md5来重命名其中的文件夹,所以也许是因为两个Yii2服务器的web \ assets文件夹有不同的命名的子文件夹,使问题发生?

不过,我尝试用下面的代码来解决这个问题:

<?php 

$params = require(__DIR__ . '/params.php'); 

$config = [ 
'id' => 'basic', 
'defaultRoute'=>'firekylin', 
'basePath' => dirname(__DIR__), 
'bootstrap' => ['log'], 
'components' => [ 

    'view' => [ 
     'theme' => [ 
      'pathMap' => [ 
       '@app/views' => '@app/views/dmstr/yii2-adminlte-asset/example-views/yiisoft/yii2-app' 
      ], 
     ], 
    ], 

    'assetManager' => [ 
     'class' => 'yii\web\AssetManager', 
     'forceCopy' => true, 
    ], 


    'request' => [ 
     // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation 
     'cookieValidationKey' => '2MhlyGaaGs_uqt_apwy1jLahR_wZ8dBv', 

     'parsers' => [ 
      'application/json' => 'yii\web\JsonParser', 
      'text/json' => 'yii\web\JsonParser', 
     ] 
    ], 
    'cache' => [ 
     'class' => 'yii\caching\FileCache', 
    ], 
    'user' => [ 
     'identityClass' => 'app\models\OriginUser', 
     'enableAutoLogin' => true, 
    ], 
    'errorHandler' => [ 
     'errorAction' => 'site/error', 
    ], 
    'mailer' => [ 
     'class' => 'yii\swiftmailer\Mailer', 
     // send all mails to a file by default. You have to set 
     // 'useFileTransport' to false and configure a transport 
     // for the mailer to send real emails. 
     'useFileTransport' => true, 
    ], 
    'log' => [ 
     'traceLevel' => YII_DEBUG ? 3 : 0, 
     'targets' => [ 
      [ 
       'class' => 'yii\log\FileTarget', 
       'levels' => ['error', 'warning'], 
      ], 
     ], 
    ], 
    'db' => require(__DIR__ . '/db.php'), 
    /* 
    'urlManager' => [ 
     'enablePrettyUrl' => true, 
     'showScriptName' => false, 
     'rules' => [ 
     ], 
    ], 
    */ 
], 
'params' => $params, 
]; 

if (YII_ENV_DEV) { 
$config['bootstrap'][] = 'debug'; 
$config['modules']['debug'] = [ 
    'class' => 'yii\debug\Module', 
]; 

$config['bootstrap'][] = 'gii'; 
$config['modules']['gii'] = [ 
    'class' => 'yii\gii\Module', 
]; 
$config['components']['assetManager']['forceCopy'] = true; 
} 

return $config; 

public function beforeAction($action){ 

     Yii::$app->assetManager->forceCopy = YII_DEBUG; 

    return parent::beforeAction($action); 
} 

我尝试使用assetManager或

$config['components']['assetManager']['forceCopy'] = true; 

在配置\网络.php或函数beforeAction在我自己的控制器中禁止资产文件夹,但它不起作用。是否有任何方法可以解决问题?

+0

确保yii2核心的文件路径和版本在两台服务器上都相同。然后清除资产文件夹。 –

+0

文件的两个路径是无功/网络/ prj4_firekylin/firekylin_php,我已经明确的文件夹,但它不工作:(他们的孩子文件的名称仍然是不同的。 –

+0

对不起,我错了。这也取决于创建文件的时间。 –

回答

3

确保yii2核心的文件路径和版本在两台服务器上都相同。

扩展assetManager类和自定义hash()函数。 每次更改资产时请记住清除资产文件夹。

如:

Class MyAssetManager extend yii\web\AssetManager{ 
    protected function hash($path) 
    { 
     $path = is_file($path) ? dirname($path) : $path; 
     return sprintf('%x', crc32($path . Yii::getVersion())); 
    } 
} 

一些其他的方式:

  1. 使用hashCallback性质:http://www.yiiframework.com/doc-2.0/yii-web-assetmanager.html# $ hashCallback细节
  2. 更改负载平衡配置会话如路线基地:NGINX 会议持久性
  3. 使用CDN作为资产
+0

谢谢!有用:) –