PHP高流量默認頁面


3

我有一個php網站,它將在幾週內遇到高流量。我們對服務器進行了擴展,以接受流量,但是在發生某些情況時需要某種回退頁。

有人知道實現此目標的簡單方法嗎?

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

是否需要在有條件的情況下甚至在到達客戶時檢查var_dump(http_response_code());的結果?

3

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.