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

 

访问页面

http://192.168.31.216:81

输入用户名和密码

效果如下:

文本参考链接:
https://www.cnblogs.com/wushuaishuai/p/9361811.html
https://blog.csdn.net/haigenwong/article/details/84477218
https://www.cnblogs.com/silent2012/p/8377837.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值