This demonstrates adding HTTP Basic Authentication to an nginx container, using instructions from the nginx website.
To run the demo, all you'll need is docker and docker-compose.
To build the container:
docker-compose build
docker-compose up -dTo test the container:
Open up a browser and navigate to http://127.0.0.1:9000/. The default username and password is myusername and mypassword, respectively. It's recommended to use an incognito browser for testing. To reset chrome when testing chrome://restart; but note that this is a complete browser restart and logout...
To generate new passwords, edit the htpasswd that's created when building the container.
# login to the container
docker-compose exec nginx-pw sh
# change a password, view, and exit
htpasswd -B /etc/nginx/conf/htpasswd myusername
cat /etc/nginx/conf/htpasswd
exitTo shutdown containers:
docker-compose downTo integrate with a python, using requests:
import requests
sess = requests.Session()
# 401 authorization required
print(sess.get('http://127.0.0.1:9000').text)
# attach authentication; success!
sess.auth = ('myusername', 'mypassword')
print(sess.get('http://127.0.0.1:9000').text)