1.安装Nginx
系统是debian
或者ubuntu
的可以用以下命令安装,其他系统的安装方法请自行搜索。
sudo apt install nginx-extras -y
2.创建conf文件
sudo vim /etc/nginx/conf.d/file_server.conf
3.配置conf文件
server {listen80;listen[::]:80;server_name 127.0.0.1; # 自己PC的ip或者服务器的域名 charset utf-8; # 避免中文乱码 root /home/ubuntu/share; # 存放文件的目录 location / { fancyindex on; # 索引fancyindex_exact_size off; # 关闭文件大小fancyindex_localtime on; # 显示文件时间fancyindex_name_length 255;# 美化,也可以注释掉不要fancyindex_header "/Nginx-Fancyindex-Theme-light/header.html";fancyindex_footer "/Nginx-Fancyindex-Theme-light/footer.html";fancyindex_ignore "examplefile.html";fancyindex_ignore "Nginx-Fancyindex-Theme-light";fancyindex_time_format "%Y-%m-%d %H:%M";# 密码,也可以注释掉不用auth_basic "user login";auth_basic_user_file /etc/nginx/passwd/file_server;}}
4.美化
下载Fancy Index主题解压该主题,注意,解压后有2个主题,分别是Nginx-Fancyindex-Theme-light
和Nginx-Fancyindex-Theme-dark
,选择自己喜欢的一个并上传到root
目录,在这边是/home/xx/share
(在这里我选择的是Nginx-Fancyindex-Theme-light,如果选择的是Nginx-Fancyindex-Theme-dark,则相应的在上面配置文件中也要修改)5.加密
生成密码文件
mkdir -p /etc/nginx/passwd/;touch file_server
echo "
账户名
:$(openssl passwd密码
)" > /etc/nginx/passwd/file_server
把上面的账户名
和密码
改成自己的账户和密码即可。
6.重新加载nginx的配置文件
sudo systemctl restart nginx.service
最终效果图如下所示: