最近准备使用nginx的上传文件的功能,居然花了两天,其中走过的弯路将不在赘述。
系统:ubuntu
在ubuntu安装nginx还算是蛮容易的:sudo apt-get install nginx
一条命令就可以解决。
但是如果要使用上传模块就必须重新编译,这也就意味着原来使用apt-get安装的nginx需要卸载掉包括配置文件:````sudo apt-get autoremove --purge nginx```
然后下载nginx上传所需要的模块:
cd
mkdir tmp && cd tmp
git clone -b 2.2 https://github.com/vkholodkov/nginx-upload-module.git
git clone https://github.com/masterzen/nginx-upload-progress-module.git
将nginx的源码下载下来:
sudo mkdir /opt/rebuildnginx
cd /opt/rebuildnginx
sudo apt-get source nginx
安装依赖:
sudo apt-get install libpcre3 libpcre3-dev
可能还需要
sudo apt-get install openssl libssl-dev
开始编译:
sudo ./configure --with-cc-opt='-g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-ipv6 --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_spdy_module --with-http_sub_module --with-http_xslt_module --with-mail --with-mail_ssl_module --add-module=/home/xxx/tmp/nginx-upload-module --add-module=/home/xxx/tmp/nginx-upload-progress-module
sudo make && make install
注意编译最后要加上:
--add-module=/home/yzh/tmp/nginx-upload-module --add-module=/home/yzh/tmp/nginx-upload-progress-module
可进行打包:
sudo apt-get install checkinstall #下载打包工具
sudo checkinstall -D -y -install=no -default make install
使用dpkg进行安装:
sudo dpkg -i nginx_1.4.6-1_amd64.deb
dpkg安装时可能会遇到这个问题
dpkg: error processing archive nginx_1.4.6-1_amd64.deb (--install):
trying to overwrite '/etc/nginx/win-utf', which is also in package nginx-common 1.10.0-0ubuntu0.16.04.4
Errors were encountered while processing:
nginx_1.4.6-1_amd64.deb
这样的话只要安装时加一个参数即可,这个做法会将之前的nginx覆盖:
sudo dpkg -i --force-overwrite nginx_1.4.6-1_amd64.deb
在配置nginx之前:
需要将其他服务器/etc/init.d/nginx 这个文件拷贝过来,因为编译安装的nginx并没有添加进service 启动较为麻烦:
考过来之后需要改两处地方:
在PATH最后添加/usr/share/nginx/sbin:
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/share/nginx/sbin:
修改为:
DAEMON=/usr/share/nginx/sbin/nginx
现在就可以重新启动nginx了 sudo /etc/init.d/nginx restart
配置nginx文件:
http{
upload_progress proxied 1m;
include /etc/nginx/conf.d/*.conf;
server {
listen 8889;
client_header_timeout 1m;
client_body_timeout 1m;
proxy_connect_timeout 60s;
proxy_read_timeout 1m;
proxy_send_timeout 1m;
client_max_body_size 50M; #限制文件大小
location / {
proxy_pass http://127.0.0.1:9000; # 后端程序
}
location ^~ /upload {
upload_pass /api/upload; # 后端处理文件的地址,此处地址不应和上面的/upload地址一样
upload_resumable on;
upload_cleanup 400 413 404 499 500-505;
upload_store /tmp/upload_tmp;
upload_store_access user:rw all:rw;
upload_limit_rate 0;
upload_set_form_field "file_name" $upload_file_name;
upload_set_form_field "content_type" $upload_content_type;
upload_set_form_field "tmp_path" $upload_tmp_path;
upload_aggregate_form_field "md5" $upload_file_md5;
upload_aggregate_form_field "size" $upload_file_size;
upload_pass_form_field "^.*$";
upload_pass_args on; #打开开关,意思就是把前端脚本请求的参数会传给后端
track_uploads proxied 30s; # 必须放最后一行
}
location ^~ /progress {
upload_progress_json_output; # 以json的数据格式输出
report_uploads proxied;
}
}
}
上传文件时,action="/upload?X-Progress-ID=FmN6gTEshAHCcUvR"
其中X-Progress-ID需要是唯一的id,用来上传时获取进度的id
获取上传进度需向/progress发送一个get请求 /process?X-Progress-ID=FmN6gTEshAHCcUvR
即可获得上传的进度
参考:
使用Nginx Upload Module实现上传文件功能
How to: Build your own version of NginX