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


This is an short tutorial how to change http version from 1 to 2 in Tomcat.

Http2 is good in performance compared with 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 to improve performance
  • Need htts configuration as a prerequisite
  • https handshake is not supported in websockets

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

What are the things required to configure http2 in tomcat?

  • SSL must installed with configured Certificates

Configure http2 in tomcat9.0

As per tomcat documentation.

  • HTTP/2 need to configured HTTP connection over TLS
  • UpgradeProtocol tag with className must be configured inside 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 tomcat installation directory or TOMCAT_HOME folder
  • In my System, It is installed at C:\tomcat9.0
  • Go to conf directory
  • Open Server.xml in 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 below command
// windows
shutdown.bat
startup.bat

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

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

You can check using 2 ways

  • 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 All option
  • protocol section shows 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 established and handshake takes most of the time.

And multiple request, HTTP2 is very fast over HTTP1

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

How to add compression to http2 in tomcat?

Some times, We need to add compression of resources using gzip compression format for loading resources, thus improves performances

Please add below properties to UpgradeProtocol in server..xml

  • compressableMimeType
  • compression
  • compressionMinSize
  • useSendfile

Now it loads all compressed resources for each http2 request.

Conclusion

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

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.