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/passwords

Perfect. Now when we run docker-compose up --build and visit http://localhost/ in the browser we will be prompted to login.