使用的是ngx_http_auth_basic_module 模块,nginx 默认自带。效果如下图


使用 htpasswd 生成密码文件

没有该命令需要安装 httpd-tools(centos) 或 apache2-utils(ubuntu)。一般在你敲下命令后系统会提示你应该装什么

root@ubuntu:/home/nginx/conf.d# htpasswd

Command 'htpasswd' not found, but can be installed with:

apt install apache2-utils

使用命令

htpasswd 
    -c 创建新文件
    -d 用 CRYPT密码加密 最多8个字符 不安全
    -s 用 SHA加密 不安全
    -p 明文密码 不加密
    

创建密码文件

htpasswd -d -c ./passwd nick

配置 NGINX

location / {
    ....
    auth_basic "Password";
    auth_basic_user_file ./passwd;  
}