Nginx 文件下载服务器搭建

下载站使用 autoindex 展示文件列表,并通过 sendfile 和缓存头提高传输效率。目录只放允许公开下载的文件。

文件下载配置

server {
    listen 80;
    server_name dl.example.com;
    root /srv/downloads;

    location / {
        autoindex on;
        autoindex_exact_size off;
        autoindex_localtime on;
        sendfile on;
        tcp_nopush on;
    }

    location ~ /\. {
        deny all;
    }
}

准备目录并测试

sudo install -d /srv/downloads
printf 'demo
' | sudo tee /srv/downloads/demo.txt
sudo chmod -R a+rX /srv/downloads
sudo nginx -t && sudo nginx -s reload
curl http://127.0.0.1/
curl -O http://127.0.0.1/demo.txt

安全