爲用戶提供視頻服務,除了原始文件外,一般都要轉爲更加常規的.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上傳任務