有时候有些接口需要配置权限访问,但是有不方便直接在项目中添加,此时可以使用nginx做代理,然后在nginx中做权限验证.
- 创建密码文件
linux下运行:
htpasswd -cm /etc/nginx/htpasswd you_user_name
其中/etc/nginx/htpasswd
为秘钥的存储路径,you_user_name
为账号名
输入密码:
New password:
Re-type new password:
Adding password for user you_user_name
然后就会生成/etc/nginx/htpasswd
文件,如果ubuntu下提示
The program 'htpasswd' is currently not installed. To run 'htpasswd' please ask your administrator to install the package 'apache2-utils'
.可以运行sudo apt-get install apache2-utils -y
安装依赖库
- 修改nginx配置文件
location / {
proxy_pass http://10.0.0.102:5601$request_uri;
#加上下面两行内容:
auth_basic "登陆验证";
auth_basic_user_file /etc/nginx/htpasswd;
}
- 重新加载nginx配置文件nginx
service nginx reload
网友评论