LineRate HTTP to HTTPS redirect

Here's a quick LineRate proxy code snippet to convert an HTTP request to a HTTPS request using the embedded Node.js engine. The relevant parts of the LineRate proxy config are below, as well.

By modifying the

redirect_domain
variable, you can redirect HTTP to HTTPS as well as doing a non-www to a www redirect. For example, you can redirect a request for
http://example.com
to
https://www.example.com
. The original URI is simply appended to the redirected request, so a request for
http://example.com/page1.html
will be redirected to
https://www.example.com/page1.html
.

This example uses the self-signed SSL certificate that is included in the LineRate distribution. This is fine for testing, but make sure to create a new SSL profile with your site certificate and key when going to production.

As always, the scripting docs can be found here.


redirect.js:

Put this script in the default scripts directory -

/home/linerate/data/scripting/proxy/
and update the
redirect_domain
and
redirect_type
variables for your environment.

"use strict";

var vsm = require('lrs/virtualServerModule');

// domain name to which to redirect
var redirect_domain = 'www.example.com';

// type of redirect.  301 = temporary, 302 = permanent
var redirect_type = 302;

vsm.on('exist', 'vs_example.com', function(vs) {
  console.log('Redirect script installed on Virtual Server: ' + vs.id);

  vs.on('request', function(servReq, servResp, cliReq) {

    servResp.writeHead(redirect_type, {
        'Location': 'https://' + redirect_domain + servReq.url
    });
    servResp.end();

  });

});


LineRate config:

real-server rs1
 ip address 10.1.2.100 80
 admin-status online
!
virtual-ip vip_example.com
 ip address 192.0.2.1 80
 admin-status online
!
virtual-ip vip_example.com_https
 ip address 192.0.2.1 443
 attach ssl profile self-signed
 admin-status online
!
virtual-server vs_example.com
 attach virtual-ip vip_example.com default
 attach real-server rs1
!
virtual-server vs_example.com_https
 attach virtual-ip vip_example.com_https default
 attach real-server rs1
!
script redirect
 source file "proxy/redirect.js"
 admin-status online 


Example:

user@m1:~/ > curl -L -k -D - http://example.com/test
HTTP/1.1 302 Found
Location: https://www.example.com/test
Date: Wed, 03-Sep-2014 16:39:53 GMT
Transfer-Encoding: chunked

HTTP/1.1 200 OK
Content-Type: text/plain
Date: Wed, 03-Sep-2014 16:39:53 GMT
Transfer-Encoding: chunked

hello world



Updated Jun 06, 2023
Version 2.0

Was this article helpful?

No CommentsBe the first to comment