首页 > 代码库 > 后台自定义下载限速控制

后台自定义下载限速控制

有时候为了做带宽控制,需要对服务器资源配置限速下载(如通过web服务器配置限速的配置参数),但一般这都是统一的对所有目录资源生效,使用上不够灵活。

我们可以自行开发一个限速模块,通过GET参数来定制每个资源的限速大小(注明:为了防止客户端修改限速参数值,可以增加校验参数,如生成下载链接时一并生成ts,加密密文等,这些不在本文涉及范围内,实现起来也很简单)

例如如下链接:

http://localhost/dl/500K                //服务器不限速http://localhost/dl/5M?limitrate=100    //限速100K/shttp://localhost/dl/50M?limitrate=1000  //限速1M/s

所以我们只需要在后台识别limitrate变量然后修改web服务器配置参数的内部变量数值即可

nginx的配置文件如下:

server {        listen       80;        server_name  localhost;        error_page   500 502 503 504  /50x.html;        location / {                root   html;                index  index.html index.htm;        }        location /dl {                limit_rate_var  "limitrate";        }}

我们实现limit_rate_var配置项的代码如下:

/* *  * Copyright (C) Ciaos *   */#include <ngx_config.h>#include <ngx_core.h>#include <ngx_http.h>typedef struct {        ngx_str_t       limit_rate_v;} ngx_http_limit_rate_v_conf_t;static void *ngx_http_limit_rate_v_create_conf(ngx_conf_t *cf);static char *ngx_http_limit_rate_v_merge_conf(ngx_conf_t *cf, void *parent,void *child);static ngx_int_t ngx_http_limit_rate_v_init(ngx_conf_t *cf);static ngx_int_t ngx_http_limit_rate_v_handler(ngx_http_request_t *r);static ngx_command_t  ngx_http_limit_rate_v_commands[] = {        { ngx_string("limit_rate_var"),                NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,                ngx_conf_set_str_slot,                NGX_HTTP_LOC_CONF_OFFSET,                offsetof(ngx_http_limit_rate_v_conf_t, limit_rate_v),                NULL },        ngx_null_command};static ngx_http_module_t  ngx_http_limit_rate_v_module_ctx = {        NULL,                                  /* preconfiguration */        ngx_http_limit_rate_v_init,              /* postconfiguration */        NULL,                                  /* create main configuration */        NULL,                                  /* init main configuration */        NULL,                                  /* create server configuration */        NULL,                                  /* merge server configuration */        ngx_http_limit_rate_v_create_conf,       /* create location configration */        ngx_http_limit_rate_v_merge_conf         /* merge location configration */};ngx_module_t  ngx_http_limit_rate_v_module = {        NGX_MODULE_V1,        &ngx_http_limit_rate_v_module_ctx,       /* module context */        ngx_http_limit_rate_v_commands,          /* module directives */        NGX_HTTP_MODULE,                       /* module type */        NULL,                                  /* init master */        NULL,                                  /* init module */        NULL,                                  /* init process */        NULL,                                  /* init thread */        NULL,                                  /* exit thread */        NULL,                                  /* exit process */        NULL,                                  /* exit master */        NGX_MODULE_V1_PADDING};static ngx_int_tngx_http_limit_rate_v_handler(ngx_http_request_t *r){        u_char *pos;        u_char  c1, c2;        u_char *s1, *s2;        size_t  n;        size_t  limit_val;        ngx_http_limit_rate_v_conf_t *clcf;        clcf = ngx_http_get_module_loc_conf(r, ngx_http_limit_rate_v_module);        if(clcf->limit_rate_v.len > 0 && r->args.len > 0){                s1 = r->args.data;                s2 = clcf->limit_rate_v.data;                n = clcf->limit_rate_v.len - 1;                c2 = *(u_char *) s2++;                do {                        do {                                c1 = *s1++;                                if (c1 == 0) {                                        pos = NULL;                                }                        } while (c1 != c2);                } while (ngx_strncmp(s1, (u_char *) s2, n) != 0);                if(*s1 != 0) {                        s1 = s1 + n;                        if(*s1 == =){                                limit_val = atoi((char *)++s1);                                r->limit_rate = limit_val * 1024;                        }                }        }        return NGX_DECLINED;}static void *ngx_http_limit_rate_v_create_conf(ngx_conf_t *cf){        ngx_http_limit_rate_v_conf_t  *clcf;        clcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_limit_rate_v_conf_t));        if (clcf == NULL) {                return NULL;        }        return clcf;}static char *ngx_http_limit_rate_v_merge_conf(ngx_conf_t *cf, void *parent, void *child){        ngx_http_limit_rate_v_conf_t *prev = parent;        ngx_http_limit_rate_v_conf_t *clcf = child;        ngx_conf_merge_str_value(clcf->limit_rate_v, prev->limit_rate_v, "");        return NGX_CONF_OK;}static ngx_int_tngx_http_limit_rate_v_init(ngx_conf_t *cf){        ngx_http_handler_pt        *h;        ngx_http_core_main_conf_t *cmcf;        cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);        h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers);        if (h == NULL) {                return NGX_ERROR;        }        *h = ngx_http_limit_rate_v_handler;        return NGX_OK;}

插件配置文件如下:

ngx_addon_name=ngx_http_limit_rate_v_moduleHTTP_AUX_FILTER_MODULES="$HTTP_AUX_FILTER_MODULES ngx_http_limit_rate_v_module"NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_limit_rate_v_module.c"

将插件编译到nginx可执行文件中即可

后台自定义下载限速控制