nginx实现密码验证获取目录浏览功能

一、nginx密码验证功能

nginx 的验证格式

nginx的认证模块指令,语法:
location / {

auth_basic "string"; 可以填写off或是string
auth_basic_user_file conf/htpasswd;
}

 

 


例如:

server {

listen 80;
server_name _;
location / {
root html/www;
index index.html;
auth_basic "learn nginx auth_module";
#nginx会去这个文件中验证账号密码
auth_basic_user_file /home/Learn_Nginx/nginx/conf/extra/htpasswd;
}
}

重启nginx
nginx -s reload

 

 


htpasswd生成密码功能

htpasswd是Apache密码生成工具,Nginx支持auth_basic认证,因此我门可以将生成的密码用于Nginx中,输入一行命令即可安装:yum -y install httpd-tools ,参数如下:

-c 创建passwdfile.如果passwdfile 已经存在,那么它会重新写入并删去原有内容.
-n 不更新passwordfile,直接显示密码
-m 使用MD5加密(默认)
-d 使用CRYPT加密(默认)
-p 使用普通文本格式的密码
-s 使用SHA加密
-b 命令行中一并输入用户名和密码而不是根据提示输入密码,可以看见明文,不需要交互
-D 删除指定的用户

#接认证文件,htpasswd -bc .access username password #在当前目录生成.access文件,用户名username,密码:password,默认采用MD5加密方式。

 

生成密码文件

[root@linux extra]# htpasswd -bc ./htpasswd zheng 123456

Adding password for user zheng

 

 

 


 

二、配合nginx目录浏览功能

location /download
{
    root /data; #指定目录所在路径
    autoindex on; #开启目录浏览
    autoindex_format html; #以html风格将目录展示在浏览器中
    autoindex_exact_size off; #切换为 off 后,以可读的方式显示文件大小,单位为 KB、MB 或者 GB
    autoindex_localtime on; #以服务器的文件时间作为显示的时间
}

如果出现乱码的情况

charset utf-8,gbk; #展示中文文件名

 

完整展示

location /download
{
    root /data; #指定目录所在路径
    autoindex on; #开启目录浏览
    autoindex_format html; #以html风格将目录展示在浏览器中
    autoindex_exact_size off; #切换为 off 后,以可读的方式显示文件大小,单位为 KB、MB 或者 GB
    autoindex_localtime on; #以服务器的文件时间作为显示的时间
    charset utf-8,gbk; #展示中文文件名
}

 

posted @ 2022-05-16 14:38  不积硅步,无以至千里  阅读(416)  评论(0编辑  收藏  举报