nginx搭建文件上传下载服务器

本文详细记录了在CentOS 6.8环境下使用Nginx配置文件上传功能的过程,包括解决多个编译错误及依赖问题,并推荐了一个支持最新Nginx版本的上传模块。

最近要搭建文件一个服务器, 准备使用python的web框架tornado来做。发现了这篇blog,准备使用nginx来实现文件上传和下载的功能。下面把实现步骤记录下来,中间有很多坑。

系统环境:centos6.8

1、下载nginx

wget  http://nginx.org/download/nginx-1.10.2.tar.gz

2、下载nginx upload module

wget http://www.grid.net.ru/nginx/download/nginx_upload_module-2.2.0.tar.gz

3、下载nginx-upload-progress-module

git clone https://github.com/masterzen/nginx-upload-progress-module.git

3、编译nginx

./configure --prefix=/usr/local/nginx --with-stream --with-http_image_filter_module --add-module=nginx upload module源码路径 --add-module=nginx-upload-progress-module源码路径 

--with-http_image_filter_module  添加nginx图片处理模块

出现如下错误:./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.

4、安装pcre注意使用高版本的, 低版本的一样会出现上面error

我使用的版本是pcre-8.38。安装过程略。安装好pcre后,执行步骤3。

出现如下错误:./configure: error: the HTTP image filter module requires the GD library.
You can either do not enable the module or install the libraries.

因为HttpImageFilterModule模块需要依赖gd-devel的支持, 安装gd-devel :yum install gd-devel


5、构建nginx出现错误:

error: no member named 'to_write' in 'ngx_http_request_body_t',在stack overflow有解决方案stackoverflow

具体原因是我们下载的nginx_upload_module-2.2.0.tar.gz不支持nginx 1.4+版本,https://github.com/hongzhidao/nginx-upload-module这个支持最新的nginx版本

6、下载wget https://github.com/hongzhidao/nginx-upload-module/archive/master.zip

7、构建、安装nginx

成功安装nginx

8、可以使用demo测试上传文件的功能了

https://github.com/hongzhidao/nginx-upload-module/blob/master/README.md

http://www.grid.net.ru/nginx/upload.en.html

上传进度获取在下面blog阐述。

为了使用 Nginx 搭建支持文件上传服务器,需要对 Nginx 的配置文件进行适当的调整,以确保服务器能够接收上传文件并正确处理请求。以下是详细的配置指南: ### 1. 配置客户端请求体大小限制 Nginx 默认对客户端请求体大小有限制(通常是 1MB),因此需要修改配置以支持大文件上传。在配置文件中添加或修改以下指令: ```nginx http { ... client_max_body_size 20M; # 设置最大上传文件大小为20MB ... } ``` 或者,如果你只想针对某个特定的 `server` 或 `location` 块进行限制,可以将 `client_max_body_size` 放在对应的块中: ```nginx server { listen 80; server_name example.com; client_max_body_size 20M; location /upload/ { # 处理上传请求的配置 } } ``` ### 2. 配置 `location` 处理上传请求 Nginx 本身并不直接处理文件上传,而是将上传的请求转发给后端应用(如 PHP、Python、Node.js 等)。因此,你需要确保 `location` 块正确地将上传请求传递给后端处理程序。例如,如果你使用的是 FastCGI(如 PHP): ```nginx location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php8.1-fpm.sock; } ``` 对于使用反向代理的应用(如 Python Flask 或 Node.js),可以这样配置: ```nginx location /upload/ { proxy_pass http://127.0.0.1:5000; # 假设后端服务运行在5000端口 proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } ``` ### 3. 配置上传文件的存储路径 虽然 Nginx 本身不直接处理文件存储,但你可以通过后端服务来处理上传文件。通常,上传文件会先存储在服务器的某个临时目录中,然后由后端服务处理并保存到指定位置。你可以在 Nginx 中设置一个目录来接收上传文件: ```nginx location /upload/ { root /var/www/html/uploads; # 设置文件上传的目标目录 autoindex on; # 启用目录浏览功能 } ``` 确保 `/var/www/html/uploads` 目录存在,并且 Nginx 进程有写入权限。 ### 4. 启用 HTTP 方法支持 默认情况下,Nginx 允许常见的 HTTP 方法(如 GET、POST、HEAD 等)。如果你希望支持 PUT 或其他方法,可以使用 `if` 指令来允许特定的方法: ```nginx location /upload/ { if ($request_method = 'POST') { add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Methods' 'POST, OPTIONS'; add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range'; add_header 'Access-Control-Max-Age' 1728000; add_header 'Content-Type' 'text/plain; charset=utf-8'; add_header 'Content-Length' 0; return 204; } if ($request_method != 'POST') { return 405; } # 其他配置 } ``` ### 5. 配置 MIME 类型和缓存控制 为了确保上传文件能够被正确识别和处理,建议明确指定 MIME 类型,并设置适当的缓存控制策略: ```nginx location ~ \.(jpg|jpeg|png|gif|pdf|doc|docx|xls|xlsx|ppt|pptx)$ { types {} default_type application/octet-stream; add_header Content-Disposition "attachment"; } location /upload/ { add_header Cache-Control "no-cache"; } ``` ### 6. 启用 SSL/TLS 加密(可选) 为了提高安全性,建议为上传服务器启用 SSL/TLS 加密。你需要获取 SSL 证书并配置 Nginx 使用 HTTPS: ```nginx server { listen 443 ssl; server_name example.com; ssl_certificate /etc/nginx/ssl/example.com.crt; ssl_certificate_key /etc/nginx/ssl/example.com.key; ssl_protocols TLSv1.2 TLSv1.3; ssl_ciphers HIGH:!aNULL:!MD5; location /upload/ { proxy_pass http://127.0.0.1:5000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } ``` ### 7. 重启 Nginx 服务 完成配置后,检查 Nginx 配置是否正确,并重启服务以应用更改: ```bash sudo nginx -t sudo systemctl restart nginx ``` ### 8. 测试文件上传功能 你可以使用 `curl` 命令测试文件上传功能: ```bash curl -X POST https://example.com/upload/ -F "file=@/path/to/file.txt" ``` 如果一切配置正确,文件应该能够成功上传到后端服务或指定的目录。 --- ###
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yongche_shi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值