Nginx 文件服务器部署文档

文档版本:v1.0 适用系统:CentOS 7 / Rocky Linux 8 / Ubuntu 20.04+ Nginx 版本:1.20+ 编写日期:2026-05-14


1. 概述

Nginx 文件服务器利用其内置的 autoindex 模块,将指定目录通过 HTTP/HTTPS 对外提供静态文件下载服务,适用于以下场景:

  • 内网软件包、镜像文件分发
  • 局域网共享存储
  • 构建产物归档与对外发布
  • 临时文件传输服务

2. 环境准备

2.1 服务器信息规划

配置项示例值说明
服务器 IP192.168.1.100根据实际环境填写
服务端口80 / 443HTTP / HTTPS
文件根目录/data/fileserver存放共享文件的目录
访问路径/files/URL 访问路径前缀

2.2 关闭 SELinux(CentOS/Rocky)

# 临时关闭
setenforce 0

# 永久关闭(需重启生效)
sed -i 's/^SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config

⚠️ 注意:生产环境建议通过 SELinux 策略放行而非直接关闭。


3. 安装 Nginx

3.1 CentOS 7 / Rocky Linux 8

# 安装 EPEL 源
yum install -y epel-release

# 安装 Nginx
yum install -y nginx

# 设置开机自启并立即启动
systemctl enable nginx --now

3.2 Ubuntu / Debian

# 更新软件源
apt update

# 安装 Nginx
apt install -y nginx

# 设置开机自启并立即启动
systemctl enable nginx --now

3.3 验证安装

nginx -v
# 示例输出:nginx version: nginx/1.20.1

systemctl status nginx

4. 配置文件服务器

4.1 创建文件存储目录

# 创建目录
mkdir -p /data/fileserver

# 设置权限(允许 nginx 用户读取)
chown -R nginx:nginx /data/fileserver
chmod -R 755 /data/fileserver

4.2 上传测试文件

# 创建测试文件
echo "Hello, FileServer!" > /data/fileserver/test.txt
mkdir -p /data/fileserver/packages
touch /data/fileserver/packages/app-v1.0.tar.gz

4.3 编写 Nginx 虚拟主机配置

新建配置文件:

vim /etc/nginx/conf.d/fileserver.conf

写入以下内容:

server {
    listen       80;
    server_name  192.168.1.100;   # 替换为实际 IP 或域名

    # 字符集,避免中文文件名乱码
    charset utf-8;

    # 访问日志
    access_log  /var/log/nginx/fileserver_access.log;
    error_log   /var/log/nginx/fileserver_error.log;

    location /files/ {
        # 映射到本地目录(注意末尾斜杠)
        alias /data/fileserver/;

        # 开启目录浏览
        autoindex on;

        # 显示文件大小(精确字节数)
        autoindex_exact_size on;

        # 显示本地时间
        autoindex_localtime on;

        # 禁止上传(只读模式)
        limit_except GET HEAD {
            deny all;
        }
    }

    # 根路径重定向到文件目录
    location = / {
        return 301 /files/;
    }

    # 自定义错误页
    error_page 403 /403.html;
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
}

4.4 检查并重载配置

# 检查配置语法
nginx -t

# 输出示例:
# nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
# nginx: configuration file /etc/nginx/nginx.conf test is successful

# 重载配置(不中断服务)
systemctl reload nginx

5. 访问控制与安全加固

5.1 IP 白名单限制

仅允许内网网段访问:

location /files/ {
    alias /data/fileserver/;
    autoindex on;
    autoindex_exact_size on;
    autoindex_localtime on;

    # 仅允许内网访问
    allow 192.168.1.0/24;
    allow 10.0.0.0/8;
    deny all;
}

5.2 HTTP Basic 认证

Step 1:安装 htpasswd 工具

# CentOS/Rocky
yum install -y httpd-tools

# Ubuntu/Debian
apt install -y apache2-utils

Step 2:创建密码文件

# 创建用户 admin,根据提示输入密码
htpasswd -c /etc/nginx/.htpasswd admin

# 追加更多用户(去掉 -c 参数)
htpasswd /etc/nginx/.htpasswd user2

Step 3:在 Nginx 配置中启用认证

location /files/ {
    alias /data/fileserver/;
    autoindex on;
    autoindex_exact_size on;
    autoindex_localtime on;

    # 启用 Basic 认证
    auth_basic           "File Server - Please Login";
    auth_basic_user_file /etc/nginx/.htpasswd;
}

Step 4:重载配置

systemctl reload nginx

5.3 隐藏 Nginx 版本号

/etc/nginx/nginx.confhttp {} 块中添加:

http {
    server_tokens off;   # 隐藏版本号
    ...
}

6. HTTPS 配置(可选)

6.1 自签名证书(测试环境)

# 创建证书存储目录
mkdir -p /etc/nginx/ssl

# 生成自签名证书(有效期 3650 天)
openssl req -x509 -nodes -days 3650 -newkey rsa:2048 \
  -keyout /etc/nginx/ssl/server.key \
  -out    /etc/nginx/ssl/server.crt \
  -subj "/C=CN/ST=Guangdong/L=Shenzhen/O=MyOrg/CN=192.168.1.100"

