Search code examples
ajaxxmlhttprequesthttp2http-status-code-504

How to fix HTTP/2.0 504 Gateway Timeout for multi simultaneous XHR connections when using HTTP/2


I activate HTTP/2 support on my server. Now i got the problem with AJAX/jQuery scipts like upload or Api handling.

After max_input_time of 60sec for php i got: [HTTP/2.0 504 Gateway Timeout 60034ms]

with HTTP/1 only a few connections where startet simultaneously and when one is finished a nother starts. with HTTP/2 all starts at once.

when fore example 100 images would uploaded it takes to long for all.

I don't wish to change the max_input_time. I hope to limit the simultaneous connections in the scripts.

thank you


Solution

  • HTTP/2 intentionally allows multiple requests in parallel. This differs from HTTP/1.1 which only allowed one request at a time (but which browsers compensated for by opening 6 parallel connections). The downside to drastically increasing that limit is you can have more requests on the go at once, contending for bandwidth.

    You’ve basically two choices to resolve this:

    1. Change your application to throttle uploads rather than expecting the browser or the protocol to do this for you.
    2. Limit the maximum number of concurrent streams allowed by your webserver. In Apache for example, this is controlled by the H2MaxSessionStreams Directive while in Nginx it is similarly controlled by the http2_max_concurrent_streams config. Other streams will need to wait.