为用户提供视频服务,除了原始文件外,一般都要转为更加常规的.mp4格式,以更好的支持用户的观看。视频文件又比较大,可能会消耗服务器大量的资源(存储、带宽等),常规的模式是将这些文件存储到云存储服务中。
在Django中,一般会采用如下的架构来部署一个网站:
nginx => uWSGI => S3
要实现开头提到的问题,需要解决:
-
自动上传到云存储(S3)
-
上传云存储之前转码
在Django中,可以自定义一个FileStorage来处理用户的上传文件,根据自己的要求上传到相应的位置。django-storages就是提供了一组Storage模块,他支持S3、CloudDB、FTP等服务,只需要简单的配置即可。第一个问题很方便的就可以解决,但是上传的文件会被自动传到S3,如果我们在上传完毕后安排一个转码任务,那么必须要重新将上传到S3的源文件下载下来,经过转码后重新上传到S3上。这无疑至少会增加一个GET操作,时间与带宽的消耗也会相应增加。能否现将文件存储到本地,转码后统一传到S3上呢?
合理的方案是,在文件上传后,以及将上传文件转给Django FileStorage(这里是django-storages中的某个模块)处理之前,进行拦截处理。nginx upload module提供了这样一个拦截上传文件的机制。
我们只需要将最终的流程设定为如下方式即可实现我们的需求:
nginx => nginx-upload(将文件存储到本地) => 转交到后台应用(django) => 安排转码及s3上传任务
具体实现:
-
手动编译nginx,添加upload-module
wget http://nginx.org/download/nginx-1.4.5.tar.gz tar xzvf nginx-1.4.5.tar.gz wget https://github.com/vkholodkov/nginx-upload-module/archive/2.2.zip unzip 2.2.zip cd nginx-1.4.5 ./configure \ --prefix=/usr/share/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 \ --http-client-body-temp-path=/var/lib/nginx/tmp/client_body \ --http-proxy-temp-path=/var/lib/nginx/tmp/proxy \ --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi \ --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi \ --http-scgi-temp-path=/var/lib/nginx/tmp/scgi \ --pid-path=/var/run/nginx.pid \ --lock-path=/var/lock/subsys/nginx \ --user=nginx \ --group=nginx \ --with-file-aio \ --with-ipv6 \ --with-http_ssl_module \ --with-http_spdy_module \ --with-http_realip_module \ --with-http_addition_module \ --with-http_geoip_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_degradation_module \ --with-http_stub_status_module \ --with-pcre \ --with-google_perftools_module \ --add-module=../nginx-upload-module/ \ --add-module=../nginx-upload-progress-module/ \ --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' \ --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E' make & sudo make install
-
安装nginx的服务脚本
#!/bin/sh # # nginx - this script starts and stops the nginx daemon # # chkconfig: - 85 15 # description: Nginx is an HTTP(S) server, HTTP(S) reverse \ # proxy and IMAP/POP3 proxy server # processname: nginx # config: /etc/nginx/nginx.conf # config: /etc/sysconfig/nginx # pidfile: /var/run/nginx.pid # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0 nginx="/usr/sbin/nginx" prog=$(basename $nginx) sysconfig="/etc/sysconfig/$prog" lockfile="/var/lock/subsys/nginx" pidfile="/var/run/${prog}.pid" NGINX_CONF_FILE="/etc/nginx/nginx.conf" [ -f $sysconfig ] && . $sysconfig start() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 echo -n $"Starting $prog: " daemon $nginx -c $NGINX_CONF_FILE retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval } stop() { echo -n $"Stopping $prog: " killproc -p $pidfile $prog retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } restart() { configtest_q || return 6 stop start } reload() { configtest_q || return 6 echo -n $"Reloading $prog: " killproc -p $pidfile $prog -HUP echo } configtest() { $nginx -t -c $NGINX_CONF_FILE } configtest_q() { $nginx -t -q -c $NGINX_CONF_FILE } rh_status() { status $prog } rh_status_q() { rh_status >/dev/null 2>&1 } # Upgrade the binary with no downtime. upgrade() { local oldbin_pidfile="${pidfile}.oldbin" configtest_q || return 6 echo -n $"Upgrading $prog: " killproc -p $pidfile $prog -USR2 retval=$? sleep 1 if [[ -f ${oldbin_pidfile} && -f ${pidfile} ]]; then killproc -p $oldbin_pidfile $prog -QUIT success $"$prog online upgrade" echo return 0 else failure $"$prog online upgrade" echo return 1 fi } # Tell nginx to reopen logs reopen_logs() { configtest_q || return 6 echo -n $"Reopening $prog logs: " killproc -p $pidfile $prog -USR1 retval=$? echo return $retval } case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart|configtest|reopen_logs) $1 ;; force-reload|upgrade) rh_status_q || exit 7 upgrade ;; reload) rh_status_q || exit 7 $1 ;; status|status_q) rh_$1 ;; condrestart|try-restart) rh_status_q || exit 7 restart ;; *) echo $"Usage: $0 {start|stop|reload|configtest|status|force-reload|upgrade|restart|reopen_logs}" exit 2 esac
sudo cp nginx /etc/init.d sudo chmod +x /etc/init.d sudo chkconfig --add nginx sudo chkconfig nginx on
-
配置nginx-upload
#文件上传到此地 location /dynamic/photo/share { upload_pass /share_photo; upload_pass_args on; # 将上传的文件保存到这个目录下 # 目录是被散列化的,应该存在子目录 0 1 2 3 4 5 6 7 8 9 upload_store /tmp/nginx_upload ; # 设置请求体的字段(添加自己后端处理的信息) upload_set_form_field "${upload_field_name}_name" $upload_file_name; upload_set_form_field "${upload_field_name}_content_type" $upload_content_type; upload_set_form_field "${upload_field_name}_path" $upload_tmp_path; # 指示后端关于上传文件的md5值和文件大小 upload_aggregate_form_field "${upload_field_name}_md5" $upload_file_md5; upload_aggregate_form_field "${upload_field_name}_size" $upload_file_size; # 指示原样转到后端的参数,可以正则表达式表示 upload_pass_form_field "^.*$"; } #上传完毕后转到此地址 location /share_photo { #将请求专递给后台应用 proxy_pass http://localhost:8000/dynamic/photo/share/; }
传到后台应用的一个示例:
-
安排转码及s3上传任务