Redirect Http Url to Https in Elastic beanstalk loadbalancer

若如初见. 提交于 2019-12-11 05:38:48

问题


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 ?


回答1:


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.




回答2:


Found the solution create a config file in the following location

~/workspace/server/
|-- .ebextensions
|   -- httpd
|      -- conf.d
|         -- abc.conf

with the following content

<VirtualHost *:80>
   LoadModule rewrite_module modules/mod_rewrite.so

   RewriteEngine On
   RewriteCond %{HTTP:X-Forwarded-Proto} !https
   RewriteCond %{HTTP_USER_AGENT} !ELB-HealthChecker
   RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

   <Proxy *>
     Require all granted
   </Proxy>

   ProxyPass / http://localhost:8080/ retry=0
   ProxyPassReverse / http://localhost:8080/
   ProxyPreserveHost on

   ErrorLog /var/log/httpd/elasticbeanstalk-error_log

</VirtualHost>

Load mod_rewrite is required in newer Tomcat installations.

Source: https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/configuring-https-httpredirect.html



来源:https://stackoverflow.com/questions/44805261/redirect-http-url-to-https-in-elastic-beanstalk-loadbalancer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!