密码保护AWS Node EB应用程序

时间:2014-07-10 17:25:49

标签: node.js amazon-web-services elastic-beanstalk

我已启动并运行Node Elastic Beanstalk应用程序(尽管是ELB)。现在它只是服务器上的AWS示例节点应用程序。由于这是一个开发服务器,在我将实际代码推送到服务器之前,我需要密码保护整个事物(这用于客户端审查等)。

我在试图弄清楚如何做到这一点时遇到了很多麻烦。似乎应用程序代码被删除/var/app/并且/var/www/html/(没有隐藏文件)中没有任何内容我通常会设置htaccess文件。它使用的是我从未使用过的nginx代理,而且我不确定文件是如何提供的。

锁定此服务器的最佳方法是什么?安全组? htaccess的?还有别的吗?

3 个答案:

答案 0 :(得分:17)

在Node.js Beanstalk应用程序中,您的实例将其/etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf设置如下:

server {
    listen 8080;

    location / {
        proxy_pass  http://nodejs;
        proxy_set_header   Connection "";
        proxy_http_version 1.1;
        proxy_set_header        Host            $host;
        proxy_set_header        X-Real-IP       $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    gzip on;
}

你想要的是这样设置:

server {
    listen 8080;

    location / {
        proxy_pass  http://nodejs;
        proxy_set_header   Connection "";
        proxy_http_version 1.1;
        proxy_set_header        Host            $host;
        proxy_set_header        X-Real-IP       $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        auth_basic "Restricted";
        auth_basic_user_file /etc/nginx/.htpasswd;
    }
    gzip on;
}

在/etc/nginx/.htpasswd使用单独的密码文件,其中包含您的登录凭据。

第1步: 转到本地linux环境并输入

sudo htpasswd -c .htpasswd someusernameofyourchoice

导航到.htpasswd文件并提取您生成的用户名和密码行。它看起来像这样:

someusernameofyourchoice:$apr1$.1EAU7DD$rt9jdihy1U.cFuBzJTMed.

第2步:

现在在节点应用程序的根目录中(.git /目录所在的位置)创建一个名为.ebextensions /

的隐藏目录

导航到.ebextensions /目录,因为您需要制作2个文件。

第3步:

第一个文件是将在beanstalk应用程序上生成.htpasswd文件的配置文件。将之前生成的用户名和密码哈希放入此文件中,并按如下方式命名:

00_nginx_htpasswd.config

files:
  "/etc/nginx/.htpasswd" :
    mode: "000755"
    owner: root
    group: root
    content: |
      someusernameofyourchoice:$apr1$.1EAU7DD$rt9jdihy1U.cFuBzJTMed.

第4步:

您将在.ebextensions /目录中创建的第二个文件将更新弹性beanstalk环境中的00_elastic_beanstalk_proxy.conf文件。名称如下:

01_nginx_auth.config

files:
  /tmp/deployment/nginx_auth.sh:
    mode: "000755"
    content: |
        sed -i 's/$proxy_add_x_forwarded_for;/$proxy_add_x_forwarded_for;\n     auth_basic "Restricted";\n  auth_basic_user_file \/etc\/nginx\/.htpasswd;\n/' /tmp/deployment/config/#etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf
container_commands:
  nginx_auth:
    command: "/tmp/deployment/nginx_auth.sh"

注意:如果您只希望密码保护在特定环境中,例如您的开发环境。您可以将环境变量传递到您的环境中(在beanstalk仪表板上的配置> Software Configurations面板中),然后您可以向此文件的命令添加条件,该命令在运行之前检查该环境变量。这样,您可以使用密码保护您的开发环境,同时让您的生产环境免费供公众访问。当您将所有内容都放入git以将其推送到beanstalk环境时,这非常方便。以下是带有这些新增内容的修改文件:

01_nginx_auth.config

files:
  /tmp/deployment/nginx_auth.sh:
    mode: "000755"
    content: |
      if [ "$NODE_ENV" == "development" ]; then
        sed -i 's/$proxy_add_x_forwarded_for;/$proxy_add_x_forwarded_for;\n     auth_basic "Restricted";\n  auth_basic_user_file \/etc\/nginx\/.htpasswd;\n/' /tmp/deployment/config/#etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf
      fi
container_commands:
  nginx_auth:
    command: "/tmp/deployment/nginx_auth.sh"

第5步:

在.ebextensions /目录中创建这两个文件后,提交它们并将它们推送到弹性beanstalk。现在应该提示您输入在步骤1中生成的用户名和密码组合。

答案 1 :(得分:3)

安全组只会根据源IP进行阻止,而nginx不支持htaccess。相反,他们支持这样的配置:

server {
    ...
    auth_basic "closed website";
    auth_basic_user_file conf/htpasswd;
}

但要实现这一点,您需要使用弹性beanstalk .ebextensions来修改默认的nginx配置。这根本不容易。

最快的方法可能是在节点应用程序本身内支持HTTP身份验证。有很多指南,但这里有一个:http://www.sitepoint.com/http-authentication-in-node-js/

答案 2 :(得分:0)

如果您使用的是[JsonSpecifiedProperty(string methodName)],则可以使用Express.js添加轻量级中间件。在我的情况下,我所需要的只是一个用户名和密码来锁定公众的网站。在Elastic Beanstalk上使用Node.js服务器时,这是该方案最简单的解决方案。

var auth = require('basic-auth');

app.use(function(req, res, next) {
  var credentials = auth(req);

  if (!credentials || credentials.name !== 'buster' || credentials.pass !== 'getinfree') {
    res.statusCode = 401;
    res.setHeader('WWW-Authenticate', 'Basic realm="example"');
    res.end('Access denied.');
  } else {
    next();
  }
});

请注意,此解决方案需要在您的Node应用中添加代码,而不是直接干扰nginx。

相关问题