net::ERR_HTTP2_PROTOCOL_ERROR – Code Example

Total
0
Shares

Today we will the cause and solution of net::ERR_HTTP2_PROTOCOL_ERROR. This is a http/2 error and could be caused by number of reasons like –

  • Wrong headers
  • Empty headers
  • Wrong content size
  • Multiple times gzipped
  • Filled disk space on server
  • Setting minBytesPerSecond on IIS server
  • Small client_max_body_size value on nginx server
  • Long Base64 Strings
  • Wrong computer clock time

Solutions with Code Examples

1. Wrong Headers

Some examples of wrong headers are –

Content -Length: 2000  # Extra space after Content
Content-Length : 2000  # Space in between Length and :
Contnt-Length: 2000    # Wrong spelling of Content

You need to write the correct headers otherwise you will get net::ERR_HTTP2_PROTOCOL_ERROR or HTTP2_SESSION_RECV_INVALID_HEADER.

2. Empty Headers

Sometimes an empty header can raise this error. The possibility is high when it is an important header like Authorization

{
 'Authorization': ''
}

Do not set Authorization header to be null or empty.

3. Wrong Content Size

In https websites one of the security measures is to check content-length header and match it with the size of webpage. If both of them are equal then the webpage is unaltered otherwise it’s a security issue and browser will throw error. The same thing happens when your server is putting a wrong value in content-length header. So, you need to make sure that the value is correct.

4. Multiple Times Gzipped

Sometimes a response is gzipped multiple times. This could be due to server configuration and plugins. Suppose you are editing an image and using library like imagick. Imagick performed some computations over image and return the gzipped output. Later your Apache or Nginx server gzipped the response again and return it to the client browser.

Now, due to multiple gzip, the actual size of response and content-length will differ. This will lead to net::ERR_HTTP2_PROTOCOL_ERROR as discussed in previous point.

Solution is to either turn off gzip plugin in your server or prevent multiple gzip. Suppose you are setting content-length header like this –

header ('Content-Length:'. Filesize($cache_file));

Then it may give wrong size due to gzip. Turn gzip off by adding this into your .htaccess and keep using content-length header –

SetEnvIfNoCase Request_URI ^ / your_php_file.php no-gzip -vary

Setting gzip off in Nginx –

server {
  ...
  ...
  gzip off;
  proxy_max_temp_file_size 0;
  location / {
    proxy_pass http://127.0.0.1:3000/;
  ....

5. Filled Disk Space on Server

Disk space is a major reason because if it is filled, then server could return broken or half response. This will lead to mismatch in content-length and actual size of page. Point 3 will take effect and browsers will return net::ERR_HTTP2_PROTOCOL_ERROR.

6. Setting minBytesPerSecond on IIS server

There is a setting in IIS server minBytesPerSecond which is helpful in preventing slow drip ddos attack. According to it, a response need to be of at least defined bytes per second. This is set in minBytesPerSecond and by default it is 240. If a response is less than 240 bytes then there will be error. Solution is to set it to 0 –

  • Open IIS Configuration Editor
  • Select system.applicationHost/webLimits in Section
  • Set minBytesPerSecond to 0

7. Update Google Chrome

Sometimes older version of Google Chrome browser could issue errors. It’s best to keep it updated.

8. Small client_max_body_size value on nginx server

If your web pages are larger than the client_max_body_size setting in nginx server, then you will get the error because of partial response. Increase this value to your desired limit. By default it is 1MB.

location /uploads {
    ...
    client_max_body_size 100M;
}

9. Long Base64 Strings

If you have long Base64 image strings and using HTTP/2 cloudflare setting then you may encounter an issue. The solution is to either reduce the strings size or turn off HTTP/2 in cloudflare.

10. Wrong computer time

Your SSL certificate will create issues if the computer time is wrong. If SSL is wrong then browser will issue the error. So, you need to keep the computer clock in sync using OS auto time.

Conclusion

In this article we discussed multiple reasons which could lead to the net::ERR_HTTP2_PROTOCOL_ERROR and provided their solutions. After using these solutions your HTTP2 errors will be resolved.