Laravel Herd - Fix 504 - 503 PHP and Nginx Errors

Video thumbnail

I was performing some operations on a Laravel application in production mode. As usual, errors are hidden in this environment, and any problems should be logged.

When performing a certain operation, an error always occurred. At first, it was a 503, and then it turned into a 504, which was strange because no associated log was generated.
And that's the first thing you should check: if a log isn't generated, the problem may not be with Laravel, but with another component, such as PHP or the web server (Apache or Nginx, depending on your stack).

In my case, I'm using Nginx. I already more or less knew where the problem was coming from: lack of resources. Below, I'll explain how I addressed it.

Reproducing the bug in development

I decided to replicate the operation in the development environment, where I have the advantage of seeing the errors directly on screen.

To my surprise, no log was generated in development either, which reinforced the hypothesis that the problem wasn't in Laravel, but rather on the server or PHP.

Detect the problem: operation + Error NO on screen/log

With that information, the next step was to analyze what operation was being executed.
In my case, I was generating a workbook (like the one you're seeing on screen). I generate all workbooks directly in production, where I have the data already sanitized.

Normally, these workbooks are between 400 and 500 pages long. But this one in particular is a monster at 813 pages, twice the usual size.

Nature of the problem: insufficient resources

This implies two things:

  1. The operation will take a long time.
  2. It's much more complicated than the others and requires more resources.

So, we now have a clearer idea of ​​what the problem is: an intensive operation exceeding the default limits.

Increase time and memory resources to PHP and nginx

Since there were no logs generated by Laravel, but there was anomalous behavior (504 error), the next step was to check the limits configured in PHP.

In my case, I use Laravel Herd, which uses Nginx, so the approach was a little different than using Apache.

The first thing I did was increase the resources in the php.ini file. This is typically what you try when you have heavy operations:

C:\Users\<user>\.config\herd\bin\php8X

max_execution_time = 600
memory_limit = 1024M
post_max_size = 256M
upload_max_filesize = 256M

How do I access the php.ini file in Laravel Herd?

In Laravel Herd, it's very easy:

  1. Go to the PHP icon in the interface.
  2. Right-click on the active version.
  3. Select the option: "Open php.ini in directory."

Once opened, find the corresponding lines and modify them.

After making those changes... it didn't work.

Uploading time and memory resources to nginx

Since PHP didn't solve the problem, it was time to look deeper into the server.

I moved on to review the Nginx configuration file. In Laravel, this is located inside:

C:\Users\<user>\.config\herd\config\nginx\herd.conf

location / {
    rewrite ^ "C:/Program Files/Herd/resources/app.asar.unpacked/resources/valet/server.php" last;
    
    #MIAS
    proxy_read_timeout 3000;
    proxy_connect_timeout 3000;
    proxy_send_timeout 3000;

}

However, it didn't work either.

Nginx logs

Then I went to the Nginx server log, which is at:

C:\Users\<user>\.config\herd\Log

This is where I did find some useful information. When I checked the log, it clearly indicated a timeout, which wasn't showing up in the Laravel logs:

[error] 616#35124: *1 upstream timed out (10060: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond) while reading response header from upstream, client: 127.0.0.1, server: , request: "POST /livewire/update HTTP/1.1", upstream: "fastcgi://127.0.0.1:9084", host: "xx.test", referrer: "http://xx.test/dashboard/books/edit/1"

I copied the error message and passed it to ChatGPT (although you could also Google it).

This helped me confirm that the problem was a timeout, and that the parameters to be modified were not only those in php.ini, but also those in the Nginx configuration file.

Final configuration that solved the problem
I adjusted the following lines in Nginx:

C:\Users\<user>\.config\herd\config\nginx\herd.conf

location ~ [^/]\.php(/|$) {
    ***
    #MIAS
    fastcgi_read_timeout 600;
    fastcgi_send_timeout 600;
    send_timeout 600;
}

Summary

  • Identify the operation causing the error.
  • If there are no logs in Laravel, it could be a PHP or server issue.
  • First, adjust the php.ini parameters: max_execution_time and memory_limit.
  • If that doesn't work, move on to checking the Nginx configuration files.
  • Use the server log for more clues.
  • Adjust the timeouts in Nginx if necessary.
  • Always comment out your changes and make backups before modifying critical files.

I agree to receive announcements of interest about this Blog.

We talked about how to fix 503 and/or 504 errors for expensive operations that DO NOT generate logs or errors on the screen in the Laravel app.

- Andrés Cruz

En español