Nginx13-下载站点与用户认证
1、Nginx制作下载站点
(1)下载站点是什么
- 我们先来看一个网站nginx.org/download/这个我们刚开始学习Nginx的时候给大家看过,该网站主要就是用来提供用户来下载相关资源的网站,就叫做下载网站。
(2)如何制作一个下载站点
-
nginx使用的是模块ngx_http_autoindex_module来实现的(内置),该模块处理以斜杠("/")结尾的请求,并生成目录列表。
-
nginx编译的时候会自动加载该模块,但是该模块默认是关闭的,我们需要使用下来指令来完成对应的配置。
(3)相关指令
- **autoindex:**启用或禁用目录列表输出。
语法 | autoindex on|off; |
---|---|
默认值 | autoindex off; |
位置 | http、server、location |
- **autoindex_format:**设置目录列表的格式。
- 注意:该指令在1.7.9及以后版本中出现。
语法 | autoindex_format html|xml|json|jsonp; |
---|---|
默认值 | autoindex_format html; |
位置 | http、server、location |
-
**autoindex_exact_size:**对应HTLM格式,指定是否在目录列表展示文件的详细大小。
-
默认为on,显示出文件的确切大小,单位是bytes。
-
改为off后,显示出文件的大概大小,单位是kB或者MB或者GB。
-
注意:只有当
autoindex_format
指令设置为 html 时候,autoindex_exact_size
指令才会起作用。
-
语法 | autoindex_exact_size on|off; |
---|---|
默认值 | autoindex_exact_size on; |
位置 | http、server、location |
-
**autoindex_localtime:**对应HTML格式,是否在目录列表上显示时间。
-
默认为off,显示的文件时间为GMT时间。
-
改为on后,显示的文件时间为文件的服务器时间。
-
语法 | autoindex_localtime on | off; |
---|---|
默认值 | autoindex_localtime off; |
位置 | http、server、location |
(4)案例实现
- 首先准备几个文件,上传到服务器
# 创建文件目录
mkdir /opt/download
# 上传之后文件如下
[root@localhost ~]# tree /opt/download
/opt/download
├── keepalived-2.0.20.tar.gz
├── memcached-1.6.31.tar.gz
├── nginx-1.26.2.zip
└── redis-7.0.4.tar.gz
- Nginx的配置文件添加配置项,root 指令后面必须是下载路径,因为我的下载路径是
/opt/download
,所以这里填写/opt
,而 location 的/download
会自动拼接到后面,形成完整的下载路径。
vim /usr/local/nginx/conf/nginx.conf
location /download {
root /opt; # 下载目录所在的路径,location 后面的 /download 拼接到 /opt 后面
# 以这些后缀的文件点击后为下载,注释掉则 txt 等文件是在网页打开并查看内容
if ($request_filename ~* ^.*?\.(txt|doc|pdf|rar|gz|zip|docx|exe|xlsx|ppt|pptx|conf)$){
add_header Content-Disposition 'attachment;';
}
autoindex on; # 启用目录列表的输出
autoindex_exact_size on; # 在目录列表展示文件的详细大小
autoindex_format html; # 设置目录列表的格式为 html
autoindex_localtime on; # 目录列表上显示系统时间
}
- 重启Nginx,测试访问:http://192.168.119.161/download/
- autoindex_format参数设置成json,重启Nginx,效果如下
- autoindex_format参数设置成xml,重启Nginx,效果如下
2、Nginx用户认证模块
(1)概述
- 对应系统资源的访问,我们往往需要限制谁能访问,谁不能访问。这就是我们通常所说的认证部分,认证需要做的就是根据用户输入的用户名和密码来判定用户是否为合法用户,如果是则放行访问,如果不是则拒绝访问。
- Nginx 对应用户认证是通过
ngx_http_auth_basic_module
模块实现,它允许通过使用「HTTP基本身份验证」协议验证用户名和密码来限制对资源的访问。默认情况下 Nginx 是已经安装了该模块,如果不需要则使用--without-http_auth_basic_module
删除认证模块。
(2)相关指令
- **auth_basic:**使用“ HTTP基本认证”协议启用用户名和密码的验证
- 开启后,服务端会返回401,指定的字符串会返回到客户端,给用户以提示信息,但是不同的浏览器对内容的展示不一致。
语法 | auth_basic string|off; |
---|---|
默认值 | auth_basic off; |
位置 | http,server,location,limit_except |
- **auth_basic_user_file:**指定用户名和密码所在文件。
- 指定文件路径,该文件中设置用户名和密码,密码需要进行加密。可以采用工具自动生成。
语法 | auth_basic_user_file file; |
---|---|
默认值 | — |
位置 | http,server,location,limit_except |
(3)案例实现
- 实现下载站点下载文件的时候进行身份校验
- Nginx的配置文件添加配置项auth_basic和auth_basic_user_file
vim /usr/local/nginx/conf/nginx.conf
location /download{
# 下载站点
root /opt; # 下载目录所在的路径,location 后面的 /download 拼接到 /opt 后面
autoindex on; # 启用目录列表的输出
autoindex_exact_size on; # 在目录列表展示文件的详细大小
autoindex_format html; # 设置目录列表的格式为 html
autoindex_localtime on; # 目录列表上显示系统时间
# 认证模块
auth_basic 'please input your auth'; # 启用户名和密码的验证,并在请求头插入数据
auth_basic_user_file htpasswd; # 存用户名和密码的文件路径
}
- 我们需要使用
htpasswd
工具生成包含用户名和密码的文件,使用之前先进行安装
yum install -y httpd-tools
# 语法
htpasswd -c /usr/local/nginx/conf/htpasswd username //创建一个新文件记录用户名和密码
htpasswd -b /usr/local/nginx/conf/htpasswd username password //在指定文件新增一个用户名和密码
htpasswd -D /usr/local/nginx/conf/htpasswd username //从指定文件删除一个用户信息
htpasswd -v /usr/local/nginx/conf/htpasswd username //验证用户名和密码是否正确
# 创建用户名密码 test test
[root@localhost conf]# htpasswd -c /usr/local/nginx/conf/htpasswd test
New password:
Re-type new password:
Adding password for user test
# 查看生成的配置文件是加密的
vim /usr/local/nginx/conf/htpasswd
test:$apr1$/D5v9cQX$xcBrjg4f6p/UDlVj8HNZt1
- 测试访问:http://192.168.119.161/download/,输入账号密码下载成功