server {
listen [::]:80 default_server;
listen 80 default_server;
server_name _;
auth_basic_user_file /etc/nginx/server.htpasswd;
auth_basic "Administrator’s Area";
location /public {
auth_basic off;
proxy_pass http://localhost:9002;
}
location / {
proxy_pass http://localhost:9001;
}
}
# server 01
server {
listen [::]:9001;
listen 9001;
server_name _;
add_header Content-Type "text/plain";
return 200 "hello world: server 01";
}
# server 02
server {
listen [::]:9002;
listen 9002;
server_name _;
add_header Content-Type "text/plain";
return 200 "hello world: server 02";
}
# generate your passwod # htpasswd -c ./server.htpasswd -B username
perl -le '
my $commentary = "testing";
my $username = "username";
my $password = crypt("your-password", "salt-hash");
my $row = "$username:$password:$commentary";
open(my $fh, ">>", "server.htpasswd") or die;
say $fh $row;
close $fh;
print $row;
'
docker run --detach --name nginx-password \
--volume "${PWD}/password.conf:/etc/nginx/conf.d/default.conf" \
--volume "${PWD}/server.htpasswd:/etc/nginx/server.htpasswd" \
--publish "8080:80" \
--workdir "/usr/share/nginx/html/" \
nginx
curl -SsD- -u 'username:your-password' http://localhost:8080/
curl -SsD- http://localhost:8080/
curl -SsD- http://localhost:8080/public
docker stop nginx-password
docker rm nginx-password