Redirect http to https in Tomcat under Elastic BeanStalk

If you want to redirect all HTTP requests to HTTPS , then this is something that is done at the server level; in this case it will be handled by Apache Tomcat. It is assumed that a valid https domain is already configured and the web app is running on it.

We need to create an .ebextensions folder under which there will be a custom server.xml which will do the redirection. Within .ebextensions there has to be a fixed folder structure as shown below:


Under this path we create a file called elasticbeanstalk.conf with the following contents:

<VirtualHost *:80>
   LoadModule rewrite_module modules/
   RewriteEngine On
   RewriteCond %{HTTP:X-Forwarded-Proto} !https
   RewriteCond %{HTTP_USER_AGENT} !ELB-HealthChecker
   RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
   <Proxy *>
     Require all granted
   ProxyPass / http://localhost:8080/ retry=0
   ProxyPassReverse / http://localhost:8080/
   ProxyPreserveHost on
   ErrorLog /var/log/httpd/elasticbeanstalk-error_log

As of March 2021, Beanstalk expects the .ebextensions folder to be either under webapp or under webapp/WEB-INF . When you create a WAR file open it once to confirm that the .ebextensions folder exists in the root. Once you deploy it on AWS, then all http requests to the domain will get redirected to https.

Be the first to comment

Leave a Reply

Your email address will not be published.