⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ngx_http_proxy_module.c

📁 nginx 反向代理0.7.1版本 用于实现反向代理
💻 C
📖 第 1 页 / 共 5 页
字号:
    if (conf->upstream.next_upstream & NGX_HTTP_UPSTREAM_FT_OFF) {        conf->upstream.next_upstream = NGX_CONF_BITMASK_SET                                       |NGX_HTTP_UPSTREAM_FT_OFF;    }    ngx_conf_merge_path_value(conf->upstream.temp_path,                              prev->upstream.temp_path,                              NGX_HTTP_PROXY_TEMP_PATH, 1, 2, 0,                              ngx_garbage_collector_temp_handler, cf);    if (conf->method.len == 0) {        conf->method = prev->method;    } else {        conf->method.data[conf->method.len] = ' ';        conf->method.len++;    }    ngx_conf_merge_value(conf->upstream.pass_request_headers,                              prev->upstream.pass_request_headers, 1);    ngx_conf_merge_value(conf->upstream.pass_request_body,                              prev->upstream.pass_request_body, 1);    ngx_conf_merge_value(conf->upstream.intercept_errors,                              prev->upstream.intercept_errors, 0);    ngx_conf_merge_value(conf->redirect, prev->redirect, 1);    if (conf->redirect) {        if (conf->redirects == NULL) {            conf->redirects = prev->redirects;        }        if (conf->redirects == NULL && conf->url.data) {            conf->redirects = ngx_array_create(cf->pool, 1,                                            sizeof(ngx_http_proxy_redirect_t));            if (conf->redirects == NULL) {                return NGX_CONF_ERROR;            }            pr = ngx_array_push(conf->redirects);            if (pr == NULL) {                return NGX_CONF_ERROR;            }            pr->handler = ngx_http_proxy_rewrite_redirect_text;            pr->redirect = conf->url;            if (conf->vars.uri.len) {                pr->replacement.text = conf->location;            } else {                pr->replacement.text.len = 0;                pr->replacement.text.data = NULL;            }        }    }    /* STUB */    if (prev->proxy_lengths) {        conf->proxy_lengths = prev->proxy_lengths;        conf->proxy_values = prev->proxy_values;    }    ngx_conf_merge_uint_value(conf->headers_hash_max_size,                              prev->headers_hash_max_size, 512);    ngx_conf_merge_uint_value(conf->headers_hash_bucket_size,                              prev->headers_hash_bucket_size, 64);    conf->headers_hash_bucket_size = ngx_align(conf->headers_hash_bucket_size,                                               ngx_cacheline_size);    hash.max_size = conf->headers_hash_max_size;    hash.bucket_size = conf->headers_hash_bucket_size;    hash.name = "proxy_headers_hash";    if (ngx_http_upstream_hide_headers_hash(cf, &conf->upstream,                                            &prev->upstream,                                            ngx_http_proxy_hide_headers, &hash)        != NGX_OK)    {        return NGX_CONF_ERROR;    }    if (conf->upstream.upstream == NULL) {        conf->upstream.upstream = prev->upstream.upstream;        conf->vars = prev->vars;        conf->upstream.schema = prev->upstream.schema;    }    if (conf->body_source.data == NULL) {        conf->body_source = prev->body_source;        conf->body_set_len = prev->body_set_len;        conf->body_set = prev->body_set;    }    if (conf->body_source.data && conf->body_set_len == NULL) {        ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));        sc.cf = cf;        sc.source = &conf->body_source;        sc.flushes = &conf->flushes;        sc.lengths = &conf->body_set_len;        sc.values = &conf->body_set;        sc.complete_lengths = 1;        sc.complete_values = 1;        if (ngx_http_script_compile(&sc) != NGX_OK) {            return NGX_CONF_ERROR;        }        if (conf->headers_source == NULL) {            conf->headers_source = ngx_array_create(cf->pool, 4,                                                    sizeof(ngx_keyval_t));            if (conf->headers_source == NULL) {                return NGX_CONF_ERROR;            }        }        s = ngx_array_push(conf->headers_source);        if (s == NULL) {            return NGX_CONF_ERROR;        }        s->key.len = sizeof("Content-Length") - 1;        s->key.data = (u_char *) "Content-Length";        s->value.len = sizeof("$proxy_internal_body_length") - 1;        s->value.data = (u_char *) "$proxy_internal_body_length";    }    if (conf->headers_source == NULL) {        conf->flushes = prev->flushes;        conf->headers_set_len = prev->headers_set_len;        conf->headers_set = prev->headers_set;        conf->headers_set_hash = prev->headers_set_hash;        conf->headers_source = prev->headers_source;    }    if (conf->headers_set_hash.buckets) {        return NGX_CONF_OK;    }    conf->headers_names = ngx_array_create(cf->pool, 4, sizeof(ngx_hash_key_t));    if (conf->headers_names == NULL) {        return NGX_CONF_ERROR;    }    if (conf->headers_source == NULL) {        conf->headers_source = ngx_array_create(cf->pool, 4,                                                sizeof(ngx_keyval_t));        if (conf->headers_source == NULL) {            return NGX_CONF_ERROR;        }    }    conf->headers_set_len = ngx_array_create(cf->pool, 64, 1);    if (conf->headers_set_len == NULL) {        return NGX_CONF_ERROR;    }    conf->headers_set = ngx_array_create(cf->pool, 512, 1);    if (conf->headers_set == NULL) {        return NGX_CONF_ERROR;    }    src = conf->headers_source->elts;    for (h = ngx_http_proxy_headers; h->key.len; h++) {        for (i = 0; i < conf->headers_source->nelts; i++) {            if (ngx_strcasecmp(h->key.data, src[i].key.data) == 0) {                goto next;            }        }        s = ngx_array_push(conf->headers_source);        if (s == NULL) {            return NGX_CONF_ERROR;        }        *s = *h;        src = conf->headers_source->elts;    next:        continue;    }    src = conf->headers_source->elts;    for (i = 0; i < conf->headers_source->nelts; i++) {        hk = ngx_array_push(conf->headers_names);        if (hk == NULL) {            return NGX_CONF_ERROR;        }        hk->key = src[i].key;        hk->key_hash = ngx_hash_key_lc(src[i].key.data, src[i].key.len);        hk->value = (void *) 1;        if (src[i].value.len == 0) {            continue;        }        if (ngx_http_script_variables_count(&src[i].value) == 0) {            copy = ngx_array_push_n(conf->headers_set_len,                                    sizeof(ngx_http_script_copy_code_t));            if (copy == NULL) {                return NGX_CONF_ERROR;            }            copy->code = (ngx_http_script_code_pt)                                                 ngx_http_script_copy_len_code;            copy->len = src[i].key.len + sizeof(": ") - 1                        + src[i].value.len + sizeof(CRLF) - 1;            size = (sizeof(ngx_http_script_copy_code_t)                       + src[i].key.len + sizeof(": ") - 1                       + src[i].value.len + sizeof(CRLF) - 1                       + sizeof(uintptr_t) - 1)                    & ~(sizeof(uintptr_t) - 1);            copy = ngx_array_push_n(conf->headers_set, size);            if (copy == NULL) {                return NGX_CONF_ERROR;            }            copy->code = ngx_http_script_copy_code;            copy->len = src[i].key.len + sizeof(": ") - 1                        + src[i].value.len + sizeof(CRLF) - 1;            p = (u_char *) copy + sizeof(ngx_http_script_copy_code_t);            p = ngx_cpymem(p, src[i].key.data, src[i].key.len);            *p++ = ':'; *p++ = ' ';            p = ngx_cpymem(p, src[i].value.data, src[i].value.len);            *p++ = CR; *p = LF;        } else {            copy = ngx_array_push_n(conf->headers_set_len,                                    sizeof(ngx_http_script_copy_code_t));            if (copy == NULL) {                return NGX_CONF_ERROR;            }            copy->code = (ngx_http_script_code_pt)                                                 ngx_http_script_copy_len_code;            copy->len = src[i].key.len + sizeof(": ") - 1;            size = (sizeof(ngx_http_script_copy_code_t)                    + src[i].key.len + sizeof(": ") - 1 + sizeof(uintptr_t) - 1)                    & ~(sizeof(uintptr_t) - 1);            copy = ngx_array_push_n(conf->headers_set, size);            if (copy == NULL) {                return NGX_CONF_ERROR;            }            copy->code = ngx_http_script_copy_code;            copy->len = src[i].key.len + sizeof(": ") - 1;            p = (u_char *) copy + sizeof(ngx_http_script_copy_code_t);            p = ngx_cpymem(p, src[i].key.data, src[i].key.len);            *p++ = ':'; *p = ' ';            ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));            sc.cf = cf;            sc.source = &src[i].value;            sc.flushes = &conf->flushes;            sc.lengths = &conf->headers_set_len;            sc.values = &conf->headers_set;            if (ngx_http_script_compile(&sc) != NGX_OK) {                return NGX_CONF_ERROR;            }            copy = ngx_array_push_n(conf->headers_set_len,                                    sizeof(ngx_http_script_copy_code_t));            if (copy == NULL) {                return NGX_CONF_ERROR;            }            copy->code = (ngx_http_script_code_pt)                                                 ngx_http_script_copy_len_code;            copy->len = sizeof(CRLF) - 1;            size = (sizeof(ngx_http_script_copy_code_t)                    + sizeof(CRLF) - 1 + sizeof(uintptr_t) - 1)                    & ~(sizeof(uintptr_t) - 1);            copy = ngx_array_push_n(conf->headers_set, size);            if (copy == NULL) {                return NGX_CONF_ERROR;            }            copy->code = ngx_http_script_copy_code;            copy->len = sizeof(CRLF) - 1;            p = (u_char *) copy + sizeof(ngx_http_script_copy_code_t);            *p++ = CR; *p = LF;        }        code = ngx_array_push_n(conf->headers_set_len, sizeof(uintptr_t));        if (code == NULL) {            return NGX_CONF_ERROR;        }        *code = (uintptr_t) NULL;        code = ngx_array_push_n(conf->headers_set, sizeof(uintptr_t));        if (code == NULL) {            return NGX_CONF_ERROR;        }        *code = (uintptr_t) NULL;    }    code = ngx_array_push_n(conf->headers_set_len, sizeof(uintptr_t));    if (code == NULL) {        return NGX_CONF_ERROR;    }    *code = (uintptr_t) NULL;    hash.hash = &conf->headers_set_hash;    hash.key = ngx_hash_key_lc;    hash.max_size = conf->headers_hash_max_size;    hash.bucket_size = conf->headers_hash_bucket_size;    hash.name = "proxy_headers_hash";    hash.pool = cf->pool;    hash.temp_pool = NULL;    if (ngx_hash_init(&hash, conf->headers_names->elts,                      conf->headers_names->nelts)        != NGX_OK)    {        return NGX_CONF_ERROR;    }    return NGX_CONF_OK;}static char *ngx_http_proxy_pass(ngx_conf_t *cf, ngx_command_t *cmd, void *conf){    ngx_http_proxy_loc_conf_t *plcf = conf;    size_t                      add;    u_short                     port;    ngx_str_t                  *value, *url;    ngx_url_t                   u;    ngx_uint_t                  n;    ngx_http_core_loc_conf_t   *clcf;    ngx_http_script_compile_t   sc;    if (plcf->upstream.schema.len) {        return "is duplicate";    }    clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);    value = cf->args->elts;    url = &value[1];    n = ngx_http_script_variables_count(url);    if (n) {        ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));        sc.cf = cf;        sc.source = url;        sc.lengths = &plcf->proxy_lengths;        sc.values = &plcf->proxy_values;        sc.variables = n;        sc.complete_lengths = 1;        sc.complete_values = 1;        if (ngx_http_script_compile(&sc) != NGX_OK) {            return NGX_CONF_ERROR;        }#if (NGX_HTTP_SSL)        if (ngx_http_proxy_set_ssl(cf, plcf) != NGX_OK) {            return NGX_CONF_ERROR;        }#endif        clcf->handler = ngx_http_proxy_handler;        return NGX_CONF_OK;    }    if (ngx_strncasecmp(url->data, (u_char *) "http://", 7) == 0) {        add = 7;        port = 80;    } else if (ngx_strncasecmp(url->data, (u_char *) "https://", 8) == 0) {#if (NGX_HTTP_SSL)        if (ngx_http_proxy_set_ssl(cf, plcf) != NGX_OK) {            return NGX_CONF_ERROR;        }        add = 8;        port = 443;#else        ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,                           "https protocol requires SSL support");        return NGX_CONF_ERROR;#endif    } else {        ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid URL prefix");        return NGX_CONF_ERROR;    }    ngx_memzero(&u, sizeof(ngx_url_t));    u.url.len = url->len - add;    u.url.data = url->data + add;    u.default_port = port;    u.uri_part = 1;    u.no_resolve = 1;    plcf->upstream.upstream = ngx_http_upstream_add(cf, &u, 0);    if (plcf->upstream.upstream == NULL) {        return NGX_CONF_ERROR;    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -