LOGO 首页 OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 技术文档 其他文档  
 
网站管理员

Nginx 搭建文件服务器--局域网快速使用

admin
2026年10月30日 17:3 本文热度 46

一、为什么选择 Nginx 作为文件服务器

1. 性能优势

  • 高并发处理 - 轻量级,支持大量并发连接
  • 低资源消耗 - 内存占用少,CPU使用率低
  • 静态文件服务 - 专门优化过的静态文件传输
  • 高稳定性 - 长期运行稳定可靠

2. 功能特性

  • 简单的配置 - 配置文件简洁明了
  • HTTP基本认证 - 内置访问控制
  • 目录浏览 - 自动显示目录内容
  • 防盗链 - 防止他人盗用资源

二、基础配置

1. 安装 Nginx

# Ubuntu/Debiansudo apt updatesudo apt install nginx # CentOS/RHELsudo yum install nginx# 或者使用 dnf (较新版本)sudo dnf install nginx # Windows# 下载安装包并运行

2. 基本文件服务器配置

# /etc/nginx/sites-available/fileserverserver {    listen 80;    server_name your-domain.com; # 或者使用IP地址   # 文件服务器根目录    location / {        root /var/www/files; # 指定文件存储目录        autoindex on; # 启用目录浏览        autoindex_exact_size off; # 文件大小显示为人类可读格式        autoindex_localtime on; # 显示本地时间而非GMT时间   # 设置基本认证(可选)        auth_basic ”Restricted Access”;        auth_basic_user_file /etc/nginx/.htpasswd;    }   # 限制访问日志(可选)    access_log /var/log/nginx/fileserver_access.log;    error_log /var/log/nginx/fileserver_error.log;}

3. 创建认证文件

# 安装htpasswd工具sudo apt install apache2-utils # Ubuntu/Debian# 或sudo yum install httpd-tools # CentOS/RHEL # 创建用户和密码文件sudo htpasswd -c /etc/nginx/.htpasswd username# 系统会提示输入密码


三、高级配置

1. HTTPS 配置

