使用Meteor-Up、SSL和NGINX部署Meteor到生产环境

7
我在使用meteor-up来将我的meteor应用程序(以下称为“myApp”)部署到生产环境时遇到了困难,使用https和NGINX作为代理。特别是,我认为我在配置正确的端口和/或路径方面遇到了麻烦。
在大多数情况下,部署工作正常。它正在数字海洋水滴上运行,具有mongohq(现在是compose.io)数据库。我的“mup setup”,“mup reconfig”(现在在我的mup.json文件上运行了很多次)和meteor-up的“mup deploy”命令都没有报告错误。如果我通过ssh登录数字海洋上的ubuntu环境并运行“status myApp”,则会报告“myApp start/running, process 10049”,当我检查mongohq数据库时,我可以看到预期的myApp集合已经被创建和填充。因此,我认为该应用程序正在正常运行。
我的问题是,我无法找到该站点,并且在没有使用NGINX服务器的经验的情况下,我无法确定是否设置了端口和转发方面出现了一些基本错误。
我在下面重现了我的NGINX配置文件和mup.json文件的相关部分。
根据下面的设置,我期望的行为是,如果我的meteor应用程序在mup.json中侦听端口3000,则在访问该站点时应该出现该应用程序。实际上,如果我将mup.json的env.PORT设置为3000,则访问站点时我的浏览器告诉我存在重定向循环。如果我将mup的env.PORT更改为80或完全省略env.PORT,则会收到“502 Bad Gateway”消息 - 这部分是可以预料的,因为myApp应该在localhost: 3000上侦听,我不会指望在其他任何地方找到任何东西。
非常感谢所有的帮助。

MUP.JSON(相关部分,如果需要显示更多,请通知我)

"env": {
  "PORT": 3000,
  "NODE_ENV": "production",
  "ROOT_URL": "http://myApp.com",
  "MONGO_URL": // working ok, not reproduced here,
  "MONGO_OPLOG_URL": // working ok I think,
  "MAIL_URL": // working ok
}

NGINX

server_tokens off;

# according to a digital ocean guide i followed here, https://www.digitalocean.com/community/tutorials/how-to-deploy-a-meteor-js-application-on-ubuntu-14-04-with-nginx, this section is needed to proxy web-socket connections

map $http_upgrade $connection_upgrade {
      default upgrade;
      ''      close;
}

# HTTP

server {
      listen 80 default_server;
      listen [::]:80 default_server ipv6only=on;
      server_name myApp.com;
      # redirect non-SSL to SSL
      location / {
              rewrite ^ https://$server_name$request_uri? permanent;
      }
}

# HTTPS

server {
      listen 443 ssl spdy;

      # this domain must match Common Name (CN) in the SSL certificate

      server_name myApp.com;

      root html;
      index index.html index.htm;

      ssl_certificate /etc/nginx/ssl/tempcert.crt;
      ssl_certificate_key /etc/nginx/ssl/tempcert.key;

      ssl_stapling on;
      ssl_session_cache shared:SSL:10m;
      ssl_session_timeout 5m;

      ssl_prefer_server_ciphers on;
      ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
      ssl_ciphers 'long string I didn't reproduce here'

      add_header Strict-Transport-Security "max-age=31536000;";

      location / {
              proxy_pass http://localhost:3000;
              proxy_http_version 1.1;
              proxy_set_header Upgrade $http_upgrade;
              proxy_set_header Connection "upgrade";
              proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
              proxy_set_header Host $host;
              proxy_set_header X-Real-IP $remote_addr;
      }
}

另外请注意,SSL证书已经被配置并且正常工作,因此我认为问题可能出在端口、路径和转发的配置上。我不知道重定向循环是从哪里来的。


难道不应该是 "ROOT_URL": "https://myApp.com" 吗?位置设置看起来没问题 - 我的设置也一样(除了设置 X-Forward)。 - Steffo
4个回答

13

如果将来有人遇到此问题,我成功解决了这个问题,方法是从我的捆绑Meteor应用程序中删除force-ssl软件包。 显然,force-ssl和NGINX代理要么是多余的,要么如果同时使用可能会导致太多重定向。 在我所能找到的材料中,这并没有得到很好的记录。

如果有一种配置支持将force-ssl与提供某些目的并且优于完全删除软件包的代理一起使用,请发布,因为我很感兴趣。 谢谢。


如果您删除了 force-ssl,那么用什么来防止用户访问您的网站 http:// - evolross
2
它会从HTTP重定向到HTTPS - 请参见上面的nginx配置中读取“rewrite ^ https://$ server_name $request_uri?permanent;”的行。 - Jeremy S.
问题在于,使用OP的配置,nginx代理到http://localhost:3000,该端口尝试通过重定向到https://myapp.com来强制使用ssl,然后再代理到http://localhost:3000,这个端口又尝试强制使用ssl,如此循环。 - Mmmh mmh

6
我相信只要您在Nginx配置中添加X-Forwarded-Proto头,就可以保留force-ssl包。
例如:
 proxy_set_header X-Forward-Proto https;

此外,确保你已经设置了X-Forward-For,虽然在你发布的示例中已经包含了它。 来源

2
根据 force-ssl 包的文档,你需要将 x-forwarded-proto 头设置为 https:

因此,你在 nginx 配置中的 location 字段应该如下所示:

location / {
          #your own config...
          proxy_set_header X-Forwarded-Proto https;
  }

1
我是在NGinx代理后运行Meteor。在安装force-ssl后,我遇到了太多重定向的错误。
要解决这个问题,需要移除force-ssl,并在我的nginx配置文件中的位置添加以下几行代码:
proxy_set_header X-Forward-Proto https;
proxy_set_header X-Nginx-Proxy true;

现在完美运作。

如何移除 force-ssl?如果不移除,你的配置将无法正常工作。 - Harrison

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接