How to enable http2 in tomcat 9 and 10 | Compression configuration to http2

It is a short tutorial on how to change the HTTP version from 1 to 2 in Tomcat.

Http2 is good in performance compared with the HTTP 1.0 version.

HTTPS2 solves performance problems while loading resources over HTTP with binary format and multiplexing.

Http2 feature

  • binary format
  • multiplexing
  • Supports server push mechanism
  • HPACK header compression algorithm to improve performance
  • Need HTTPS configuration as a prerequisite
  • HTTPS handshake is not supported in WebSockets

Since the tomcat8.5 version tomcat is supporting the HTTP 2 version.

What are the things required to configure http2 in tomcat?

  • SSL must be installed with configured Certificates

Configure http2 in tomcat9.0

As per tomcat documentationđź”—.

  • HTTP/2 needs to configured HTTP connection over TLS
  • UpgradeProtocol tag with className must be configured inside the Connector tag
  • Java8 does support TLS as openSSL-based TLS.
<Connector>
  <UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol" />
</Connector>

Here are the steps to configure HTTP2.1

  • First, Go to the tomcat installation directory or TOMCAT_HOME folder
  • In my System, It is installed at C:\tomcat9.0
  • Go to the conf directory
  • Open Server.xml in a Text editor

Check for Connector element with protocol=“org.apache.coyote.http11.Http11AprProtocol”, Update to following things

<Connector port="443" protocol="org.apache.coyote.http11.Http11AprProtocol"
           maxThreads="150" SSLEnabled="true"
           >
    <UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol" />
    <SSLHostConfig>
        <Certificate certificateKeyFile="conf/myapp-key.pem"
                     certificateFile="conf/myapp-cert.pem"
                     certificateChainFile="conf/myapp-chain.pem"
                     type="RSA" />
    </SSLHostConfig>
</Connector>
  • Save the file
  • Restart or stop and start the tomcat server using the below command
// windows
shutdown.bat
startup.bat

(or)
// Linux
./shutdown.sh
./startup.sh

How to check application is installed with tomcat using the http2 version?

You can check in 2 ways to know http2 is installed in tomcat or not.

  • using browser
  • access logs

In browser access, the application url, Now all your resources are loaded with HTTP2.0 version.

  • Open developer tools
  • select network tabs, choose the All option
  • protocol section shows the http2.0 version as h2
Enable http2 in tomcat

with Access logs You can see all file request accessed information in this file.

 [11/Aug/2021:23:40:12 +0000] "GET / HTTP/2.0" 200 5689
[11/Aug/2021:23:40:12 +0000] "GET /style.min.css HTTP/2.0" 200 2103
[11/Aug/2021:23:40:12 +0000] "GET /java-logo.png HTTP/2.0" 200 1789

Performance Issue with HTTP2 vs HTTP1

HTTP2 can be configured with or without HTTPS, BUt there is no browser support for http2 without HTTPS

For a single request, Http2 is very slow compared with HTTP1 as the connection is established and the handshake takes most of the time.

And multiple requests, HTTP2 is very fast over HTTP1

HTTP2 is very good in performance due to multiplexing and header compression

How to add compression to http2 in tomcat?

Sometimes, We need to add compression of resources using gzip compression format for loading resources, thus improving performances.

Please add the below properties to UpgradeProtocol in server..xml

  • compressableMimeType
  • compression
  • compressionMinSize
  • useSendfile
<UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol" compressableMimeType="text/html,text/xml,text/plain,text/css,text/javascript,application/javascript,application/json" compression="on" compressionMinSize="1024" useSendfile="false" />

Now it loads all compressed resources for each http2 request.

Conclusion

You learned how to enable http2 in tomcat 9.0 and 10 servers. It improves performance compared with http2 and also added compression for loading resources.