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

本文详细介绍如何在Nginx中配置文件上传功能,并解决编译过程中遇到的问题,包括安装必要的模块、设置编译选项及配置文件上传路径等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

发现了这篇blog,准备使用nginx来实现文件上传和下载的功能。下面把实现步骤记录下来,中间有很多坑。

参考文档:

https://www.cnblogs.com/lidabo/p/4171515.html (good example)

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

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

http://www.codeweblog.com/文件上传从tornado迁移到nginx-upload-module/

https://www.cnblogs.com/zhanchenjin/p/5076466.html (good example analysis format)

安装要求:

1. centos7.x,

2. 系统安装了必要的编译工具

3. 下载的代码都放在/root/thb

4.本次nginx用1.13.9,

构建步骤:

1、下载nginx

# cd /root/thb; wget  http://nginx.org/download/nginx-1.13.9.tar.gz  

# tar zxvf nginx-1.13.9.tar.gz

2、下载nginx upload module

下载地址: https://github.com/hongzhidao/nginx-upload-module

别用 http://www.grid.net.ru/nginx/download/nginx_upload_module-2.2.0.tar.gz,它太老了。

把下载后的代码都要解压到  /root/thb下面。

3、下载nginx-upload-progress-module

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

# mv nginx-upload-progress-module /root/thb

4. 如果安装了nginx,则要先卸载目前的版本。

  # yum remove nginx  

5. 安装必要的编译工具

# yum -y install gcc gcc-c++ autoconf automake gd gd-devel zlib zlib-devel openssl openssl-devel pcre-devel 

6、编译nginx

# cd /root/thb/nginx-1.13.9; 
./configure  --with-debug --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --add-module=/root/thb/nginx-upload-module-master --add-module=/root/thb/nginx-upload-progress-module --with-stream --with-http_image_filter_module --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-file-aio --with-cc-opt='-Wno-format-security -Wno-unused-but-set-variable -Wno-unused-result -D NGX_HAVE_OPENSSL_MD5_H=1 -D NGX_OPENSSL_MD5=1 -D NGX_HAVE_OPENSSL_SHA1_H=1 -O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2  -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic'

注: 可能出现的编译错误及其解决方案.

0:编译 nginx-upload-module时候,如果出现 md5.h找不到的话,则必须确保你安装了openssl,且确保你的编译选择用了如下的选择(直接用上面的配置就OK)

-D NGX_HAVE_OPENSSL_MD5_H=1 -D NGX_OPENSSL_MD5=1 

1; 出现如下错误:./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.

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

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

 # yum install -y pcre-devel

2:出现如下错误:./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

7. 构建nginx

# make 

如果构建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

  # make install

说明,上面执行完毕后,nginx会安装到 /usr/sbin/, 配置文件在 /etc/nginx下面。

如果在CentOS要以服务化来管理nginx,则还要执行下面的步骤

8. CentOS的服务化管理nginx 

# vi /lib/systemd/system/nginx.service, 内容如下。

[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target
# systemctl enable nginx.service
# systemctl start nginx
# systemctl status nginx

到此为止,nginx就可以运行了。下面说如何配置文件上传功能。

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

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

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

10. 上传进度获取在下面的这个blog说明

https://blog.youkuaiyun.com/bigtree_3721/article/details/79879072

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值