【问题标题】:How can I deploy a secure (HTTPS) Meteor app on Heroku?如何在 Heroku 上部署安全 (HTTPS) Meteor 应用程序?
【发布时间】:2016-09-06 11:49:00
【问题描述】:

我想将我的 Meteor 应用程序部署到 Heroku 并使其只能通过 HTTPS 访问。理想情况下,我想尽可能便宜地做到这一点。

【问题讨论】:

    标签: ssl meteor heroku https ssl-certificate


    【解决方案1】:

    创建证书

    运行这些命令以获取 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 文件夹中创建目录路径。如果您没有,请创建一个。

    mkdir -p public/.well-known/acme-challenge/
    

    然后创建文件SOME-LENGTHY-KEY并将SOME-LONGER-KEY放入其中

    echo SOME-LONGER-KEY > public/.well-known/acme-challenge/SOME-LENGTHY-KEY
    

    提交更改并将其推送到您的 Heroku 应用。

    git push heroku master
    

    现在按 Enter 继续验证过程。你应该会收到这样的消息

    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.pemprivkey.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 将标头传递给 URL。我们传递的标头确保没有使用缓存,并确保您获得新证书而不是旧证书。

    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 登录,您需要向他们提供您的 URL 的新 https 版本。

    续订

    再次运行certbot-auto

    ./certbot-auto certonly --manual -d app.yoursite.com
    

    可能提示您使用相同内容的相同端点。如果是这样,只需按 Enter。如果没有,则需要重复上述步骤。

    然后它将创建新的证书文件,您将使用它上传到 Heroku

    heroku certs:update /etc/letsencrypt/live/app.yoursite.com/fullchain.pem /etc/letsencrypt/live/app.yoursite.com/privkey.pem
    

    然后进行确认,运行上面的验证 SSL 是否工作命令

    来源

    【讨论】:

    • 是的,它是一个测试版功能,但任何想要使用它的人都可以使用它。 blog.heroku.com/…
    • 抱歉,我不得不删除我之前的评论,其中包含我的客户名称,这里是:heroku labs:enable http-sni -a your-app 尚不适用于所有人,因为这是一个测试版功能。相反,您可以在 /etc/letsencrypt/live/yourdomain.com/ 文件夹中获取 fullchain.pemprivkey.pem,然后手动将它们添加到 Heroku 仪表板中
    • 我确认该命令对我不起作用,来自法国,我收到一条消息说此功能不存在或不可用。其他一切都很好,非常感谢这个答案:)
    • 这对我不起作用,收到有关需要运行的警告su: must be run from a terminal
    猜你喜欢
    • 2019-02-04
    • 2016-10-10
    • 1970-01-01
    • 2014-09-24
    • 2014-10-26
    • 2016-05-15
    • 2015-01-08
    • 2017-05-26
    • 2020-07-03
    相关资源
    最近更新 更多