I got a HTTP Connection Failure and 502 error for requests between a Service API I use that POST's to my API on laravel forge.
Everything was working fine. then I got a bunch of connection fails.
I am on digital ocean.
I found this error:
2018/03/20 11:01:04 [error] 4685#4685: *16223 connect() to unix:/var/run/php/php7.1-fpm.sock failed (11: Resource temporarily unavailable) while connecting to upstream, client:
this came out of the blue. same amount of traffic and requests as before
Did you notice if you had a bunch file probing in the logs? Max connections were probably exceeded if so. You definitely need to impose strict limits for an API. Middleware can be used effectively for that.
I would try middleware first because it will limit the number of attempts which is probably the issue. Here is a simple middleware for limiting attempts:
namespace App\Http\Middleware;
use App\Traits\ApiResponser;
use Illuminate\Routing\Middleware\ThrottleRequests;
class CustomThrottleRequests extends ThrottleRequests
{
use ApiResponser;
/**
* Create a 'too many attempts' response.
*
* @param string $key
* @param int $maxAttempts
* @return \Symfony\Component\HttpFoundation\Response
*/
protected function buildResponse($key, $maxAttempts)
{
$response = $this->errorResponse('Too Many Attempts.', 429);
$retryAfter = $this->limiter->availableIn($key);
return $this->addHeaders(
$response,
$maxAttempts,
$this->calculateRemainingAttempts($key, $maxAttempts, $retryAfter),
$retryAfter
);
}
}
Then, in App\Http\Kernel.php and in the api array set something like throttle:10,1. In the same file and inside the $routeMiddleware you should add the class with something like throttle => \App\Http\Middleware\CustomZThrottleRequests. That should do it if I didn't forget anything.