一、为什么选择 Nginx 作为文件服务器
1. 性能优势
2. 功能特性
二、基础配置
1. 安装 Nginx
sudo apt updatesudo apt install nginx sudo yum install nginxsudo dnf install nginx
2. 基本文件服务器配置
server { listen 80; server_name your-domain.com; location / { root /var/www/files; autoindex on; autoindex_exact_size off; autoindex_localtime on; 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. 创建认证文件
sudo apt install apache2-utils sudo yum install httpd-tools 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_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; }} server { 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 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; 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; add_header Cache-Control ”public, immutable”; } location ~* \.txt$ { root /var/www/files; add_header Content-Type text/plain; }}
四、完整的生产环境配置
upstream fileserver_backend { server 127.0.0.1:8080;}server { listen 80; listen [::]:80; server_name your-domain.com; return 301 https://$server_name$request_uri;}server { listen 443 ssl http2; listen [::]:443 ssl http2; server_name your-domain.com; ssl_certificate /path/to/certificate.crt; ssl_certificate_key /path/to/private.key; ssl_trusted_certificate /path/to/ca.crt; 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. 启用站点
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 nginxsudo 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. 访问控制
location / { root /var/www/files; allow 192.168.1.0/24; allow 127.0.0.1; deny all; 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 on; tcp_nopush on; tcp_nodelay on;}
2. Gzip压缩
gzip 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. 常用命令
sudo systemctl start nginx sudo systemctl stop nginx sudo 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 编辑过