Nginx 访问认证
Nginx认证模块,auth_basic 、auth_basic_user_file
auth_basic:off | string
location / { auth_basic 'string'; auth_basic_user_file conf/htpasswd; }
完整实例如下:
server { listen 80; server_name learn_nginx.com; charset utf-8; location / { root /www/vod; index index.html index.htm; auth_basic "string"; auth_basic_user_file conf/htpasswd; } }
linux提供了密码生成命令
htpasswd 是apache提供的密码生成工具 nginx也支持auth_basic 模块,因此我们可以利用Htpasswd 命令生成账号密码文件,提供给nginx使用
# 安装
yum install httpd-tools -y
# 语法
htpasswd -bc .access username password
# 解释
-b 在命令行中输入账号密码 -c 创建密码文件 username 账号 password 密码 默认.access 文件采用加密方式md5验证
实验:
1、准备一个用于练习的配置文件,注意,还未制定密码文件
server { listen 88; # 注意 如果是线上服务器 需要在管理平台规则中开放端口 server_name _; location / { root html/www; index index.html; auth_basic "learn nginx auth_module"; auth_basic_user_file /usr/local/nginx/conf/extra/htpasswd; } }
2、生成密码文件
[root@VM-4-2-centos extra]# htpasswd -bc ./htpasswd chaoge 666 Adding password for user chaoge
3、访问页面即提示输入密码
http://42.192.180.238:88/ 用户名 chaoge 密码 666