我最初的想法是在請求頁面時檢查標頭,但不太了解那是否重要。如果出現504 Gateway Timeout錯誤,用戶甚至還會收到初始的HTTP標頭嗎?



For a better answer we certainly could use some more information, for example:

  • What web server are you using?
  • How are you balancing connections to them?
  • Do you have any layer of cache?
  • What kind of data do you send clients? Can it be cached? It is behind an HTTPs-only protocol?

Based on the information you gave, I would discourage doing what you want to do in PHP. As you may have guessed, depending on the occasion (all workers are busy processing other requests), PHP won't even be triggered and a 504 header will be sent to the client as a response by the web server, without reaching PHP as you already expect.

I would recommend a HTTP cache layer before all requests, like Varnish. By default it will cache pages and prevent that your web servers get overloaded. In the event of that happening, you can easily configure varnish to send a response (HTML page, for example) instead of the 504 page. But if the page can be cached, than if you have more than one varnish load balanced with HAproxy (for example) you will hardly experience the problem.