Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

MansourM's avatar

Laravel Dockerized Server Freezes During External API Requests - Seeking Help on Timeout Issues

Hello Laracast community,

I hope you're all doing well. I am encountering an issue with my Dockerized Laravel server, specifically during tests on development. The main containers managed by Docker Compose include Nginx Proxy Manager, MySQL 8, PhpMyAdmin, and the Laravel server.

you can find my docker setup here (still WIP) :

https://github.com/MansourM/ez-docker-for-laravel/tree/profiles

The Problem:

I have observed random freezes on the server, when running tests that involve external API requests. The server will freezes/timeout indefinitely, i wrote a test to be sure if the cause are external api requests are or not.

Test Scenario:

    public function testExternalApiRequests($count)
    {
        if ($count > 100)
            dd("max count is 100, yours: " + $count);
        $requestCount = $count;

        for ($i = 1; $i <= $requestCount; $i++) {
            $response = $this->makeRequest("https://jsonplaceholder.typicode.com/todos/$i");
            dump("Request $i response:", json_decode($response->getBody()->getContents(), true));
        }
        dd("DONE!");
    }

    private function makeRequest($url, $options = [])
    {
        $client = new \GuzzleHttp\Client();
        return $client->request('GET', $url, $options);
    }

and as i had assumed the server freezes randomly when runing this code. also even if the code doesn't not compeletly freeze the server indefinitly and finishes, the server is frozen (can't open any other page) while the loop is running. i checked logs and the only error im getting in logs are timeouts

[error] 14040#14040: *206681 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 5.125.19.238, server: test.andro.law, request: "GET /favicon.ico HTTP/2.0", upstream: "http://172.18.0.5:8000/favicon.ico", host: "test.andro.law", referrer: "https://myurlwashere.com/"

and these ones

2024/02/18 16:42:21 [warn] 14099#14099: 512 worker_connections are not enough, reusing connections
2024/02/18 16:42:21 [alert] 14099#14099: 512 worker_connections are not enough
2024/02/18 16:42:21 [error] 14098#14098: *208003 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 127.0.0.1, server: localhost-nginx-proxy-manager, request: "GET /jquery-3.3.1.slim.min.js HTTP/1.1", upstream: "http://127.0.0.1:80/jquery-3.3.1.slim.min.js", host: "37.32.8.118"

it seems that it has something to do with worker_connections, i searched online it was suggested to increase it, but im asking here because that does not make sense because the default connection count is suposes to be ~1k so it should be fine in my case when im just 1 person testing ...

my main questions right now are:

  1. I can't fully understand the root cuase of the problem.
  2. what is the correct solution?
0 likes
2 replies
MansourM's avatar

one more thing i noticed right now is the automatically generated nginx conf for my server is

server {
  set $forward_scheme http;
  set $server         "laravel-server-test";
  set $port           8000;

  listen 80;
listen [::]:80;

listen 443 ssl http2;
listen [::]:443 ssl http2;

  server_name test.andro.law;

  # Let's Encrypt SSL
  include conf.d/include/letsencrypt-acme-challenge.conf;
  include conf.d/include/ssl-ciphers.conf;
  ssl_certificate /etc/letsencrypt/live/npm-1/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/npm-1/privkey.pem;

  # Block Exploits
  include conf.d/include/block-exploits.conf;

  access_log /data/logs/proxy-host-1_access.log proxy;
  error_log /data/logs/proxy-host-1_error.log warn;

  location / {

    # Proxy!
    include conf.d/include/proxy.conf;
  }

  # Custom
  include /data/nginx/custom/server_proxy[.]conf;
}

and the laravel suggested configs from docs are

server {
    listen 80;
    listen [::]:80;
    server_name example.com;
    root /srv/example.com/public;
 
    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-Content-Type-Options "nosniff";
 
    index index.php;
 
    charset utf-8;
 
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
 
    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }
 
    error_page 404 /index.php;
 
    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }
 
    location ~ /\.(?!well-known).* {
        deny all;
    }
}

Could this be the cause? I'm not entirely familiar with the nginx configurations, so I need to delve into the documentation for a better understanding.

I haven't encountered this issue before, but to be honest, I've never tested it in a production environment either.

MansourM's avatar
MansourM
OP
Best Answer
Level 1

upon further investigation i noticed the guides i followed to set docker-compose up were both wrong on a very important thing php artisan serve is not for production and is development only I'm not sure if this is the cause of the problem but ill try to follow this guide

https://www.digitalocean.com/community/tutorials/how-to-install-and-set-up-laravel-with-docker-compose-on-ubuntu-22-04

it might fix things, but still i really want to understand what was causing the timeout problem in the first place

Please or to participate in this conversation.