1、修改nginx.conf,新增Server节点,如下
server {
listen 9000; #端口
server_name file_download; #服务名
charset utf-8; # 避免中文乱码
root /usr/local/nginx/static; #显示的根索引目录,注意这里要改成你自己的,目录要存在 ,也就是你要下载文件的目录
location / {
autoindex on; #开启索引功能
autoindex_exact_size off; # 关闭计算文件确切大小(单位bytes),只显示大概大小(单位kb、mb、gb)
autoindex_localtime on; # 显示本机时间而非 GMT 时间
}
}
配置完成,重启Nginx,进入nginx/sbin 执行: ./nginx -s reload
访问 ip:9000 即可看到我们上传的文件
2、有时候我们为了安全起见,访问下载目录时需要使用密码,这时可以进行如下配置:
如想加上密码需要安装软件:
1>安装httpd-tools:
yum -y install httpd-tools
2>使用htpasswd
生成指定用户名和密码的权限文件,示例如下,按照提示输入两次密码
# 在/etct/nginx目录下,生成passwd110文件 # 添加test110用户
htpasswd -c /etc/nginx/passwd110 test110
3>配置nginx,在刚刚的 server 节点下加入:
# 以下是配置用户名和密码访问
auth_basic "Please input password"; #这里是验证时的提示信息
auth_basic_user_file /etc/nginx/passwd110;
例如:
server {
listen 9000; #端口
server_name file_download; #服务名
charset utf-8; # 避免中文乱码
root /usr/local/nginx/static; #显示的根索引目录,注意这里要改成你自己的,目录要存在
location / {
autoindex on; #开启索引功能
autoindex_exact_size off; # 关闭计算文件确切大小(单位bytes),只显示大概大小(单位kb、mb、gb)
autoindex_localtime on; # 显示本机时间而非 GMT 时间
auth_basic "Please input password"; #这里是验证时的提示信息
auth_basic_user_file /usr/local/nginx/passwd110;
}
}