Laravel线上运行常见问题及解决方法

一、问题及解决一

部分服务的api使用Laravel编写,但是上线后有3%-5%的概率,不能返回正常的结果,而是返回未知500错误与{“error”:”Unauthenticated.”}403错误。

通过Jmeter压测与日志监测后发现上报的异常如下:

1、production.ERROR: RuntimeException: The only supported ciphers are AES-128-CBC and AES-256-CBC with the correct key lengths.

2、production.ERROR: PDOException: SQLSTATE[HY000] [1045] Access denied for user ‘forge’@’localhost’ (using password: NO)

3、production.ERROR: League\OAuth2\Server\Exception\OAuthServerException: The resource owner or authorization server denied the request.

分析过程:

1、第二类型错误Access denied for user ‘forge’@’localhost’很奇怪,因为众所周知Laravel的默认mysql账户为forge。但是若.env文件内声明了mysql账号后,应该会使用.env文件内的配置,而非还是forge。因此怀疑.env文件未加载成功。

2、第一类经过谷歌后,是加密方式与.env文件内设置冲突导致。

解决方法:

1、重新生成APP_KEY

php artisan key:generate

执行后.env文件内的APP_KEY重新生成。

2、修改.env文件内系统运行环境,由debug环境设置为生产环境

APP_ENV=production

3、由于怀疑.env文件加载失败,导致了很多错误,因此尝试建立配置文件缓存。

php artisan route:clear
php artisan config:clear

php artisan route:cache
php artisan optimize 
php artisan config:cache
composer dumpautoload -o

二、问题及解决二

经过上述改动,并使用Jmeter按照15QPS不间断测试50W次后,发现还有0.03%会返回未知的失败。

经过跟踪后发现出现的问题如下:

1、production.ERROR: ErrorException: openssl_verify(): key type not supported in this PHP build!

2、production.ERROR: PDOException: SQLSTATE[HY000] [2002] 由于目标计算机积极拒绝,无法连接。

3、production.ERROR: League\OAuth2\Server\Exception\OAuthServerException: The resource owner or authorization server denied the request.

分析过程:

1、经过大量观察后发现,错误3总是在错误2之后,也就是很可能数据库连接错误导致了错误3 The resource owner or authorization server denied the request.的这个问题。因此错误3暂时忽略,主要管靠错误2即可。

2、经过统计后发现,错误2出现的次数占据0.0005%,应该是数据库的问题,暂时忽略。

3、错误1目前出错的概率为0.001%左右,经过谷歌后(https://bugs.php.net/bug.php?id=66501),发现是php本身的bug,暂时忽略。

三、问题及解决三

某天发现Laravel挂了,错误如下:

Notice: Undefined variable: _GET in D:\thSites\xbjs.api\vendor\symfony\http-foundation\Request.php on line 315

Fatal error: Uncaught TypeError: Argument 1 passed to Symfony\Component\HttpFoundation\Request::createRequestFromFactory() must be of the type array, null given, called in D:\thSites\xbjs.api\vendor\symfony\http-foundation\Request.php on line 315 and defined in D:\thSites\xbjs.api\vendor\symfony\http-foundation\Request.php:2014 Stack trace: #0 D:\thSites\xbjs.api\vendor\symfony\http-foundation\Request.php(315): Symfony\Component\HttpFoundation\Request::createRequestFromFactory(NULL, Array, Array, Array, Array, Array) #1 D:\thSites\xbjs.api\vendor\laravel\framework\src\Illuminate\Http\Request.php(59): Symfony\Component\HttpFoundation\Request::createFromGlobals() #2 D:\thSites\xbjs.api\public\index.php(53): Illuminate\Http\Request::capture() #3 {main} thrown in D:\thSites\xbjs.api\vendor\symfony\http-foundation\Request.php on line 2014

  没有英汉互译结果
请尝试网页搜索