Javascript yii2和负载平衡
我正在开发一个PHPYII2应用程序。我对yii2有一个奇怪的问题,这是我的问题 正如我们所知,Yii2项目中有一个web\assets文件夹,它将加载自己的资产包,但在配置负载平衡时,我发现chrome的开发者工具控制台抛出以下错误:Javascript yii2和负载平衡,javascript,css,apache,yii2,load-balancing,Javascript,Css,Apache,Yii2,Load Balancing,我正在开发一个PHPYII2应用程序。我对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
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,正好有一个Yi2应用程序在运行,网页就会成功加载js、css文件:
有两台服务器完全运行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;
我尝试使用assetManager或
$config['components']['assetManager']['forceCopy'] = true;
在config\web.php或function beforeAction中,在我自己的控制器中禁用assets文件夹,但它不起作用。是否有任何方法可以解决此问题?确保两台服务器上的Yi2 core的文件路径和版本相同 扩展
assetManager
类和自定义hash()
函数每次更改资产时,请记住清除资产文件夹。
例如:
另一种方式:
确保两台服务器上的yii2 core的文件路径和版本相同。然后清除资产文件夹。两个文件的路径都是var/www/prj4_firekylin/firekylin_php,我已经清除了文件夹,但它不起作用:(它们的子文件的名称仍然不同。对不起,我的错误。这还取决于文件的创建时间。
$config['components']['assetManager']['forceCopy'] = true;
Class MyAssetManager extend yii\web\AssetManager{
protected function hash($path)
{
$path = is_file($path) ? dirname($path) : $path;
return sprintf('%x', crc32($path . Yii::getVersion()));
}
}