How to Configure Apache Reverse Proxy With HTTP/2
By: Albin Issac
Tags: Programming, Software Development, Http2, Apache, Technology

Reverse Proxy With HTTP2

Let us now enable the reverse proxy to integrate with the back-end server through the h2 protocol. As a prerequisite, ensure that the back-end server is enabled with the h2 protocol.

As a first step, enable the required proxy modules:

LoadModule proxy_module modules/mod_proxy.so

LoadModule proxy_http2_module modules/mod_proxy_http2.so

Enable these additional proxy configurations into the virtual host:



ServerAdmin

DocumentRoot "C:\Albin\blogData\htdocs"

ServerName myexample.com

ErrorLog "logs/myexample.com-error.log"

CustomLog "logs/myexample.com-access.log" common



SSLProxyEngine on

RewriteEngine on

SSLEngine on



ProxyPass / h2://localhost:8443/

ProxyPassReverse / http://localhost:8443/



SSLCertificateFile C://Albin/blogData/cors/medium/ssl/server.crt

SSLCertificateKeyFile C://Albin/blogData/cors/medium/ssl/server.key



Protocols h2 h2c http/1.1 ServerAdmin webmaster@dummy-host.example.com DocumentRoot "C:\Albin\blogData\htdocs"ServerName myexample.comErrorLog "logs/myexample.com-error.log"CustomLog "logs/myexample.com-access.log" commonRewriteEngine onSSLEngine onSSLCertificateFile C://Albin/blogData/cors/medium/ssl/server.crtSSLCertificateKeyFile C://Albin/blogData/cors/medium/ssl/server.keyProtocols h2 h2c http/1.1 </VirtualHost>

Apache proxies the requests to back-end servers through the h2 protocol. Now the end-to-end flow (browser → Apache → back end) is enabled with the h2 protocol.

Here, the back-end server is enabled with the h2 protocol along with SSL on port 8443. If you need to connect with the back-end server that is enabled with h2 over HTTP (no SSL), then use the h2c protocol instead of h2 to connect with the back-end server.

For the demo, I have used the Tomcat server as the back end. Tomcat server version 9+ enables support for the h2 protocol (the mod_jk Apache module can be directly used to connect with Tomcat over h2, but I used mod_proxy_http2 to show the general scenario).

To enable the h2 protocol in Tomcat, add the configuration below into the conf/server.xml and configure the server.crt and server.key paths. I am using the same cert and key files used in the Apache server:

<Connector port="8443" protocol="org.apache.coyote.http11.Http11AprProtocol"

maxThreads="150" SSLEnabled="true" >

<UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol" />

<SSLHostConfig>

<Certificate certificateKeyFile="conf/server.key"

certificateFile="conf/server.crt"

type="RSA" />

</SSLHostConfig>

</Connector>

Download Apache Portable Runtime (APR). APR is a supporting library for the Apache web server. It provides a set of APIs that map to the underlying operating system. Where the OS does not support a particular function, APR will provide an emulation. The APR module tcnative-1.dll can be downloaded from the following location. Copy tcnative-1.dll to the bin folder of the Tomcat server based on your system architecture.

Enable the configuration below in catalina.bat to include tcnative-1.dll as part of the classpath(modify the existing JAVA_OPTS configuration and enable Djava.library.path as an additional parameter):

set "JAVA_OPTS=%JAVA_OPTS% -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Djava.library.path=C:\Albin\SW\Reverse-Proxy-Tomcat\8080\apache-tomcat-9.0.30\bin"

After the restart, the Tomcat server is now accessible through the h2 protocol over 8443 port with SSL.

The HTTP2 protocol will help us to achieve better performance for websites. This reduces the overall page load time by using advanced features like multiplexing, header compressing, stream priority, and server push. In the next tutorial, we will see how to use the h2 protocol to improve the page load time.

References:

https://www.cloudflare.com/website-optimization/http2/what-is-http2/