Skip to content

Instantly share code, notes, and snippets.

@ryanlelek
Last active August 29, 2015 13:56
Show Gist options
  • Save ryanlelek/8796257 to your computer and use it in GitHub Desktop.
Save ryanlelek/8796257 to your computer and use it in GitHub Desktop.
'use strict';
// Firstly, please don't use this in production.
// Use nginx forwarding/upstream instead
// Nginx is way more secure and battle-tested
// ################
// ### Method 1 ###
// ################
// Detect the protocol (HTTP or HTTPS)
// if HTTPS, continue...
var schema = (req.headers['x-forwarded-proto'] || '').toLowerCase();
if (schema === 'https') {
next();
} else {
// Redirect or Deny request
// Example: res.redirect('https://yoursite.com');
}
// ################
// ### Method 2 ###
// ################
// Regular HTTP server
var http = express.createServer();
// Redirect all traffic to HTTPS
http.get('*', function (req, res) {
res.redirect('https://' + req.headers.host + req.url);
}).listen(8080);
// Add these Firewall Rules
// iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080
// iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 443 -j REDIRECT --to-port 3000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment