Search code examples
amazon-web-servicesspring-bootamazon-ec2httpselastic-load-balancer

Redirect Http Url to Https in Elastic beanstalk loadbalancer


I've my spring boot app running on tomcat EC2 instances behind the Loadbalancer, which has configured with Https and internelly using Http.

I want to redirect url requests to HTTP to HTTPS.

I found this document from AWS Support

As it says I need to config the Apache backend with the following config

RewriteEngine On

RewriteCond %{HTTP:X-Forwarded-Proto} =http

RewriteRule . https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]

My Question is where to add these config?

There is another document says i need to add .ebextensions directory to the webapp directory and place configurations there. If that is the case, then what is the directory structure and config file format ?


Solution

  • Create a folder .ebextensions in the root of your project (and make sure it is in the root of the bundle you're uploading to Elastic Beanstalk. Something like this:

    ~/workspace/my-app/
    |-- .ebextensions
    |   -- httpd
    |      -- conf.d
    |         -- myconf.conf
    -- index.jsp
    

    in myconf.conf

    RewriteEngine On
    RewriteCond %{HTTP:X-Forwarded-Proto} =http
    RewriteRule . https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]
    

    There is a whole article on customizing the Java apps running in Elastic Beanstalk Tomcat: http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/java-tomcat-platform.html Have a look for more info.