安装htpasswd工具
# [RHEL/CentOS]
sudo yum install httpd-tools
#[Debian/Ubuntu]
sudo apt install apache2-utils
生成密码文件
htpasswd -c /etc/nginx/authfile username
编辑nginx.conf
添加
auth_basic "closed site";
auth_basic_user_file /etc/nginx/authfile;
重启nginx服务
systemctl restart nginx