如果光是满足文件目录的需求有许多方案,如小程序webd,Python的SimpleHTTPServer,NPM的http-server,开源的的FileBrowser,闭源的FileRun…
而我选择了一个不上不下的方案,也就是Nginx+FancyIndex模块。优点是可以轻松地实现防盗链和Https的支持。

编译安装Nginx

必须要编译时添加FancyIndex模块才可以使用,所以先编译Nginx
克隆FancyIndex模块的源码

1
git clone https://github.com/aperezdc/ngx-fancyindex.git ngx-fancyindex
1
apt update && sudo apt-get install build-essential libtool gcc automake autoconf make libpcre3 libpcre3-dev zlib1g-dev openssl-y

http://nginx.org/download/ 下载最新的Nginx源码,后缀为*.tar.gz的才是Linux版本

1
2
3
wget http://nginx.org/download/nginx-1.17.6.tar.gz
tar xvzf nginx-*.tar.gz && rm nginx-*.tar.gz
cd nginx-*

编译时记得连带将FancyIndex模块添加进去

1
2
3
./configure --prefix=/usr --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx_error.log --http-log-path=/var/log/nginx_access.log --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx.lock --user=root --group=root --with-http_ssl_module --without-http_ssi_module --without-http_memcached_module --without-http_browser_module --without-http_geo_module  --without-http_scgi_module  --without-http_uwsgi_module --without-select_module --add-module=../ngx-fancyindex
make
sudo make install

将Nginx添加开机启动

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
sudo tee /etc/systemd/system/nginx.service >/dev/null <<'EOF'
[Unit]
Description=nginx
After=network.target

[Service]
User=root
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx
ExecReload=/usr/sbin/nginx -s reload
ExecStop=/usr/sbin/nginx -s stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

将Nginx交给Systemd托管,设置Nginx下次开机启动

1
sudo systemctl daemon-reload && sudo systemctl enable nginx.service

为FancyIndex更换默认主题

默认的橙色主题是在太丑了,换成一个并没有名字的FancyIndex Theme
cd到你的站点根目录,如/www/wwwroot,然后克隆这个主题

1
git clone https://github.com/467815891a/nginx-fancyindex-theme.git fancyindex

在Nginx站点配置文件的location域中配置FancyIndex,这里直接贴上我Nginx的整个配置文件,以此为例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80

user root;
worker_processes 2;

pid /var/run/nginx.pid;
error_log /var/log/nginx_error.log;

events {
use epoll;
worker_connections 1024;
multi_accept on;
}


http {
include mime.types;
default_type application/octet-stream;
charset utf-8;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 60;
client_header_buffer_size 4k;
open_file_cache max=102400 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 1;
client_header_timeout 15;
client_body_timeout 15;
reset_timedout_connection on;
send_timeout 15;
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 3;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
server_tokens off;
access_log /var/log/nginx_access.log;

server {
listen 443 ssl;
server_name weiyangbo.com www.weiyangbo.com; #此处填你网站的域名
#下面两行是你的ssl证书的路径
ssl_certificate /etc/nginx/cert/xxxxxxx.pem;
ssl_certificate_key /etc/nginx/cert/xxxxxxx.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
root /www; #此处填你的网站目录
location / {
expires 10h;
fancyindex on;
fancyindex_exact_size off;
fancyindex_localtime on;
fancyindex_header "/fancyindex/header.html";
fancyindex_footer "/fancyindex/footer.html";
fancyindex_ignore "fancyindex" "Download"; #可以自定义文件服务器中不显示的文件或文件夹
fancyindex_name_length 500;
}
#这是防盗链设置
location ~* ^.+\.(jpg|gif|png|img|apk|tar.gz|wmv|jpeg|mp3|mp4|zip|rar)$ {
valid_referers none blocked weiyangbo.com www.weiyangbo.com;
if ($invalid_referer){
return 403;
break;
}
access_log off;
}
}
#重定向80端口的全部http请求去https
server {
listen 80;
server_name weiyangbo.com www.weiyangbo.com; #网站域名,跟上面保持一致
return 301 https://$server_name$request_uri;
}
}

测试网站

检测配置文件有无语法错误

1
nginx -t

尝试启动Nginx,看看实际效果。

1
nginx

定时清理Nginx日志

Nginx没有日志自动归档或者清除的功能,我们要借助logrotate来定时清理日志,不然如果网页访问量大,没几天日志大小就上GB了,磁盘都不知道怎么爆的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
sudo tee /etc/logrotate.d/nginx >/dev/null <<'EOF'
/var/log/nginx_*.log {
su root root
size 10000
daily
rotate 3
missingok
dateext
compress
delaycompress
notifempty
create 640 root root
sharedscripts
postrotate
if [ -f /var/run/nginx.pid ]; then
l -USR1 `cat /var/run/nginx.pid`
fi
endscript
}
EOF

sudo logrotate /etc/logrotate.d/nginx