簡體   English   中英

如何使用Lets Encrypt,Node和Express 4設置HTTPS

[英]How to set up HTTPS using Lets Encrypt, Node, and Express 4

我做了什么

我使用certbot證明我擁有我的域,該域生成了幾個.pem文件。 證書在此處列出: https : //certbot.eff.org/docs/using.html#where-are-my-certificates

我發現這篇文章很有意義,並且與我從Googling那里獲得的所有其他信息相匹配,但是當我這樣做並運行節點時,無法使用https連接到我的網站。 Http可以像往常一樣正常工作。

我的服務器是express.js + node.js,並且我沒有使用像nginx這樣的反向代理。 它在Google Cloud Platform的Ubuntu上運行。

相關代碼為:

var http = require('http');
var https = require('https');
var privateKey  = fs.readFileSync('/etc/letsencrypt/live/troywolters.com/privkey.pem', 'utf8');
var certificate = fs.readFileSync('/etc/letsencrypt/live/troywolters.com/fullchain.pem', 'utf8');
var credentials = {key: privateKey, cert: certificate};
var app = express();

// Lots of other express stuff (app.use()'s)

var httpServer = http.createServer(app);
var httpsServer = https.createServer(credentials, app);
httpServer.listen(80);
httpsServer.listen(443);

什么不起作用

當我嘗試使用https://troywolters.com連接到我的網站時,連接超時並且什么也沒有發生。 我究竟做錯了什么?

問題的答案是我的托管平台(Google雲平台)在默認配置中不允許防火牆通過端口443。 運行

gcloud compute firewall-rules create allow-https --description "Incoming https allowed." --allow tcp:443

允許通過端口443傳入流量,並解決了該問題。

感謝Server Fault的Michael Hampton的提示。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM