密码保护AWS Node EB应用程序

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

我有很多麻烦试图找出如何做到这一点。 看起来应用程序代码被放在/var/app/并且在/var/www/html/ (没有隐藏的文件)中没有任何东西,我通常会设置一个htaccess文件。 这是使用nginx代理,我从来没有使用过,我不确定如何提供文件。

locking这个服务器的最好方法是什么? 安全组? htaccess的? 别的东西?

安全组只会根据源IP进行阻塞,而ntax不支持htaccess。 相反,他们支持这样的configuration:

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

但是为了达到这个目的,你需要使用弹性beanstalk的.ebextensions来修改默认的nginxconfiguration。 这并不容易。

对你来说最快的方法可能是在你的节点应用本身中支持HTTPauthentication。 有很多的指导,但这里是一个: http : //www.sitepoint.com/http-authentication-in-node-js/

在您的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中有一个单独的密码文件,它包含了您的login凭证。

第1步:转到您的本地Linux环境并input

 sudo htpasswd -c .htpasswd someusernameofyourchoice 

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

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

第2步:

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

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

第3步:

第一个文件将会是在你的beanstalk应用程序中生成你的.htpasswd文件的configuration文件。 将之前生成的用户名和密码散列放到这个文件中并命名如下:

00_nginx_htpasswd.config

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

步骤4:

您将在.ebextensions /目录中创build的第二个文件将更新弹性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" 

注意:如果您只希望密码保护在某个环境(如您的开发环境)上。 您可以将环境variables传递到您的环境中(在beanstalk仪表板的“configuration”>“软件configuration”面板中),然后可以为该文件的命令添加一个条件,在运行之前检查该环境variables。 这样,您可以密码保护您的开发环境,同时让您的生产环境免费供公众访问。 当你把所有东西都放到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 /目录中创build了这两个文件,就提交它们并将它们推送到弹性beanstalk。 现在应提示您input在步骤1中生成的用户名和密码组合。

如果您使用Express.js ,则可以使用basic-auth软件包添加轻量级中间件。 就我而言,我只需要一个用户名和密码来locking公众的网站。 在Elastic Beanstalk上使用Node.js服务器时,这是该场景的最简单的解决scheme。

 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(); } }); 

注意这个解决scheme需要在Node应用程序中添加代码,而不是直接干扰nginx。