centerOS 6.5安装nginx并添加nginx-upload-module断点续传模块

第一步:安装nginx:
1,如果没有安装pcre和openssl需要先安装:

  yum -y install pcre*
  yum -y install openssl* 

2,下载nginx-1.7.8,例如我的nginx下载在/tmp文件夹下:

    wget http://nginx.org/download/nginx-1.7.8.tar.gz

3,解压编译安装:
进入/tmp文件夹,解压nginx:

   tar -zxvf nginx-1.7.8.tar.gz

进入解压后的nginx-1.7.8文件夹:
进行编译安装:

进入文件夹:cd nginx-1.7.8   执行下面的三条命令:

./configure  
make
make install

(此过程默认把nginx安装在/usr/local文件夹下,如果想指定安装路径 ./configure --prefix=path)

4,安装完成之后,重启nginx:(nginx的默认端口是80)

/usr/local/nginx/sbin/nginx -s reload
如果重启时候出错报:
nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)
执行下面的命令:
 /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

5,关闭防火墙,或者添加防火墙规则:
A 关闭防火墙:

service iptables stop

B 或者添加防火墙规则:

vi /etc/sysconfig/iptables
编辑防火墙文件

添加这样一条开放80端口的规则后保存:
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

重启服务即可:
service iptables restart

第二步,添加nginx-upload-module模块
1,下载模块,下载到/tmp:

cd /tmp
wget https://codeload.github.com/vkholodkov/nginx-upload-module/zip/2.2
unzip 2.2

2,安装模块:

进入到nginx源码所在的文件夹,就是我刚才的nginx解压后的文件夹,/tmp/nginx-1.7.8 执行下面两条指令:

.configure --add-module=/tmp/nginx-upload-module-2.2/

make //注意,如果是添加模块,只需要make,不要执行make install 否则会覆盖安装。

3,配置 nginx的配置文件(在/usr/local/nginx/conf路径下),添加以下规则:

server {
[...]
        location /resumable_upload {
                #开启断点续传功能
                upload_resumable on; 
                #断点续传临时目录(必须存在)
                upload_state_store /usr/local/nginx/upload_temp ;
                #文件保存目录(必须存在)
                upload_store /usr/local/nginx/upload_temp;
                upload_set_form_field $upload_field_name.path "$upload_tmp_path";
        }
[...]
}

配置完之后,需要重启nginx

3,测试:
1,开启日志可以查看上传情况:

  在/usr/local/nginx/logs下有日志文件access.logerror.log:
     开启两个终端查看日志:
     查看access.log:   tail -f access.log
     查看error.log:    tail -f error.log

2,编辑上传示例的python文件;
ptyhon源码:

#!/usr/bin/python
# -*- coding: utf-8 -*- 


import os.path
import requests
import hashlib

# 待上传文件路径
FILE_UPLOAD = "/tmp/test"
# 上传接口地址
UPLOAD_URL = "http://localhost:80/resumable_upload"


def upload(fp, file_pos, size, file_size):
    session_id = get_session_id()
    fp.seek(file_pos)
    payload = fp.read(size)
    content_range = "bytes {file_pos}-{pos_end}/{file_size}".format(file_pos=file_pos,
                    pos_end=file_pos+size-1,file_size=file_size)
    headers = {'Content-Disposition': 'attachment; filename="big.TXT"','Content-Type': 'application/octet-stream',
                'X-Content-Range':content_range,'Session-ID': session_id,'Content-Length':str(size)}
    res = requests.post(UPLOAD_URL, data=payload, headers=headers)
    print(res.text)


# 根据文件名hash获得session id
def get_session_id():
    m = hashlib.md5()
    file_name = os.path.basename(FILE_UPLOAD)
    m.update(file_name)
    return m.hexdigest()

def main():

    file_pos = 0

    # 单个片段上传的字节数
    file_s = 8

    file_size = os.path.getsize(FILE_UPLOAD)
    fp = open(FILE_UPLOAD,"r")

    while True:
        if file_pos + file_s>= file_size:
            upload(fp, file_pos, file_size - file_pos, file_size)
            fp.close()
            break
        else:
            upload(fp, file_pos, file_s, file_size)
            file_pos = file_pos + file_s

if __name__ == "__main__":
    main()

执行 python文件,就能看到日志打印结果,以及在上传文件夹下找到上传的文件。
参考内容:

http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=29791971&id=4702007
https://www.centos.bz/2015/09/nginx-upload-module-multipart-form-data-resumable/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值