[英]yii2 and load balancing
我正在研究PHP Yii2應用程序。 我對yii2有一個奇怪的問題,這是我的問題。
眾所周知,Yii2項目中有一個web \\ assets文件夾,它將加載它自己的資產包,但在配置負載平衡時,我發現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文件,但是一旦我使用服務器的ip正好運行Yii2應用程序,網頁將成功加載js,css文件:
左邊一個是使用負載平衡服務器訪問,右邊一個是使用Yii2服務器訪問
有兩台服務器正好運行Yii2,它們都有自己的web \\ assets文件夾,並有不同的緩存:
我發現一旦我停止其中一個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;
在我自己的控制器中的config \\ web.php或函數beforeAction中禁止資產文件夾,但它不起作用。有什么方法可以解決問題嗎?
確保兩台服務器上的yii2核心的文件路徑和版本相同。
擴展assetManager
類和自定義hash()
函數。 請記住每次更改資產時清除Assets文件夾。
例如:
Class MyAssetManager extend yii\web\AssetManager{
protected function hash($path)
{
$path = is_file($path) ? dirname($path) : $path;
return sprintf('%x', crc32($path . Yii::getVersion()));
}
}
其他一些方式:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.