0%

Nginx搭建文件服务器

Nginx 搭建文件服务器:快速实现文件共享与管理

Nginx 不仅是高性能的 Web 服务器和反向代理,还能通过简单配置搭建轻量级文件服务器,用于共享文档、图片、安装包等资源。本文详细讲解 Nginx 文件服务器的配置方法、功能优化及上传限制设置,帮助快速构建实用的文件共享服务。

基础配置:开启目录浏览

Nginx 默认不显示目录结构,需通过autoindex系列指令开启目录浏览功能,核心配置如下:

1
2
3
4
5
6
7
8
9
10
11
server {
listen 9090; # 监听端口(如9090)
server_name _; # 匹配任意域名(可改为具体域名如file.example.com)
root /share/file; # 文件存储的根目录(需提前创建)
charset utf-8; # 支持中文文件名显示

# 开启目录浏览核心配置
autoindex on; # 启用目录列表显示(默认关闭)
autoindex_exact_size on; # 显示文件精确大小(单位为字节),off则显示KB/MB等
autoindex_localtime on; # 显示文件的本地修改时间(默认显示GMT时间)
}

配置说明

  1. root /share/file
    • 指定文件服务器的根目录,所有文件需存放在该目录下(如/share/file/docs//share/file/images/);
    • 需确保 Nginx 进程对该目录有读取权限(可执行chmod 755 /share/file)。
  2. 目录浏览优化
    • autoindex_localtime on:让文件修改时间显示为服务器本地时间(更直观);
    • charset utf-8:避免中文文件名乱码(关键配置)。

访问效果与目录结构

配置完成后,重启 Nginx(nginx -s reload),通过http://服务器IP:9090访问:

  • /share/file下有docs/image.pngarchive.zip,则页面会显示这些文件 / 目录的列表,包含名称、大小、修改时间;
  • 点击目录可进入子目录,点击文件可直接下载(浏览器支持的格式如图片、文本会直接预览)。

限制文件上传大小(解决 413 错误)

默认情况下,Nginx 对客户端请求体大小限制较严格(约 1M),若通过工具(如curl、Postman)向文件服务器上传大文件,可能返回413 Request Entity Too Large错误。需通过client_max_body_size调整限制。

配置方法

client_max_body_size可在httpserverlocation块中设置,范围依次递减(优先级:location > server > http):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 方法1:在server块中设置(仅对当前虚拟主机生效)
server {
listen 9090;
server_name _;
root /share/file;
client_max_body_size 50m; # 允许最大上传50M文件
# ... 其他配置
}

# 方法2:在http块中设置(全局生效)
http {
client_max_body_size 100m; # 所有虚拟主机默认允许最大100M
# ... 其他配置
}

高级功能:限制访问与安全优化

1. 限制特定 IP 访问

仅允许内部 IP 访问文件服务器,拒绝外部网络:

1
2
3
4
5
6
7
server {
# ... 基础配置

# 允许192.168.1.0/24网段访问,其他IP拒绝
allow 192.168.1.0/24;
deny all;
}

2. 启用用户认证(密码保护)

通过ngx_http_auth_basic_module模块设置用户名密码,限制访问:

  • 步骤 1:生成密码文件
    使用htpasswd工具(Apache 工具包,需提前安装)生成密码文件:

    1
    2
    3
    # 创建密码文件(/etc/nginx/.htpasswd),添加用户fileuser
    htpasswd -c /etc/nginx/.htpasswd fileuser
    # 按提示输入密码(如123456)
  • 步骤 2:配置认证

    1
    2
    3
    4
    5
    6
    7
    server {
    # ... 基础配置

    # 启用认证
    auth_basic "File Server Login"; # 认证提示信息
    auth_basic_user_file /etc/nginx/.htpasswd; # 密码文件路径
    }

    访问时需输入用户名fileuser和密码,否则返回 401 Unauthorized。

3. 隐藏敏感文件

通过location匹配特定文件(如.git.env),禁止访问:

1
2
3
4
5
6
7
8
9
server {
# ... 基础配置

# 禁止访问隐藏文件和敏感文件
location ~ /\.git | /\.env {
deny all;
return 403;
}
}

文件上传方案(配合第三方工具)

Nginx 本身不直接支持文件上传功能,需配合后端脚本(如 Python/PHP)或专用工具实现:

1. 简单上传:使用curl命令

在服务器本地通过curl上传文件到文件服务器目录:

1
2
# 上传localfile.zip到/share/file/uploads/(需提前创建uploads目录)
curl -X POST -F "file=@localfile.zip" http://localhost:9090/uploads/
  • 注意:需确保/share/file/uploads/目录有写入权限(chmod 777 /share/file/uploads,生产环境建议用更严格的权限)。

2. 网页上传:集成简单表单

在文件服务器根目录创建upload.html,通过表单实现浏览器上传:

1
2
3
4
5
<!-- /share/file/upload.html -->
<form action="/uploads/" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<button type="submit">上传</button>
</form>
  • 需配合后端脚本处理上传(如 PHP),或使用 Nginx 的upload_module模块(需额外编译安装)。

常见问题解决

  1. 中文文件名乱码
    确保配置charset utf-8,且服务器文件系统支持中文(如 UTF-8 编码的 Linux 系统)。
  2. 403 Forbidden 错误
    • 检查 Nginx 进程对文件目录的权限(chown -R nginx:nginx /share/file);
    • 若启用了auth_basic,确认用户名密码正确。
  3. 上传大文件失败
    • client_max_body_size外,还需检查后端服务(如 PHP)的上传限制(upload_max_filesizepost_max_size

欢迎关注我的其它发布渠道

表情 | 预览
快来做第一个评论的人吧~
Powered By Valine
v1.3.10