使用Nginx搭建文件服务器的全过程
为什么选择 Nginx 作为文件服务器1.性能优势高并发处理- 轻量级支持大量并发连接低资源消耗- 内存占用少CPU使用率低静态文件服务- 专门优化过的静态文件传输高稳定性- 长期运行稳定可靠2.功能特性简单的配置- 配置文件简洁明了HTTP基本认证- 内置访问控制目录浏览- 自动显示目录内容防盗链- 防止他人盗用资源基础配置1.安装 Nginx1234567891011# Ubuntu/Debiansudoapt updatesudoaptinstallnginx# CentOS/RHELsudoyuminstallnginx# 或者使用 dnf (较新版本)sudodnfinstallnginx# Windows# 下载安装包并运行2.基本文件服务器配置123456789101112131415161718192021# /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.创建认证文件12345678# 安装htpasswd工具sudoaptinstallapache2-utils# Ubuntu/Debian# 或sudoyuminstallhttpd-tools# CentOS/RHEL# 创建用户和密码文件sudohtpasswd -c/etc/nginx/.htpasswd username# 系统会提示输入密码高级配置1.HTTPS 配置1234567891011121314151617181920212223242526272829server {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.防盗链配置1234567891011121314151617server {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.压缩传输1234567891011121314151617server {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/xmlrss text/javascript;location / {root /var/www/files;autoindex on;autoindex_exact_size off;autoindex_localtime on;}}4.自定义文件类型处理12345678910111213141516171819202122server {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;}}完整的生产环境配置123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778# /etc/nginx/sites-available/fileserver.confupstream fileserver_backend {server 127.0.0.1:8080; # 如果需要反向代理到应用服务器}server {listen 80;listen [::]:80;server_name your-domain.com;# 重定向到HTTPSreturn 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; modeblock always;add_header Strict-Transport-Security max-age63072000 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.启用站点12345678910111213# 创建软链接Ubuntu/Debiansudoln-s/etc/nginx/sites-available/fileserver.conf/etc/nginx/sites-enabled/# 或者直接复制配置文件sudocp/etc/nginx/sites-available/fileserver.conf/etc/nginx/conf.d/fileserver.conf# 测试配置sudonginx -t# 重新加载配置sudosystemctl reload nginx# 或sudonginx -s reload2.创建文件目录12345678# 创建文件存储目录sudomkdir-p/var/www/filessudochownwww-data:www-data/var/www/filessudochmod755/var/www/files# 或者使用自定义目录mkdir-p ~/shared-files# 在配置中使用绝对路径文件上传功能配合后端1.Nginx 配置配合后端处理上传123456789101112131415161718192021server {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.简单的上传页面12345678910111213!DOCTYPE htmlhtmlheadtitle文件上传/title/headbodyh2文件上传/h2formaction/uploadmethodpostenctypemultipart/form-datainputtypefilenamefilerequiredbuttontypesubmit上传/button/form/body/html安全配置1.访问控制1234567891011121314# IP白名单location / {root /var/www/files;allow 192.168.1.0/24; # 允许特定IP段allow 127.0.0.1;deny all; # 拒绝其他所有IPautoindex on;}# 基本认证location /protected/ {auth_basic Restricted;auth_basic_user_file /etc/nginx/.htpasswd;}2.防止恶意文件上传1234567891011121314151617# 禁止执行脚本文件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.缓存配置12345678910111213141516# 启用文件缓存location / {root /var/www/files;autoindex on;autoindex_exact_size off;autoindex_localtime on;# 设置缓存expires 1y;add_header Cache-Control public, immutable;# 启用sendfilesendfile on;tcp_nopush on;tcp_nodelay on;}2.Gzip压缩12345678910111213# 全局启用Gzipgzipon;gzip_vary on;gzip_min_length 1024;gzip_proxied expired no-cache no-store private must-revalidate auth;gzip_typestext/plaintext/csstext/xmltext/javascriptapplication/javascriptapplication/xmlrssapplication/json;管理和监控1.常用命令1234567891011121314151617# 启动Nginxsudosystemctl start nginx# 停止Nginxsudosystemctl stop nginx# 重启Nginxsudosystemctl restart nginx# 重载配置sudosystemctl reload nginx# 检查配置sudonginx -t# 查看状态sudosystemctl status nginx2.日志查看12345678# 查看访问日志sudotail-f/var/log/nginx/access.log# 查看错误日志sudotail-f/var/log/nginx/error.log# 统计访问量sudoawk{print $1}/var/log/nginx/access.log |sort|uniq-c |sort-nr总结使用 Nginx 作为文件服务器的优势高性能- 静态文件服务效率极高配置简单- 配置文件直观易懂功能丰富- 支持认证、防盗链、缓存等安全可靠- 企业级稳定性资源占用少- 轻量级适合各种规模部署Nginx 是搭建文件服务器的理想选择特别适合用于静态文件分发、内网文件共享、CDN节点等场景。
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2515298.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!