Rackspace Cloudvent Calendar Day 10: Redirect HTTP To HTTPS On Your Cloud Load Balancer

In the spirit of the holiday season, I’m sharing some tips and features of our cloud that you may not know about. In this countdown I’ll have 24 posts for you, each covering a single topic or feature about Rackspace Cloud products. We’ll post one per day so keep checking back!

When I worked in support, customers would frequently ask me to create a redirect to HTTPS in their .htaccess files. For the record, here’s how to do that:

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]

Rewrites using mod_rewrite on a web server is expensive from a processing perspective, as each request has to be evaluated to see if it should be redirected. If you want your entire site to be secure anyway, I recommend setting this up at the load balancer. Each request for HTTP will get a 301 redirect to the same resource but on HTTPS. Save the following JSON as lb.json, editing it with sane values for your scenario:

{
    "loadBalancer": {
        "name": "a-new-loadbalancer",
        "port": 443,
        "protocol": "HTTPS",
  "httpsRedirect": true
        "virtualIps": [
            {
                "type": "PUBLIC"
            }
        ],
        "nodes": [
            {
                "address": "10.1.1.1",
                "port": 80,
                "condition": "ENABLED"
            }
        ]
    }
}

Notice the httpsRedirect set to true? That’s where the magic happens. Now we just need to submit that JSON in a POST request to the Load Balancer API:

curl –s –X POST –d lb.json –H “Content-Type: application/json” –H “X-Auth-Token: $TOKEN” https://ord.loadbalancers.api.rackspacecloud.com/v1.0/1234/loadbalancers

Congrats! You’ve created a load balancer that will redirect insecure traffic to HTTPS.

Check back tomorrow for another tip! And be sure to take a look at the previous entries in the Rackspace Cloudvent Calendar.

  • Vamsi Krishna

    Hi ! thanks for the article. where to save the lb.json file ?

  • Your JSON appears to be missing a comma after the word true on this line:

    “httpsRedirect”: true

    Additionally, I get HTTP code 400: Invalid Response with the following error:

    {“messages”:[“JSON does not match the expected schema”]}

    Any ideas?

  • For those who stumble on this post in the future, PyRax 1.6.4+ now has an option to tweak this using a simple python script as well:

    lb.update(httpsRedirect=True)