How to Resolve ERR_HTTP2_PROTOCOL_ERROR in Your Browser

3 min read

If you search online, many websites offer the same standard advice, which is unrelated to development and often unhelpful for advanced users:

  • Clear the browser cache;
  • Disable extensions;
  • Disable QUIC;
  • Disable security software such as antivirus or a firewall;
  • Check that the date and time are correct;
  • Enable a VPN;
  • Update the browser;
  • And so on.

The standard suggestions found on many websites may help an everyday user resolve the issue. However, if you are a developer or DevOps engineer and the problem appeared after deploying an application, these actions may not help. In that situation, try the following steps:

Your nginx virtual host configuration contains the following directives, or uses their default values:

fastcgi_buffer_size 4k;
fastcgi_buffers 8 4k;

fastcgi_buffer_size sets the size of the buffer used to read the first part of the response from the FastCGI server. This part usually contains a small response header. By default, the buffer matches the memory page size, which is either 4K or 8K depending on the platform. It can be reduced if required.

fastcgi_buffers controls response buffering. When enabled, nginx reads the response from the FastCGI server as quickly as possible and stores it in the buffers defined by fastcgi_buffer_size and fastcgi_buffers.

If the entire response does not fit in memory, part of it may be written to a temporary file on disk. This is controlled by the following directives:

  • fastcgi_max_temp_file_size: sets the maximum size of the temporary file.
  • fastcgi_temp_file_write_size: sets the size of the data chunks written to the temporary file.

These options help manage memory and disk usage when handling FastCGI responses and support stable application operation.

With buffering disabled, the response is passed to the client synchronously as it arrives. nginx does not attempt to read the entire FastCGI response. The maximum amount of data it can receive at once is determined by fastcgi_buffer_size.

Buffering can also be enabled or disabled by returning ‘yes’ or ‘no’ in the X-Accel-Buffering response header. This behaviour can be disabled with fastcgi_ignore_headers.

An insufficient buffer size can cause ERR_HTTP2_PROTOCOL_ERROR when requesting resources. Increasing the buffer size, for example by doubling it, may therefore resolve the issue.

Here is an example nginx configuration:

fastcgi_buffer_size 16k; # Increase the buffer size to 16KB
fastcgi_buffers 4 16k; # Set the number and size of buffers

# Prevent the header from controlling buffering
fastcgi_ignore_headers "X-Accel-Buffering";

These changes allow nginx to handle larger chunks of data, which may resolve ERR_HTTP2_PROTOCOL_ERROR. Adjust the values to suit your application and server configuration.

The likelihood of resolving the problem is 99%. Increase the values again if the problem returns.

How to Choose fastcgi_buffer_size and fastcgi_buffers Values

For more precise tuning, determine the maximum and average sizes of the data received by nginx, then use those figures to set appropriate values.

Maximum size: awk '($9 ~ /200/)' access.log | awk '{print $10}' | sort -nr | head -n 1

Note that we consider only HTTP 200 OK responses.

Average size: echo $(( `awk '($9 ~ /200/)' access.log | awk '{print $10}' | awk '{s+=$1} END {print s}'` / `awk '($9 ~ /200/)' access.log | wc -l` ))

Suppose you obtain the following values:

Maximum: 31,345 bytes (31K, rounded up to 32K).

Average: 6,251 bytes (6K, rounded up to 8K).

The resulting settings would be:

fastcgi_buffer_size 8k; # Covers the average requirement
fastcgi_buffers 4 8k; # Covers the maximum: 4 × 8K = 32K

Back to blog