我想将我的 Meteor 应用程序部署到 Heroku 并使其只能通过 HTTPS 访问。理想情况下,我希望尽可能便宜地做到这一点。
最佳答案
创建证书
运行这些命令以获取 certbot-auto。 certbot-auto 应该适用于大多数系统
wget https://dl.eff.org/certbot-auto
chmod 755 certbot-auto
此命令启动获取证书的过程。 -d
标志允许您传入要保护的域。或者,如果没有 -d
标志,它会弹出一个提示,您可以在其中输入域。
./certbot-auto certonly --manual -d app.yoursite.com
然后它会询问您以下内容。不要按回车键。
Make sure your web server displays the following content at
http://app.yoursite.com/.well-known/acme-challenge/SOME-LENGTHY-KEY before continuing:
SOME-LONGER-KEY
使用选择器
我建议使用此方法,因为在更新时,您只需要更新一个环境变量。您可以按如下方式使用 public/
,但每次都需要重新构建整个应用
运行 meteor add meteorhacks:picker
在服务器端文件中,添加以下内容
import { Picker } from 'meteor/meteorhacks:picker';
Picker.route('/.well-known/acme-challenge/:routeKey', (params, request, response) => {
response.writeHead('200', {'Content-Type': 'text/plain'});
response.write(process.env.SSL_PAGE_KEY)
response.end();
});
然后设置环境变量 SSL_PAGE_KEY
为 SOME-LONGER-KEY
heroku config:set SSL_PAGE_KEY=SOME-LONGER-KEY
使用公共(public)/
在您的 public
文件夹中创建目录路径。如果您没有,请创建一个。
mkdir -p public/.well-known/acme-challenge/
然后创建文件 SOME-LENGTHY-KEY
并将 SOME-LONGTHY-KEY
放入其中
echo SOME-LONGER-KEY > public/.well-known/acme-challenge/SOME-LENGTHY-KEY
提交更改并将其推送到您的 Heroku 应用。
git push heroku master
现在按回车键继续验证过程。您应该会收到这样的消息
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at
/etc/letsencrypt/live/app.yoursite.com/fullchain.pem. Your cert will
expire on 2016-04-11. To obtain a new version of the certificate in
the future, simply run Let's Encrypt again.
上传证书
要将您的证书上传到 Heroku,首先启用 SSL Beta
heroku labs:enable http-sni -a your-app
heroku plugins:install heroku-certs
然后将您的 fullchain.pem
和 privkey.pem
添加到 Heroku。
sudo heroku _certs:add /etc/letsencrypt/live/app.yoursite.com/fullchain.pem /etc/letsencrypt/live/app.yoursite.com/privkey.pem
您可以验证证书是否已上传
heroku _certs:info
更改您的 DNS 设置
更新您的 DNS 以指向 app.yoursite.com.herokudns.com
验证 SSL 是否正常工作
要检查是否已设置 SSL,请运行以下命令。 -v
为您提供详细的输出。 -I
仅显示文档信息。 -H
将 header 传递给 URL。我们传递的 header 确保未使用缓存,并将确保您获得新证书而不是旧证书。
curl -vI https://app.yoursite.com -H "Cache-Control: no-cache"
检查输出是否包含以下内容
* Server certificate:
* subject: C=US; ST=CA; L=SF; O=SFDC; OU=Heroku; CN=app.yoursite.com
如果 subject
行不包含 CN=app.yoursite.com
,请等待 5 到 10 分钟,然后重试。如果是这样,您几乎可以开始了。
进行 Meteor 特定更改
要完成此过程,您需要将 ROOT_URL
环境变量更改为新的 https
版本。
heroku config:set ROOT_URL=https://app.yoursite.com
然后您需要确保您的用户始终使用带有 force-ssl
包的 SSL
meteor add force-ssl
最后,如果您在应用(Facebook、Google 等)中设置了任何 OAuth 登录,您需要为他们提供新的 https
版本的 URL。
更新
再次运行certbot-auto
./certbot-auto certonly --manual -d app.yoursite.com
它可能提示您输入具有相同内容的相同端点。如果是这样,只需按回车键。如果没有,您将需要重复上述步骤。
然后它将创建新的证书文件,您将使用这些文件将其上传到 Heroku
heroku certs:update /etc/letsencrypt/live/app.yoursite.com/fullchain.pem /etc/letsencrypt/live/app.yoursite.com/privkey.pem
然后运行上面的Verify SSL is working命令进行确认
来源
关于ssl - 如何在 Heroku 上部署安全的 (HTTPS) Meteor 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39348340/