server {    listen 443 ssl http2;    server_name your-domain.com;     ssl_certificate /path/to/your/certificate.crt;    ssl_certificate_key /path/to/your/private.key;   # SSL 安全配置    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 /var/www/files;        autoindex on;        autoindex_exact_size off;        autoindex_localtime on;   # 限制文件上传大小        client_max_body_size 100M;    }} # HTTP 重定向到 HTTPSserver {    listen 80;    server_name your-domain.com;    return 301 https://$server_name$request_uri;}

2. 防盗链配置

server {    listen 80;    server_name your-domain.com;     location /files/ {        root /var/www;        autoindex on;        autoindex_exact_size off;        autoindex_localtime on;   # 防盗链配置        valid_referers none blocked server_names *.your-domain.com;        if ($invalid_referer) {            return 403;        }    }}

3. 压缩传输

server {    listen 80;    server_name your-domain.com;   # 启用Gzip压缩    gzip on;    gzip_vary on;    gzip_min_length 1024;    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;     location / {        root /var/www/files;        autoindex on;        autoindex_exact_size off;        autoindex_localtime on;    }}

4. 自定义文件类型处理

server {    listen 80;    server_name your-domain.com;   # 自定义MIME类型    location ~* \.pdf$ {        root /var/www/files;        add_header Content-Type application/pdf;        add_header Content-Disposition attachment;    }     location ~* \.(jpg|jpeg|png|gif)$ {        root /var/www/files;        expires 30d; # 缓存30天        add_header Cache-Control ”public, immutable”;    }     location ~* \.txt$ {        root /var/www/files;        add_header Content-Type text/plain;    }}


四、完整的生产环境配置

# /etc/nginx/sites-available/fileserver.confupstream fileserver_backend {    server 127.0.0.1:8080;# 如果需要反向代理到应用服务器}server {    listen 80;    listen [::]:80;    server_name your-domain.com;# 重定向到HTTPS    return 301 https://$server_name$request_uri;}server {    listen 443 ssl http2;    listen [::]:443 ssl http2;    server_name your-domain.com;# SSL证书配置    ssl_certificate /path/to/certificate.crt;    ssl_certificate_key /path/to/private.key;    ssl_trusted_certificate /path/to/ca.crt;# SSL安全配置    ssl_protocols TLSv1.2 TLSv1.3;    ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384;    ssl_prefer_server_ciphers off;# 安全头    add_header X-Frame-Options DENY always;    add_header X-Content-Type-Options nosniff always;    add_header X-XSS-Protection ”1; mode=block” always;    add_header Strict-Transport-Security ”max-age=63072000” always;# 文件上传限制    client_max_body_size 100M;# 静态文件服务    location / {        root /var/www/files;        autoindex on;        autoindex_exact_size off;        autoindex_localtime on;# 缓存静态文件        expires 1d;        add_header Cache-Control ”public, immutable”;# 防盗链        valid_referers none blocked server_names *.your-domain.com;        if ($invalid_referer) {            return 403;        }    }# 特定目录配置    location ^~ /private/ {        root /var/www;        auth_basic ”Private Area - Authorization Required”;        auth_basic_user_file /etc/nginx/.htpasswd;    }# 日志配置    access_log /var/log/nginx/fileserver_access.log;    error_log /var/log/nginx/fileserver_error.log;}# 服务状态监控(可选)server {    listen 127.0.0.1:8080;# 仅本地访问    location /nginx_status {        stub_status on;        access_log off;        allow 127.0.0.1;        deny all;    }}


五、启用配置

1. 启用站点

# 创建软链接(Ubuntu/Debian)sudo ln -s /etc/nginx/sites-available/fileserver.conf /etc/nginx/sites-enabled/ # 或者直接复制配置文件sudo cp /etc/nginx/sites-available/fileserver.conf /etc/nginx/conf.d/fileserver.conf # 测试配置sudo nginx -t # 重新加载配置sudo systemctl reload nginx# 或sudo nginx -s reload

2. 创建文件目录

# 创建文件存储目录sudo mkdir -p /var/www/filessudo chown www-data:www-data /var/www/filessudo chmod 755 /var/www/files # 或者使用自定义目录mkdir -p ~/shared-files# 在配置中使用绝对路径


六、文件上传功能(配合后端)

1. Nginx 配置(配合后端处理上传)

server {    listen 80;    server_name your-domain.com;   # 文件上传处理(需要后端应用)    location /upload {        proxy_pass http://127.0.0.1:3000; # 后端应用地址        client_max_body_size 100M;        proxy_set_header Host $host;        proxy_set_header X-Real-IP $remote_addr;        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;    }   # 文件访问    location /files/ {        root /var/www;        autoindex on;        autoindex_exact_size off;        autoindex_localtime on;    }}

2. 简单的上传页面

<!DOCTYPE html>     文件上传     文件上传          上传


七、安全配置

1. 访问控制

# IP白名单location / {    root /var/www/files;    allow 192.168.1.0/24; # 允许特定IP段    allow 127.0.0.1;    deny all; # 拒绝其他所有IP    autoindex on;} # 基本认证location /protected/ {    auth_basic ”Restricted”;    auth_basic_user_file /etc/nginx/.htpasswd;}

2. 防止恶意文件上传

# 禁止执行脚本文件location ~* \.(php|pl|py|jsp|asp|sh|cgi)$ {    deny all;    return 404;} # 限制上传文件类型location /uploads {    location ~* \.(jpg|jpeg|png|gif|pdf|doc|docx|txt|zip|rar)$ {  # 允许的文件类型    }     location ~* \.(php|html|htm|js|css)$ {        deny all;        return 404;    }}


八、性能优化

1. 缓存配置

# 启用文件缓存location / {    root /var/www/files;    autoindex on;    autoindex_exact_size off;    autoindex_localtime on;   # 设置缓存    expires 1y;    add_header Cache-Control ”public, immutable”;   # 启用sendfile    sendfile on;    tcp_nopush on;    tcp_nodelay on;}

2. Gzip压缩

# 全局启用Gzipgzip on;gzip_vary on;gzip_min_length 1024;gzip_proxied expired no-cache no-store private must-revalidate auth;gzip_types    text/plain    text/css    text/xml    text/javascript    application/javascript    application/xml+rss    application/json;


九、管理和监控

1. 常用命令

# 启动Nginxsudo systemctl start nginx # 停止Nginxsudo systemctl stop nginx # 重启Nginxsudo systemctl restart nginx # 重载配置sudo systemctl reload nginx # 检查配置sudo nginx -t # 查看状态sudo systemctl status nginx

2. 日志查看

# 查看访问日志sudo tail -f /var/log/nginx/access.log # 查看错误日志sudo tail -f /var/log/nginx/error.log # 统计访问量sudo awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -nr


十、总结

使用 Nginx 作为文件服务器的优势:

  • 高性能 - 静态文件服务效率极高
  • 配置简单 - 配置文件直观易懂
  • 功能丰富 - 支持认证、防盗链、缓存等
  • 安全可靠 - 企业级稳定性
  • 资源占用少 - 轻量级,适合各种规模部署


阅读原文:点击这里​


该文章在 2026/10/30 17:03:17 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2026 ClickSun All Rights Reserved  粤ICP备13012886号-2  粤公网安备44030602007207号