Ssl – Reverse proxy from apache to tomcat (for https to http)

apache-2.2PROXYreverse-proxyssltomcat

I am trying to front my tomcat installation with Apache 2 webserver. The idea is to let apache handle the SSL/https part and then forward the normal request to the tomcat on same machine running on port 8080.

As mentioned here, I am using the following configuration :

<VirtualHost *:*>
    ProxyPreserveHost On
    ProxyPass / http://127.0.0.1:8080/
    ProxyPassReverse / http://127.0.0.1:8080/

    ServerName my-server-name.com
</VirtualHost>

Listen 443
NameVirtualHost *:443
<VirtualHost *:443>
    SSLEngine On
    SSLCertificateFile /etc/letsencrypt/archive/my-server-name.com/cert-file
    SSLCertificateKeyFile /etc/letsencrypt/archive/my-server-name.com/key-file
    SSLCertificateChainFile /etc/letsencrypt/archive/my-server-name.com/chain-file

    ProxyPass / http://127.0.0.1:8080/
    ProxyPassReverse / http://127.0.0.1:8080/

</VirtualHost>

The above configuration is resulting in :

  1. Visiting http://my-server-name.com is opening the tomcat landing page
  2. Visiting https://my-server-name.com is opening the apache landing page

But what I expect is to always redirect to https://my-server-name.com which should open the tomcat landing page (which will evantually be replaced by my application deployed on the ROOT)

Can someone please guide me or any pointer to a step by step guide to front tomcat with apache for https to http handling

Best Answer

The first vhost isn't needed as it seems and the NameVirtualHost directive can also be dropped, resulting in:

Listen 80    
Listen 443

<VirtualHost *:80>
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^(.*)$
    RewriteRule ^(.*)$ https://%1$1 [R=Permanent,L,QSA]
</VirtualHost>

<VirtualHost *:443>
    SSLEngine On
    SSLCertificateFile /etc/letsencrypt/archive/my-server-name.com/cert-file
    SSLCertificateKeyFile /etc/letsencrypt/archive/my-server-name.com/key-file
    SSLCertificateChainFile /etc/letsencrypt/archive/my-server-name.com/chain-file

    ProxyPreserveHost On
    ProxyPass / http://127.0.0.1:8080/
    ProxyPassReverse / http://127.0.0.1:8080/
</VirtualHost>
Related Topic