Nginx with Basic Authentication - jonatello/lab-musing GitHub Wiki

https://docs.nginx.com/nginx/admin-guide/security-controls/configuring-http-basic-authentication/

Nginx supports HTTP Basic Authentication and it's a great and simple way to password protect something that is being proxied by Nginx. Assuming Nginx is already configured and working, these steps will describe how to add this additional feature.

Create Password file

A tool like htpasswd can be used to create a file which is used to store the usernames and encrypted passwords. Install the tool with the following (on FreeBSD) using pkg as our package manager

pkg install py27-htpasswd

Create the password file at the specified path with an initial user "test" with the following

htpasswd.py -c /usr/local/etc/nginx/.htpasswd

htpasswd.py /usr/local/etc/nginx/.htpasswd test

Configure Nginx

As a very basic example, we will setup Nginx to require a user/pass from our htpasswd file, but only for our path at /admin.html

location /admin.html { auth_basic “Admins only”; auth_basic_user_file /usr/local/etc/nginx/.htpasswd; }