Nginx-模块-ngx_http_auth_basic_module【访问认证】

Nginx-学习目录

1、基础

1.1、作用

ngx_http_auth_basic_module模块允许使用HTTP基本身份验证,验证用户名和密码来限制对资源的访问。

1.2、指令介绍

1.2.1、auth_basic-启用或关闭用户和密码认证

# 使用HTTP基本身份验证协议启用用户名和密码验证。
Syntax: auth_basic string | off;
Default: 
auth_basic off;
Context: http, server, location, limit_except

1.2.2、auth_basic_user_file-密码保存的文件

# 指定保存用户名和密码的文件
Syntax: auth_basic_user_file file;
Default: —
Context: http, server, location, limit_except

1.3、密码生成的方法

1.3.1、密码文件格式

# comment
name1:password1
name2:password2:comment
name3:password3

1.3.2、openssl生成

openssl passwd

1.3.3、httpd-tools生成

yum install httpd-tools -y

# 首次增加,需要-c
htpasswd -b -c /etc/nginx/auto_conf test 1234

# 追加用户,不用-c
htpasswd -b /etc/nginx/auto_conf cyc 123

2、实战

2.1、基于用户名和密码认证

2.1.1、配置nginx

cat >/etc/nginx/conf.d/mirror.cyc.com.conf <<'EOF'
server{
  listen 80;
  server_name mirror.cyc.com;

  charset utf-8; 
  root /mnt/;

  location /cdrom {
    autoindex on;
    autoindex_exact_size off;
    autoindex_localtime on;

    auth_basic "Enter Password:";
    auth_basic_user_file /etc/nginx/auto_conf; 

  }

  location / {
    index index.html;
  }
}
EOF

 

2.1.2、重新加载ngnix

systemctl reload nginx

 

2.1.3、测试访问

 

posted @ 2023-04-27 15:20  小粉优化大师  阅读(102)  评论(0编辑  收藏  举报