安装httpd-tools, 内部有工具htpasswd,可用于生成用户名及密码
yum install httpd-tools
# 创建密码文件,并向里面添加一个用户“xxx_user”
htpasswd -c /etc/nginx/conf.d/murd.passwd xxx_user
# 向密码文件中追加一个用户"xxx_user", 密码为“xxx_password”
htpasswd -b /etc/nginx/conf.d/murd.passwd xxx_user xxx_password
配置Nginx
vim /etc/nginx/conf.d/default.conf
server {
listen 80;
server_name localhost;
auth_basic "hurd";
auth_basic_user_file /etc/nginx/conf.d/hurd.passwd;
proxy_buffering off;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /data/hurd/static/;
index index.html;
}
测试刚刚修改过的配置文件
nginx -t
在刚开始时遇到一个问题"invalid number of arguments in "auth_basic_user_file" directive", 最后发现是新增的第二行忘记添加分号导致的,如下第二行没有加分号结尾:
auth_basic "hurd";
auth_basic_user_file /etc/nginx/conf.d/hurd.passwd
测试通过后,尝试让文件生效
nginx -s reload
输入地址测试看是否访问门户网站提示输入用户名及密码
正确的效果示意图如下:
![](https://img.haomeiwen.com/i4686333/4097c52a70c0974a.png)
网友评论