有时候,需要对在外网的站点做访问时的密码限制,这时候,我们可以使用htpasswd命令,配合nginx即可,如果服务器上没有htpasswd命令,首先安装一下
1 安装
yum install -y httpd-tools
2 生成密码文件
htpasswd -c 文件 用户名
如:
htpasswd -c /tmp/httpasswd consul
生成的文件在/tmp/httpasswd,用户名:consul
敲下命令后,会让你输入密码
3 使用
location / {
proxy_next_upstream http_500 http_502 http_504 error timeout invalid_header;
proxy_redirect off;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Cookie $http_cookie;
proxy_pass http://consul;
#密码认证
auth_basic "Restricted";
auth_basic_user_file /usr/local/openresty/nginx/conf/httpasswd;
}
我这里的/usr/local/openresty/nginx/conf/httpasswd文件所在的目录,是在docker里面的目录
4 查看效果


网友评论