In your Node.js Beanstalk application, your instances will have their own /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf file as follows:
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; }
What you need is to configure it as follows:
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; }
With a separate password file in /etc/nginx/.htpasswd that contains your credentials.
STEP 1: Go to the local linux environment and enter
sudo htpasswd -c .htpasswd someusernameofyourchoice
go to this .htpasswd file and pull out the username and password string that you created. It will look something like this:
someusernameofyourchoice:$apr1$.1EAU7DD$rt9jdihy1U.cFuBzJTMed.
STEP 2:
Now in the root directory of your node application (where your .git / directory is located) create a hidden directory called .ebextensions /
go to the .ebextensions / directory, since you will need to make 2 files.
STEP 3:
The first file will be the configuration file that your .htpasswd file will generate in your beanstalk application. Put the username and password that you created earlier in this file and name it like this:
00_nginx_htpasswd.config
files: "/etc/nginx/.htpasswd" : mode: "000755" owner: root group: root content: | someusernameofyourchoice:$apr1$.1EAU7DD$rt9jdihy1U.cFuBzJTMed.
STEP 4:
The second file you create in your .ebextensions / directory will update the 00_elastic_beanstalk_proxy.conf file on your elastic beanstalk environment. The name is as follows:
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"
NOTE. If you want password protection to be in a specific environment, such as a development environment. You can transfer the environment variable to your environment (in the configuration panel> Software configurations in the beanstalk toolbar), and then you can add a condition to this file command, which checks this environment variable before running it. In this way, you can password protect your development environment by leaving the free working environment for public access. Since you put everything in git to push it to your beanstalk environment, this is very convenient. The following is a modified file with these additions:
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/
STEP 5:
Once you have both of these files created in your .ebextensions / directory, copy them and push on your elastic beanstalk. You will now be prompted to enter the username and password combination generated in step 1.