Nginx 部署文件服务器 🚀

📁 快速搭建高效稳定的文件共享服务,支持内网外网访问
✨ 功能特点
- 🌐 多协议支持 - 同时支持HTTP和HTTPS访问
- 📂 目录浏览 - 自动生成美观的文件目录列表
- 🔒 安全加密 - 支持SSL/TLS加密传输
- 🎨 主题定制 - 可自定义页面头部和底部
- 📱 响应式设计 - 适配各种设备访问
- ⚡ 高性能 - 基于Nginx的高效静态文件服务
🛠️ 环境准备
1. Debian/Ubuntu 安装 Nginx
1 2 3 4 5 6 7 8 9 10 11
| sudo apt update && sudo apt install nginx -y
sudo systemctl start nginx
sudo systemctl enable nginx
nginx -v
|
2. 检查服务状态
1 2 3 4 5
| sudo systemctl status nginx
sudo nginx -t
|
🌐 带域名HTTPS文件服务
示例地址:https://wenjian.mobufan.eu.org:5553
📁 创建文件目录结构
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| sudo mkdir -p /etc/nginx/wenjian sudo chmod 755 /etc/nginx/wenjian
sudo tee /etc/nginx/wenjian/文件说明.txt <<'EOF' --------- 共享目录说明 --------- 文件分享目录:/etc/nginx/wenjian
终端查看命令:cd /etc/nginx/wenjian && ls 支持格式:所有文件类型直接下载 ------------------------------- EOF
cat /etc/nginx/wenjian/文件说明.txt
|
🔧 配置Nginx HTTPS文件服务
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
| sudo mkdir -p /etc/nginx/conf.d
sudo tee /etc/nginx/conf.d/file.conf <<'EOF' server { listen 666 ssl; server_name file.mobufan.eu.org;
ssl_certificate /etc/nginx/ssl/cert.pem; ssl_certificate_key /etc/nginx/ssl/key.pem; ssl_protocols TLSv1.2 TLSv1.3; ssl_ciphers ECDHE-RSA-AES256-GCM-SHA512:DHE-RSA-AES256-GCM-SHA512:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384; ssl_prefer_server_ciphers off;
location / { root /mnt/file; charset utf-8,gbk; autoindex on; autoindex_exact_size off; autoindex_localtime on; if ($request_filename ~* \.(txt|doc|pdf|rar|gz|zip|docx|exe|xlsx|ppt|pptx)$) { add_header Content-Disposition 'attachment'; } } } EOF
|
🔄 应用配置
1 2 3 4 5 6 7 8
| sudo nginx -t
sudo systemctl restart nginx
sudo systemctl status nginx
|
🏠 内网文件服务部署
示例地址:http://10.10.10.245:1515
📂 创建内网文件服务
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| sudo mkdir -p /etc/nginx/wenjian sudo chmod 755 /etc/nginx/wenjian
sudo tee /etc/nginx/conf.d/internal.conf <<'EOF' server { listen 1515; server_name localhost;
location / { root /etc/nginx/wenjian; autoindex on; autoindex_exact_size off; autoindex_localtime on; charset utf-8,gbk; add_header Cache-Control no-store; } } EOF
|
🎨 高级美化配置
使用Fancyindex主题
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
| sudo mkdir -p /etc/nginx/theme/fancyindex
sudo tee /etc/nginx/theme/fancyindex/header.html <<'EOF' <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>文件共享服务</title> <style> body { font-family: Arial, sans-serif; margin: 40px; } h1 { color: </style> </head> <body> <h1>📁 文件共享目录</h1> <hr> EOF
sudo tee /etc/nginx/theme/fancyindex/footer.html <<'EOF' <hr> <footer> <p>© 2024 文件共享服务 - Powered by Nginx</p> </footer> </body> </html> EOF
|
美化配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| server { listen 80; server_name fileserver.local; root /mnt/files; fancyindex on; fancyindex_localtime on; fancyindex_exact_size off; fancyindex_name_length 255; fancyindex_header "/theme/fancyindex/header.html"; fancyindex_footer "/theme/fancyindex/footer.html"; fancyindex_ignore "theme"; fancyindex_show_type on; }
|
🔒 SSL证书配置
获取SSL证书
1 2 3 4 5 6 7
| sudo apt install certbot python3-certbot-nginx -y sudo certbot --nginx -d your-domain.com
sudo mkdir -p /etc/nginx/ssl sudo chmod 700 /etc/nginx/ssl
|
⚙️ 性能优化配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| http { sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; open_file_cache max=1000 inactive=20s; open_file_cache_valid 30s; open_file_cache_min_uses 2; open_file_cache_errors on; gzip on; gzip_types text/plain text/css application/json application/javascript text/xml; }
|
🚨 常见问题解决
1. 权限问题
1 2 3
| sudo chown -R www-data:www-data /mnt/file sudo chmod -R 755 /mnt/file
|
2. 端口冲突
1 2 3 4 5
| sudo netstat -tulnp | grep :666
sudo ufw allow 666/tcp
|
3. 中文乱码
📊 监控与日志
1 2 3 4 5 6 7 8
| sudo tail -f /var/log/nginx/access.log
sudo tail -f /var/log/nginx/error.log
sudo nginx -T | grep worker_connections
|
🔧 维护命令
1 2 3 4 5 6 7 8 9 10 11
| sudo nginx -s reload
sudo nginx -t
sudo systemctl stop nginx
nginx -V
|
💡 提示:部署完成后,建议定期备份配置文件,监控服务器资源使用情况,并及时更新Nginx版本以确保安全性。
Happy File Sharing! 🎉 祝您部署顺利!