1. 安装依赖
apache2-utils (Debian, Ubuntu) 或者 httpd-tools (RHEL/CentOS/Oracle Linux).
2.生成密码
cd <nginx 配置目录> sudo htpasswd -c htpasswd user1 # 添加第一个用户 sudo htpasswd htpasswd user2 # 添加其他用户时不用添加参数 c,因为文件已经存在
3.配置 nginx.conf
server
{
listen 80 default_server;
#listen [::]:80 default_server ipv6only=on;
server_name _;
index index.html index.htm index.php;
root /home/wwwroot/default;
auth_basic "authentication";
auth_basic_user_file htpasswd;
autoindex on;
....
}
配置完成后 reload nginx
效果如下

文章最后修订于 2021年2月19日
评论 (0)