最近部署consul服务,发现consul认证配置比较麻烦,于是上网查询发现nginx支持路由认证,在此做个记录。
1.Nginx访问控制模块类型
- 基于IP的访问控制:
http_access_module
- 基于用户的信任登录:
http_auth_basic_module
2.建立口令文件
[root@monitor12011 conf]# yum -y install httpd-tools
[root@monitor12011 conf]# htpasswd -cm /usr/local/nginx/conf/auth_conf admin //第一次创建用户
New password:
Re-type new password:
Adding password for user admin
# 第二次创建用: htpasswd -m /usr/local/nginx/conf/auth_conf user2
3.用户信用设置
server {listen 80;server_name localhost;# consul注册、注销、页面显示详情location ~ ^/(v1/agent/service|v1/health/service|v1/discovery-chain){proxy_pass http://xx.xx.xx.xx:8500;auth_basic "Auth access test!";auth_basic_user_file /usr/local/nginx/conf/auth_conf;}
}
4.python访问权限路由
requests.put(f"xxxx/v1/agent/service/register",
json=json.loads(params), auth=("admin", "xxx"), verify=False)