为了更好限制访问权限,需要对服务加鉴权,具体步骤如下:
1、安装httpd-tools
yum install -y httpd-tools
2、创建密码文件htpasswd.users
htpasswd -c /etc/nginx/htpasswd.users "账户名"
如果为已存在密码文件添加用户,删除-c
3、配置nginx.conf
Nginx 的配置文件中(通常位于 /etc/nginx/nginx.conf
或 /etc/nginx/sites-available/default
,具体取决于你的安装和配置),找到或添加你想要保护的 location
块,并添加基本认证的指令。
location / {
auth_basic "Restricted Area";
auth_basic_user_file /etc/nginx/htpasswd.users;
...
}
auth_basic "Restricted Area"
设置了认证提示信息,用户在访问受保护的页面时会看到这个提示。
auth_basic_user_file
指令指定了之前创建的包含用户名和加密密码的文件路径。
4、验证修改并加载配置
nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
# 如上信息表明修改没有问题
systemctl reload nginx # 重载NGINX
参考文档
1、https://blog.51cto.com/u_16213644/10887609