2016-11-11 11 views
4

私はPHP Yii2アプリケーションを開発中です。私はyii2に奇妙な問題があります。ここに私の問題があります。yii2とロードバランシング

我々が知られているように、ウェブ\資産フォルダがYii2プロジェクトであり、それはそれが自分の資産バンドルのロードされますが、ロードバランスを設定するとき、私は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を使用すると、Webページはjs、cssファイルを正常にロードします。

あり、正確Yii2を実行している2台のサーバーがあり、それらの両方が自分のWeb \資産フォルダを有し、異なるキャッシュを持っている:

web\assets folders in two Yii2 servers

私はYii2 Serverの1つを停止した後、負荷分散サーバが動作することがわかりましたしたがって、Yii2のweb \ assetsフォルダから問題が発生していると思われます。上記の2つの画像から、Yii2はmd5を使用してフォルダの名前を変更することができます。問題を起こさせる異なる名前の子フォルダを持っていますか?私は設定\ウェブでassetManagerまたは

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

を使用しようと

<?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); 
} 

しかし、私は以下のコードで問題を解決しようとしました私の自身のコントローラで、資産フォルダを禁止するために.phpや関数beforeActionを呼び出すことはできますが、動作しません。問題?

+0

yii2コアのファイルパスとバージョンが両方のサーバーで同じであることを確認してください。その後、資産フォルダをクリアします。 –

+0

両方のファイルのパスはvar/www/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

ありがとう!できます:) –