Nginx 部署文件服务器 🚀

📁 快速搭建高效稳定的文件共享服务,支持内网外网访问


✨ 功能特点

  • 🌐 多协议支持 - 同时支持HTTP和HTTPS访问
  • 📂 目录浏览 - 自动生成美观的文件目录列表
  • 🔒 安全加密 - 支持SSL/TLS加密传输
  • 🎨 主题定制 - 可自定义页面头部和底部
  • 📱 响应式设计 - 适配各种设备访问
  • 高性能 - 基于Nginx的高效静态文件服务

🛠️ 环境准备

1. Debian/Ubuntu 安装 Nginx

1
2
3
4
5
6
7
8
9
10
11
# 安装Nginx
sudo apt update && sudo apt install nginx -y

# 启动Nginx服务
sudo systemctl start nginx

# 设置开机自启
sudo systemctl enable nginx

# 验证安装
nginx -v

2. 检查服务状态

1
2
3
4
5
# 查看Nginx状态
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证书配置(需要提前获取证书)
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

# 重启Nginx服务
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: #333; }
</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
# 使用Let's Encrypt获取免费证书(需要域名)
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
# 在nginx.conf中添加优化配置
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压缩
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
# 在配置中添加字符集设置
charset utf-8,gbk;

📊 监控与日志

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! 🎉 祝您部署顺利!