nginx 服务加鉴权

为了更好限制访问权限,需要对服务加鉴权,具体步骤如下:
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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值