最近準備使用nginx的上傳文件的功能之拨,居然花了兩天贼穆,其中走過的彎路將不在贅述洼畅。
系統(tǒng):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
這樣的話只要安裝時加一個參數(shù)即可缴挖,這個做法會將之前的nginx覆蓋:
sudo dpkg -i --force-overwrite nginx_1.4.6-1_amd64.deb
在配置nginx之前:
需要將其他服務(wù)器/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
現(xiàn)在就可以重新啟動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; # 后端處理文件的地址瘫析,此處地址不應(yīng)和上面的/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; #打開開關(guān)咸包,意思就是把前端腳本請求的參數(shù)會傳給后端
track_uploads proxied 30s; # 必須放最后一行
}
location ^~ /progress {
upload_progress_json_output; # 以json的數(shù)據(jù)格式輸出
report_uploads proxied;
}
}
}
上傳文件時,action="/upload?X-Progress-ID=FmN6gTEshAHCcUvR"
其中X-Progress-ID需要是唯一的id坟比,用來上傳時獲取進度的id
獲取上傳進度需向/progress發(fā)送一個get請求 /process?X-Progress-ID=FmN6gTEshAHCcUvR
即可獲得上傳的進度
參考:
使用Nginx Upload Module實現(xiàn)上傳文件功能
How to: Build your own version of NginX