Nginx 基于用户信任登陆

2020-01-13  本文已影响0人  Habit_1027

基于用户的信任登录模块:http_auth_basic_module

有时我们会有这么一种需求,就是你的网站的某些页面不希望公开,我们希望的是某些特定的客户端可以访问。那么我们可以在访问时要求进行身份认证,就如给你自己的家门加一把锁,以拒绝那些不速之客。

1、配置语法
Syntax:auth_basic string | off;
default:auth_basic off;
Context:http,server,location

Syntax:auth_basic_user_file file;
default:默认无
Context:http,server,location
file:存储用户名密码信息的文件。
2、配置示例(yum安装的nginx)
[root@yum-n ~]# vim /etc/nginx/conf.d/default.conf

server {
    listen 80;
    server_name localhost;
    location ~ /admin {
        root  /usr/share/nginx/html;
        index index.html index.hml;
        auth_basic "Auth access test!";
        auth_basic_user_file /etc/nginx/auth_conf;
        }
}

[root@yum-n ~]# nginx -t
[root@yum-n ~]# nginx -s reload
image.png

auth_basic不为off,开启登录验证功能,auth_basic_user_file加载账号密码文件。

3、建立口令文件

[root@yum-n ~]# yum install -y httpd-tools #htpasswd 是开源 http 服务器 apache httpd 的一个命令工具,用于生成 http 基本认证的密码文件
[root@yum-n ~]# htpasswd -cm /etc/nginx/auth_conf tom# -c 创建解密文件,-m MD5加密
[root@yum-n ~]# htpasswd -m /etc/nginx/auth_conf jack

设置密码

image.png
[root@yum-n ~]# cat /etc/nginx/auth_conf 
tom:$apr1$s6lZEoqF$yA1ezBWDeVxb89nr0M5iO1
jack:$apr1$7WFzwIMe$RjAmohrt/9HLP3.Tr.y.Q.

4、访问测试

image.png image.png
上一篇 下一篇

猜你喜欢

热点阅读