Nginx 添加 HTTP 基本认证 (HTTP Basic Authentication)

CentOS 7 安装 Nginx

1
2
$ sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
$ sudo yum install -y nginx

生成密码

1
$ printf "your_username:$(openssl passwd -crypt your_password)\n" >> /etc/nginx/conf.d/passwd

your_username & your_password 替换成你的用户名密码

配置 Nginx

1
2
3
4
5
location / {
auth_basic "your website name";
auth_basic_user_file conf.d/passwd;
autoindex on;
}

配置完成重启 nginx 即可

----本文结束 感谢您的阅读----