将 letsencrypt 与 Node Express 应用程序一起使用
Using letsencrypt with Node Express application
我正在尝试通过我的应用程序切换到 https。我想使用 Letsencrypt,但我在网上看到的所有教程都指出这需要单独的代理设置,以固定的时间间隔更新证书。我最近发现 greenlock-express 似乎将续订过程烘焙到我的快速应用程序的 https 包装中,所以我不需要设置一个单独的服务器作为续订代理(或者我误解了它的目的?)。
这是我目前拥有的:
// Express
import * as http from 'http';
import * as https from 'https';
import * as e from 'express';
const LEX = require('greenlock-express');
/** Run this using MyServer.Initialize(process.argv) **/
export class MyServer {
public app: e.Express;
private clientPath = path.join(__dirname, './public');
static Initialize(args: string[]): Promise<any> {
return new MyServer()
.start(args)
.catch((error: any) => console.error(error);
}
constructor() { }
start(args: string[]): Promise<https.Server> {
// Simplified the code here, in order to only display relevant info
return Promise.resolve(this.createServer());
}
public createServer(): https.Server {
this.app = e(); // Creates an express application
// Setup routes and standard stuff, which I removed to keep this question simple
// Setup base route to everything else
this.app.get('/*', (req: e.Request, res: e.Response) => {
res.sendFile(path.resolve(this.clientPath, 'index.html'));
});
现在,如果我在此处 return 以下内容,应用程序可以正常启动并响应 http://localhost。
return this.app.listen(80)
.on('listening', () => console.log(`Serving on http://localhost/`));
但是如果我改为尝试像这样在我的应用程序周围包装 greenlock-express 东西(这是来自 https://www.npmjs.com/package/greenlock-express 给出的示例,所以我有点失望,因为我无法将它开箱即用):
const lex = LEX.create({
server: 'staging',
email: 'my.email@mailprovider.com',
agreeTos: true,
configDir: 'cert/',
approveDomains: ['mydomain.org']
});
// Force redirect to https
http.createServer(lex.middleware(require('redirect-https')())).listen(80, function () {
console.log('Listening for ACME http-01 challenges on', this.address());
});
return <https.Server> https.createServer(lex.httpsOptions, lex.middleware(this.app))
.listen(443, () => console.log(`Serving on http://localhost/`));
}
}
没有任何效果。我确实从 http://localhost 重定向到 https://localhost,但在那之后,浏览器放弃并声明 The site cannot be reached
.
为什么?我是不是误会了什么?
编辑
我知道 letsencrypt 可能无法将 localhost 解析为 my 域。我的应用程序确实有一个域,我只是不想部署我不是 100% 确定有效的东西。我希望应用程序可以从本地主机运行和测试。这就是我的想法
server: 'staging'
部分 greenlock-express 配置用于。
是的,我想你可能有。 Letsencrypt 为域和子域(我相信最多 10 个)提供免费的、短期的、经过验证的 SSL 证书。您要做的是让 LetsEncrypt 服务器向您的服务器 http://localhost/.well-known/acme-challenge to verify you own the domain. Lets Encrypt will never resolve http://localhost 处的应用程序发出请求。您需要 运行 将此代码和模块安装在可以从您拥有的域上的 public 互联网访问的服务器上。然后 LetsEncrypt 将能够访问您的应用程序、验证域并颁发证书。然后您的站点将 运行 在 SSL 下。那个,还是我误会了什么!
没有本地主机
您不能将 localhost 与 Greenlock™ 或任何 Let's Encrypt™ 客户端一起使用。
您必须通过域访问该站点。
生产阶段
staging
选项适用于面向 public 的生产环境。如果您想确保它在现有站点上部署之前可以正常工作,您可以使用 test.mysite.com
和 www.test.mysite.com
等测试域。
新版本、视频教程、更新文档
我建议您查看最新版本,您可能会发现它更容易上手:
我正在尝试通过我的应用程序切换到 https。我想使用 Letsencrypt,但我在网上看到的所有教程都指出这需要单独的代理设置,以固定的时间间隔更新证书。我最近发现 greenlock-express 似乎将续订过程烘焙到我的快速应用程序的 https 包装中,所以我不需要设置一个单独的服务器作为续订代理(或者我误解了它的目的?)。
这是我目前拥有的:
// Express
import * as http from 'http';
import * as https from 'https';
import * as e from 'express';
const LEX = require('greenlock-express');
/** Run this using MyServer.Initialize(process.argv) **/
export class MyServer {
public app: e.Express;
private clientPath = path.join(__dirname, './public');
static Initialize(args: string[]): Promise<any> {
return new MyServer()
.start(args)
.catch((error: any) => console.error(error);
}
constructor() { }
start(args: string[]): Promise<https.Server> {
// Simplified the code here, in order to only display relevant info
return Promise.resolve(this.createServer());
}
public createServer(): https.Server {
this.app = e(); // Creates an express application
// Setup routes and standard stuff, which I removed to keep this question simple
// Setup base route to everything else
this.app.get('/*', (req: e.Request, res: e.Response) => {
res.sendFile(path.resolve(this.clientPath, 'index.html'));
});
现在,如果我在此处 return 以下内容,应用程序可以正常启动并响应 http://localhost。
return this.app.listen(80)
.on('listening', () => console.log(`Serving on http://localhost/`));
但是如果我改为尝试像这样在我的应用程序周围包装 greenlock-express 东西(这是来自 https://www.npmjs.com/package/greenlock-express 给出的示例,所以我有点失望,因为我无法将它开箱即用):
const lex = LEX.create({
server: 'staging',
email: 'my.email@mailprovider.com',
agreeTos: true,
configDir: 'cert/',
approveDomains: ['mydomain.org']
});
// Force redirect to https
http.createServer(lex.middleware(require('redirect-https')())).listen(80, function () {
console.log('Listening for ACME http-01 challenges on', this.address());
});
return <https.Server> https.createServer(lex.httpsOptions, lex.middleware(this.app))
.listen(443, () => console.log(`Serving on http://localhost/`));
}
}
没有任何效果。我确实从 http://localhost 重定向到 https://localhost,但在那之后,浏览器放弃并声明 The site cannot be reached
.
为什么?我是不是误会了什么?
编辑
我知道 letsencrypt 可能无法将 localhost 解析为 my 域。我的应用程序确实有一个域,我只是不想部署我不是 100% 确定有效的东西。我希望应用程序可以从本地主机运行和测试。这就是我的想法
server: 'staging'
部分 greenlock-express 配置用于。
是的,我想你可能有。 Letsencrypt 为域和子域(我相信最多 10 个)提供免费的、短期的、经过验证的 SSL 证书。您要做的是让 LetsEncrypt 服务器向您的服务器 http://localhost/.well-known/acme-challenge to verify you own the domain. Lets Encrypt will never resolve http://localhost 处的应用程序发出请求。您需要 运行 将此代码和模块安装在可以从您拥有的域上的 public 互联网访问的服务器上。然后 LetsEncrypt 将能够访问您的应用程序、验证域并颁发证书。然后您的站点将 运行 在 SSL 下。那个,还是我误会了什么!
没有本地主机
您不能将 localhost 与 Greenlock™ 或任何 Let's Encrypt™ 客户端一起使用。
您必须通过域访问该站点。
生产阶段
staging
选项适用于面向 public 的生产环境。如果您想确保它在现有站点上部署之前可以正常工作,您可以使用 test.mysite.com
和 www.test.mysite.com
等测试域。
新版本、视频教程、更新文档
我建议您查看最新版本,您可能会发现它更容易上手: