Hello, I have a problem when using Http Request class from Laravel. I have a simple route function to testing the case. When i have 2 request in 1 function, that the 2 request Http backend is Laravel, it not give a response, and it's buffering like always loading until we stopped it or reloaded it in NGINX Laragon. But, if we use another API Backend (e.g: Nest.JS, or Fake API). It gives the response. It's work too when i'm using 2 different Backend API too request in one function, the first request is using Laravel API, and the second request is using Nest.JS/Fake API. It's work too if i'm running my Laravel request is using service from Laravel (php artisan), and when request is work too. I'm also testing my Laravel backend (that integrated in NGINX) in Postman/Insomnia, it's work too, that no problem. I want when my project (backend or frontend) from Laravel could be worked in NGINX.
As i'm analyzed it, i'm see that one function can't running two cURL. But when i'm test, it worked in Apache.
This is the code request from Laravel (Frontend)
Route::get('testimoni', function () {
$response1 = Http::get('http://localhost/api-project/api/menu-sidebar'); //Laravel
$result1 = $response1->json();
dump($result1);
$response2 = Http::get('http://localhost/api-project/api/menu-sidebar'); //Laravel
$result2 = $response2->json();
dump($result2);
});
If i'm runningthe two requests like that in Laravel using NGINX Service. It will be buffering and always loading until i stop or reload the NGINX, it will be give response 'Bad Gateway'. It will work if using Apache Service. And will work too if one of the HTTP Request is not using Laravel (Nest.Js, Fake API, etc).
Maybe have a user that got this case could be help, Thank you
When Laravel makes an HTTP request to itself via Http::get(), Nginx locks the request in the same process. Laravel waits for the first request to finish before handling the second one. Since Laravel itself is already handling the first request, it can't handle the second one until the first finishes. This causes a deadlock where Laravel is waiting for a response from itself, but it can't process that response because it's busy waiting.
Use Http::async() to Make Requests Asynchronously
Here is modified code using async:
Route::get('testimoni', function () { $responses = Http::pool(fn ($pool) => [ $pool->get('http://localhost/api-project/api/menu-sidebar'), $pool->get('http://localhost/api-project/api/menu-sidebar'), ]); $result1 = $responses[0]->json(); $result2 = $responses[1]->json(); dump($result1, $result2); });
or you can do like this:
$result1 = app(\App\Http\Controllers\MenuSidebarController::class)->index();
It still same, when using pool for handle deadlock request. If using multiple request from Laravel API, it will be shown like this. The surprising thing is it only stuck when request Laravel API, if using Nest.JS Api or Fake API, it works like usually. Really, i'm very confused to handle this.
I mean the bug is from cURL Request, i test in sample PHP Project. It's not work too.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community