hello world, nginx + docker edition (part 3)
I had a lot of fun with the previous Nginx/Docker posts so I thought Iād do it again. Hereās a github repo which will track this project
ā
I found out it is possible to add basic HTTP authentication to an Nginx server right in the config.
In our nginx.conf file letās enable the ngx_http_auth_basic_module right under the server block:
...
server {
auth_basic "Protected Server";
auth_basic_user_file /src/passwords;
... Note thereās a reference to a auth_basic_user_file and we supply the path /src/passwords.
So letās create that file and add a username/password combo so Nginx can authenticate our users.
Nginx doesnāt allow plain text passwords so we have to encrypt. We will use the openssl library and set up our password as LetMeIn with username superuser.
Then we write that combo to the /src/passwords file, separated by a colon.
password=$(openssl passwd -crypt LetMeIn)
echo "superuser:$password" > src/passwordsPerfect. Now when we run docker-compose up --build and visit http://localhost/ in the browser we will be prompted to login.