1.生成密码

printf "user_name:$(openssl passwd -crypt password)\n" >> htpasswd

2.配置 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

效果如下

Nginx 用户认证配置-Jacky's Blog