6.2 HTTPS 虚拟主机配置

server {
    listen       443 ssl;
    server_name  192.168.1.100;
    charset      utf-8;

    ssl_certificate     /etc/nginx/ssl/server.crt;
    ssl_certificate_key /etc/nginx/ssl/server.key;

    # 推荐协议与加密套件
    ssl_protocols       TLSv1.2 TLSv1.3;
    ssl_ciphers         HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;

    access_log  /var/log/nginx/fileserver_ssl_access.log;
    error_log   /var/log/nginx/fileserver_ssl_error.log;

    location /files/ {
        alias /data/fileserver/;
        autoindex on;
        autoindex_exact_size on;
        autoindex_localtime on;
    }
}

# HTTP 自动跳转 HTTPS
server {
    listen      80;
    server_name 192.168.1.100;
    return 301  https://$host$request_uri;
}

7. 防火墙设置

7.1 firewalld(CentOS/Rocky)

# 开放 HTTP
firewall-cmd --permanent --add-service=http

# 开放 HTTPS(如启用)
firewall-cmd --permanent --add-service=https

# 重载防火墙规则
firewall-cmd --reload

# 验证
firewall-cmd --list-all

7.2 ufw(Ubuntu/Debian)

# 开放 HTTP
ufw allow 80/tcp

# 开放 HTTPS(如启用)
ufw allow 443/tcp

# 重载
ufw reload

8. 验证与测试

8.1 本地验证

# 测试 HTTP 访问
curl -I http://192.168.1.100/files/

# 带认证(如已启用)
curl -u admin:password http://192.168.1.100/files/

# 下载文件
curl -O http://192.168.1.100/files/test.txt

8.2 浏览器访问

在浏览器中访问以下地址,应看到目录列表页:

http://192.168.1.100/files/

目录列表页面示例:

Index of /files/

../
packages/                  2026-05-14 09:00   -
test.txt                   2026-05-14 09:00   19

9. 常用管理命令

操作命令
启动 Nginxsystemctl start nginx
停止 Nginxsystemctl stop nginx
重启 Nginxsystemctl restart nginx
重载配置systemctl reload nginx
检查配置语法nginx -t
查看运行状态systemctl status nginx
查看访问日志tail -f /var/log/nginx/fileserver_access.log
查看错误日志tail -f /var/log/nginx/fileserver_error.log
查看进程ps aux \| grep nginx

10. 常见问题排查

❌ 问题一:403 Forbidden

可能原因与解决方案:

# 1. 目录权限不足
chmod -R 755 /data/fileserver
chown -R nginx:nginx /data/fileserver

# 2. autoindex 未开启 → 检查配置中是否有 autoindex on;

# 3. SELinux 阻止访问
# 临时关闭 SELinux
setenforce 0

# 或赋予目录正确的 SELinux 上下文
chcon -Rt httpd_sys_content_t /data/fileserver

❌ 问题二:404 Not Found

可能原因与解决方案:

# 1. alias 路径末尾缺少斜杠 → 确保 alias 以 / 结尾
alias /data/fileserver/;

# 2. URL location 路径拼写错误 → 检查 location /files/ 与访问 URL 是否一致

# 3. 目录不存在
ls -la /data/fileserver

❌ 问题三:中文文件名乱码

# 在 server 块中添加<br>charset utf-8;

❌ 问题四:端口被占用

# 查看端口占用
ss -tlnp | grep :80

# 修改 nginx 监听端口(如改为 8080)
listen 8080;

# 重载配置
systemctl reload nginx

❌ 问题五:配置修改后不生效

# 第一步:检查语法
nginx -t

# 第二步:确认重载成功
systemctl reload nginx

# 第三步:查看是否有多个配置文件冲突
ls /etc/nginx/conf.d/
ls /etc/nginx/sites-enabled/   # Ubuntu 系统

附录:配置文件完整示例

# /etc/nginx/conf.d/fileserver.conf

server {
    listen       80;
    server_name  192.168.1.100;
    charset      utf-8;

    access_log  /var/log/nginx/fileserver_access.log;
    error_log   /var/log/nginx/fileserver_error.log;

    location /files/ {
        alias /data/fileserver/;

        autoindex            on;
        autoindex_exact_size on;
        autoindex_localtime  on;

        # IP 白名单(按需开启)
        # allow 192.168.1.0/24;
        # deny all;

        # Basic 认证(按需开启)
        # auth_basic           "File Server";
        # auth_basic_user_file /etc/nginx/.htpasswd;

        limit_except GET HEAD {
            deny all;
        }
    }

    location = / {
        return 301 /files/;
    }
}

📌 文档维护提示:本文档适用于基础文件服务器场景,如需大规模文件分发,建议结合 CDN 或对象存储(如 MinIO)进行扩展。

© 版权声明
THE END
喜欢就支持一下吧
点赞9 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容