I am using both HTTP and HTTPS listeners on my Elastic Load Balancing load balancer, but only HTTP listeners on my back-end web server. I want all traffic coming to my web server on port 80 to be redirected to HTTPS port 443, and I don’t want to change my back-end listener to port 443. When I redirect traffic in this way, my website stops working. How do I resolve this?
Usually, this is due to a process similar to the following:
- The rewrite rule on the web server for directing HTTP requests to HTTPS causes requests to use port 443 for HTTPS traffic on the load balancer.
- The load balancer still sends the requests to the back-end web server on port 80.
- The back end redirects these requests to port 443 on the load balancer.
This causes an infinite loop of redirection between the load balancer and the back-end web server, and the requests are never served.
Using the X-Forwarded-Proto header of the HTTP request, change your web server’s rewrite rule to apply if and only if the client protocol is HTTP. Ignore the rewrite rule for all other protocols used by the client.
This way, if clients use HTTP to access your website, they are redirected to an HTTPS URL, and if clients use HTTPS, they are served directly by the web server.
For example, the rewrite rule for an Apache back end would look similar to the following:
...
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule . https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]
...
HTTP, HTTPS, listener
Did this page help you? Yes | No
Back to the AWS Support Knowledge Center
Need help? Visit the AWS Support Center
Published: 2016-08-12