This answer assumes that you have already enabled https in the load balancer security group, added the SSL certificate to the load balancer, added 443 to the ports redirected by the load balancer, and entered your domain name in Elastic Beanstalk using Route 53 (or equivalent DNS) .
All you have to do is add the following to one of the .conf files in the .ebextensions directory of your project :
files: "/etc/httpd/conf.d/ssl_rewrite.conf": mode: "000644" owner: root group: root content: | RewriteEngine On <If "-n '%{HTTP:X-Forwarded-Proto}' && %{HTTP:X-Forwarded-Proto} != 'https'"> RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L] </If>
Description
It is moderately right outside the elastic beanstalk. Typically an Apache rewrite rule is added, for example:
RewriteCond %{HTTPS} off RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
Or, if behind the load balancer, as in this case:
RewriteCond %{HTTP:X-Forwarded-Proto} !https RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
However, these configurations only work in the <VirtualHost> block. Changing the RewriteCond block to <If> allows us to work correctly outside the <VirtualHost> block, which allows us to insert a standalone Apache configuration file. Please note that the standard Apache installation on CentOS (including the setting on ElasticBeanstalk) includes all files corresponding to /etc/httpd/conf.d/*.conf , which correspond to the path of the file in which we store this file.
Part of the condition -n '%{HTTP:X-Forwarded-Proto}' prevents redirection if you are not behind a load balancer, which allows you to have a common configuration between a production environment with a load balancer and https and an intermediate environment, which is a single instance and does not have https. This is not necessary if you use load balancers and https in all your environments, but it doesnβt hurt.
Bad decisions I've seen
I have seen many bad solutions to this problem, and it is worth reading them to understand why this solution is necessary.
Use Cloudfront:. Some people suggest using Cloudfront's non-cached configuration before Elastic Beanstalk to redirect HTTP to HTTPS. This adds a whole new service (which adds complexity), which is not entirely suitable (Cloudfront is a CDN, it is not the right tool to force HTTPS for inherited dynamic content). Apache configuration is the normal solution to this problem, and Elastic Beanstalk uses Apache so we can go.
SSH to the server and ...: This completely contradicts the Elastic Beanstalk point and has so many problems. Any new instances created by autoscaling will not have a modified configuration. Any cloned environment will not be configured. Any amount of a reasonable set of environmental changes will destroy the configuration. This is such a bad idea.
Overwrite Apache configuration with a new file: This falls into the correct area of ββthe solution, but leaves you with a maintenance nightmare if Elastic Beanstalk changes server configuration aspects (which they can very well do). Also see Problems in the next item.
Dynamically edit the Apache configuration file to add a few lines: This is a good idea. The problem with this is that it will not work if Elastic Beanstalk ever changes the name of its default Apache configuration file and that this file can be overwritten when you least expect it: https: //forums.aws.amazon. com / thread.jspa? threadID = 163369
Zags Oct 07 '16 at 18:23 2016-10-07 18:23
source share