Does a PHP script stop executing when you receive 504 Gateway Timeout?

13,453

Solution 1

Script will not stop executing until they reach php timeout itself.

The 504 error are generated on the gateway / proxy itself, it's not coming from the php process.

It's because this, if you have an Apache with Apache-mod-php you will never receive this error, because there's not a proxy.

Extending the explanation, think as follows:

You have a PHP process. The PHP process can be a PHP-FPM, PHP-CGI, or Apache-MOD-PHP. On this process, you have a timeout (configured on php.ini or with a ini_set).

The PHP proxy gives a response in the allowed time (aka: if you have a set_time_limit(600), your PHP process can be running up to 10 minutes).

Without relation on this sentence, you can have another process waiting for these response: That's the case of a apache (configured to contact to php by cgi or fpm), a nginx, a lighthttpd, and others. That's not the case of a apache configured by apache-mod-php. This second process, have a new timeout (proxy_timeout), configured on the vhost / general server app config. That's the time the program will wait for a response from PHP processing engine.

The last sentence, can be repeated on each proxy / gateway.

Think on this scenario:

haproxy (Timeout 1) --> Nginx (Frontend / cache) (Timeout 2) --> Apache (Timeout 3) -> PHP-FPM (PHP Timeout / set_time_limit).

And a very simple scenario:

Apache (with apache-mod-php) (PHP Timeout / set_time_limit).

Each timeout appearance (except the PHP Timeout itself) is a probable origin of a 504 HTTP gateway timeout error.

Solution 2

My understanding is that the script does continue running, but stops reporting data to the client. (Not that I have the knowledge to explain it.) Here's an article that may help:

How to Fix a 504 Gateway Timeout Error in WordPress

Share:
13,453
HOY
Author by

HOY

Updated on September 18, 2022

Comments

  • HOY
    HOY almost 2 years

    I am on a shared server (Siteground), and since my WordPress PHP script takes more than 30 seconds, it returns a 504 Gateway timeout.

    Will my query run and complete if it doesn't encounter a further error?

    Edit: I asked why I received this error to my hosting team, here the Siteground Hosting expert explained the issue as follows:

    We use both Apache + Nginx on all our servers. Apache is used for main web service, while the Nginx is used as a reverse-proxy and distribute cache. When the response cannot be served from cache (usually this refers to dynamic content) a request from Nginx is done towards Apache. This when Apache is processing the request, forwards it to your website and according to the PHP logic MySQL queries can be done or other data fetched. When this process takes too long and Apache does not return the response in a timely manner to Nginx you will see this error. In short Apache cannot server the request since the application did completed the process within the allowed time. This also means that the initiated process most probably did not completed fully and some data / actions might have been saved / executed.

    The expert says that "initiated process most probably not completed fully",

    More Details on my scenario: My script adds woocommerce products with variations using wp_insert_post method to my wordpress website. After products added, it displays the images of newly added products.

    When I add 1 product (40 variations), it completes and displays the product image. When I add 6 products (240 variations), I receive error directly in my browser.

    So to further test this issue, I modified my code and re-wrote it using ajax and added a process bar like system. (Which increments a number for each variation).

    After I run the code for 1 product (with 40 variations), the process number increases to 40, and it displays the product image.

    When I run my code for 6 product, the process number increases to 240, but it doesn't display anything, and when I check, it receives an 504 error. (jQuery.Ajax function error section)

    So this makes me think that the query runs even it timeouts, but still I am not able to be sure, and looking for details behing 504 gateway timeout error since there are no good documentation on this.

    • Stephen Ostermiller
      Stephen Ostermiller over 5 years
      It may depend on on where the gateway timeout is coming from. This is an interesting question.
    • HOY
      HOY over 5 years
      @StephenOstermiller added some more details based on your comment.
    • MrWhite
      MrWhite over 5 years
      Presumably you would know whether the script completed if all "6 products (240 variations)" were added to your website? Or is that difficult to determine? Maybe add some logging functionality to your script?
  • MrWhite
    MrWhite over 5 years
    "if you have an Apache with Apache-mod-php you will never receive this error" - can you explain this last sentence?
  • Sakura Kinomoto
    Sakura Kinomoto over 5 years
    When you use a Apache server with mod-php, the execution has done by Apache itself. Because this, you cannot get a 504 timeout, because there's not any gateway on the process. If you use php-cgi or php-fpm, the Apache /nginx / some other web server are acting as a proxy or gateway to the real processing engine. Then, 504 error appears when this gateway didn't receive a response in a configured amount of time.
  • MrWhite
    MrWhite over 5 years
    But can you not have an Nginx reverse proxy in front of Apache mod-php?
  • Sakura Kinomoto
    Sakura Kinomoto over 5 years
    Obviously, but that's not the question itself. In this case, 504 can be given by nginx, but not Apache. The question on this case, are who if the 504 error code are generated by the proxy, the execution triggered by the proxy call are not stopped. On the case of a gateway or proxy, there's at least two timers in play. The timeout of proxy communication (on nginx for example) and the timeout on the php process.
  • Sakura Kinomoto
    Sakura Kinomoto over 5 years
    I've changed the answer for try to explain in more detail the question itself.