Friday 21 July 2017

AWS - How do I redirect HTTP traffic on my server to HTTPS on my Load Balancer?

This article is merely notes for myself just now. I may turn it into a full article if I get any feedback.

I have a AWS (Amazon Web Service) Free Tier trial account and currently experimenting and going through tutorials. I have set up an Elastic Load Balancer to two Apache2 PHP Webservers (HTTPS) with, a MySQL DB Server. I then put a copy of my bespoke PHP pages, searching my music collection database.

AWS - How do I set a Elastic (fixed) IP for the Load Balancer?

I wanted to set a DNS entry for aws.taurus2.co.uk
You cannot set an Elastic IP for the Load Balancer. Instead, set a CNAME in your DNS to the Load Balancers 'DNS Name'.

i.e.

aws CNAME myloadbalancer-1641756311.eu-west-2.elb.amazonaws.com

AWS - How do I redirect HTTP traffic on my webserver to HTTPS on my Load Balancer?

Next, I wanted to redirect HTTP traffic on my webservers to HTTPS on the Load Balancer.

sudo nano /etc/apache2/apache2.conf

Add or change in the <Directory /var/www/> section

AllowOverride All
sudo nano /var/www/html/.htaccess
RewriteEngine on RewriteCond %{HTTP:X-Forwarded-Proto} ^http$ RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

The above tests the X-Forwarded-Proto header and redirects HTTP to HTTPS, without interfering with ELB health check.

sudo service apache2 reload

You can tail your apache access log with the below which, removes any "ELB-HealthChecker" entries which get in the way.

tail -F /var/log/apache2/access.log | grep -v "ELB-HealthChecker"

References:
https://forums.aws.amazon.com/thread.jspa?messageID=745509

No comments: