Skip to content

Instantly share code, notes, and snippets.

@caub
Last active October 15, 2018 06:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caub/96f5ec5503353faa247b7cd853928006 to your computer and use it in GitHub Desktop.
Save caub/96f5ec5503353faa247b7cd853928006 to your computer and use it in GitHub Desktop.
https with letsencrypt
// d run -d --name=ok -p 443:6100 -p 80:6000 --workdir=/home/node -v /etc/letsencrypt/:/etc/letsencrypt/:ro -v $PWD/server.js:/home/node/server.js node node server
var https = require("https");
var http = require("http");
var fs = require("fs");
var p="/etc/letsencrypt/live/ulti.pw/";
var hs=https.createServer({
key: fs.readFileSync(p+"privkey.pem")+"",
cert:fs.readFileSync(p+"cert.pem")+"",
ca: fs.readFileSync(p+"chain.pem")+""
}, (req,res) => {
res.write("ok");
res.end();
});
var h=http.createServer((req,res) => {
if (!req.connection.encrypted && (req.method === "GET" || req.method === "HEAD")) {
res.writeHead(308, {location: "https://" + req.headers.host + req.url});
return res.end();
}
res.write("ko");
res.end();
});
hs.listen(process.env.HTTPS_PORT || 6100);
h.listen(process.env.HTTP_PORT || 6000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment