nginx访问认证+目目录浏览

概述

在实际工作中,企业中有些网站,要求使用账号和密码才能访问,如网站后台、phpMyAdmin 、Wiki 平台 等 模块ngx_http_auth_basic_module 允许使用“HTTP基本认证”协议验证用户名和密码来限制对资源的访问 模块ngx_http_auth_basic_module 下有两条指令 auth_basic 和 auth_basic_user_file

环境

Centos 6.9 

安装epel

wget https://mirrors.aliyun.com/epel/epel-release-latest-6.noarch.rpm  rpm -ivh epel-release-latest-6.noarch.rpm

安装nginx

yum intall -y nginx

创建密钥文件

yum install -y httpd-tools  htpasswd -bc /etc/nginx/conf.d/htpasswd.users username password

注意:username和password,分别对应用名和密码

配置nginx

创建新的配置

vi /etc/nginx/conf.d/browse.conf

内容如下:

server {          listen       81;          server_name  localhost;          location / {                  root   /data/log/tomcat;                  index  index.html index.htm;                  # 设置用于认证的提示字符串                  auth_basic "Restricted Access";                  # 设置认证的密码文件                  auth_basic_user_file  /etc/nginx/conf.d/htpasswd.users;                  #自动显示目录                  autoindex  on;                  #改为off后,显示出文件的大概大小,单位是kB或者MB或者GB;即人性化方式显示文件大小否则以byte显示                  autoindex_exact_size  off;                   autoindex_localtime on;          }  }

重载配置

nginx -s reload

访问页面

输入用户名和密码

效果如下: