当前位置: 首页 > news >正文

阐述电子商务网站的建设要求肇庆网站快速排名优化

阐述电子商务网站的建设要求,肇庆网站快速排名优化,html家乡网站设计模板,bcw公关公司1.模块依赖2. 模块的初始化2.1 location的定义location的定义包含以下几种location [ | ~ | ~* | ^~ ] uri { ... } location name { ... }:表示精确匹配,只有请求的url路径与后面的字符串完全相等时,才会命中,不支持location嵌套~&#xff…

1.模块依赖

2. 模块的初始化

2.1 location的定义

location的定义包含以下几种

location [ = | ~ | ~* | ^~ ] uri { ... }
location @name { ... }

=:表示精确匹配,只有请求的url路径与后面的字符串完全相等时,才会命中,不支持location嵌套

~:表示使用正则定义的,区分大小写

~*:表示是使用正则定义的,不区分大小写

^~:表示该符号后面的字符是最佳匹配,采用该规则,不再进行后续的查找

@name:用于定义一个内部 Location 块,该块不能被外部 Client 所访问,只能被 NGINX 内部配置指令所访问,比如 try_files 或者error_page。其修饰的location不能嵌套到其它location,也不能再嵌套其它location,即只能是server这一层的

2.2 分配ngx_http_conf_ctx_t

2.2.1 ngx_http_block

其是在解析配置文件中的http分配

ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t));
ctx->main_conf = ngx_pcalloc(cf->pool,sizeof(void *) * ngx_http_max_module);
ctx->srv_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
ctx->loc_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);

2.2.2 ngx_http_core_location

其是在解析配置文件中的http块内location时分配

其中main_conf,srv_conf是延用上一层级的,loc_conf会再一次分配内存

ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t));
pctx = cf->ctx;
ctx->main_conf = pctx->main_conf;
ctx->srv_conf = pctx->srv_conf;
ctx->loc_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);

同时也会遍历模块调用create_loc_conf创建location的配置

for (i = 0; cf->cycle->modules[i]; i++) {if (cf->cycle->modules[i]->type != NGX_HTTP_MODULE) {continue;}module = cf->cycle->modules[i]->ctx;if (module->create_loc_conf) {ctx->loc_conf[cf->cycle->modules[i]->ctx_index] =module->create_loc_conf(cf);if (ctx->loc_conf[cf->cycle->modules[i]->ctx_index] == NULL) {return NGX_CONF_ERROR;}}}

设置http_core_module配置的loc_conf来源

clcf = ctx->loc_conf[ngx_http_core_module.ctx_index];
clcf->loc_conf = ctx->loc_conf;

2.3 ngx_http_add_location

构造ngx_http_location_queue_t,将当前ngx_http_core_loc_conf_t添加到上一层级ngx_http_core_loc_conf_t中的location队列中。如果是精确匹配,正则,有名或者是无名,构造的ngx_http_location_queue_t的exact来存放ngx_http_core_loc_conf_t配置,否则使用ngx_http_location_queue_t的inclusive来存放ngx_http_core_loc_conf_t配置

if (clcf->exact_match
#if (NGX_PCRE)|| clcf->regex
#endif|| clcf->named || clcf->noname){lq->exact = clcf;lq->inclusive = NULL;} else {lq->exact = NULL;lq->inclusive = clcf;}

将构造的队列添加到上一层级的队列中

ngx_queue_insert_tail(*locations, &lq->queue);

2.4 主配置中的server

在ngx_http_block中会分配main_conf

ctx->main_conf = ngx_pcalloc(cf->pool,sizeof(void *) * ngx_http_max_module);

在处理server时(ngx_http_core_server),当前层的ctx中的main_conf会延用上一层的main_conf

http_ctx = cf->ctx;
ctx->main_conf = http_ctx->main_conf;

将server放入cmcf->server中

cscf = ctx->srv_conf[ngx_http_core_module.ctx_index];
cscf->ctx = ctx;cmcf = ctx->main_conf[ngx_http_core_module.ctx_index];cscfp = ngx_array_push(&cmcf->servers);
if (cscfp == NULL) {return NGX_CONF_ERROR;
}*cscfp = cscf;

2.5 初始化location(ngx_http_init_locations)

处理ngx_http_core_srv_conf_t中的有名location(named_locations)以及ngx_http_core_loc_conf_t中的正则location(regex_locations),在作割裂之前,会先对ngx_http_core_loc_conf_t中的locations排序,使用的排序规则为ngx_http_cmp_locations,即按照exact(sorted) -> inclusive(sorted) -> regex -> named -> noname的原则进行排序,经过处理后,原先的location队列就只剩下经过排序后的exact以及inclusive类型的location了。这两类location对应配置文件中的定义,就是不含修饰符的location,带有=和^~前缀的location。

2.6 将queue转为list(ngx_http_create_locations_list)

将locations queue变成locations list

2.7 创建location的二叉查找树(ngx_http_create_locations_tree)

创建精确匹配location的二叉查找树,使用ngx_queue_middle(其时间度为O(n))得到location_list中的中间位置,如果location_list的元素个数为奇数,则是中间的一个,否则是后半部分的第一个。

ngx_queue_t *
ngx_queue_middle(ngx_queue_t *queue)
{ngx_queue_t  *middle, *next;middle = ngx_queue_head(queue);if (middle == ngx_queue_last(queue)) {return middle;}next = ngx_queue_head(queue);for ( ;; ) {middle = ngx_queue_next(middle);next = ngx_queue_next(next);if (next == ngx_queue_last(queue)) {return middle;}next = ngx_queue_next(next);if (next == ngx_queue_last(queue)) {return middle;}}
}

使用递归来构建二叉查找树

/** to keep cache locality for left leaf nodes, allocate nodes in following* order: node, left subtree, right subtree, inclusive subtree*/static ngx_http_location_tree_node_t *
ngx_http_create_locations_tree(ngx_conf_t *cf, ngx_queue_t *locations,size_t prefix)
{size_t                          len;ngx_queue_t                    *q, tail;ngx_http_location_queue_t      *lq;ngx_http_location_tree_node_t  *node;q = ngx_queue_middle(locations);lq = (ngx_http_location_queue_t *) q;len = lq->name->len - prefix;node = ngx_palloc(cf->pool,offsetof(ngx_http_location_tree_node_t, name) + len);if (node == NULL) {return NULL;}node->left = NULL;node->right = NULL;node->tree = NULL;node->exact = lq->exact;node->inclusive = lq->inclusive;node->auto_redirect = (u_char) ((lq->exact && lq->exact->auto_redirect)|| (lq->inclusive && lq->inclusive->auto_redirect));node->len = (u_short) len;ngx_memcpy(node->name, &lq->name->data[prefix], len);ngx_queue_split(locations, q, &tail);if (ngx_queue_empty(locations)) {/** ngx_queue_split() insures that if left part is empty,* then right one is empty too*/goto inclusive;}node->left = ngx_http_create_locations_tree(cf, locations, prefix);if (node->left == NULL) {return NULL;}ngx_queue_remove(q);if (ngx_queue_empty(&tail)) {goto inclusive;}node->right = ngx_http_create_locations_tree(cf, &tail, prefix);if (node->right == NULL) {return NULL;}inclusive:if (ngx_queue_empty(&lq->list)) {return node;}node->tree = ngx_http_create_locations_tree(cf, &lq->list, prefix + len);if (node->tree == NULL) {return NULL;}return node;
}

2.8 http的处理阶段

包含11个阶段

枚举

名称

NGX_HTTP_POST_READ_PHASE

在接收到完整的HTTP头部后处理的HTTP阶段

NGX_HTTP_SERVER_REWRITE_PHASE

在将请求的URI与location表达式匹配前, 修改请求的URI(所谓的重定向) 是一个独立的HTTP阶段

NGX_HTTP_FIND_CONFIG_PHASE

根据请求的URI寻找匹配的location表达式, 这个阶段只能由ngx_http_core_module模块实现, 不建议其他HTTP模块重新定义这一阶段的行为

NGX_HTTP_REWRITE_PHASE

在NGX_HTTP_FIND_CONFIG_PHASE阶段寻找到匹配的location之后再修改请求的URI

NGX_HTTP_POST_REWRITE_PHASE

这一阶段是用于在rewrite重写URL后, 防止错误的

nginx.conf配置导致死循环(递归地修改URI) , 因此, 这一阶段仅由ngx_http_core_module模块处理。 目前, 控制死循环的方式很简单, 首先检查

rewrite的次数, 如果一个请求超过10次重定向

,就认为进入了rewrite死循环, 这时在

NGX_HTTP_POST_REWRITE_PHASE阶段就会向用户返回500, 表示服务器内部错误

NGX_HTTP_PREACCESS_PHASE

表示在处理NGX_HTTP_ACCESS_PHASE阶段决定请求的访问权限前HTTP模块可以介入的处理阶段

NGX_HTTP_ACCESS_PHASE

这个阶段用于让HTTP模块判断是否允许这个请求访问

Nginx服务器

NGX_HTTP_POST_ACCESS_PHASE

在NGX_HTTP_ACCESS_PHASE阶段中, 当

HTTP模块的handler处理函数返回不允许访问的错误码时(实际就是NGX_HTTP_FORBIDDEN或者

NGX_HTTP_UNAUTHORIZED) , 这里将负责向用户发送拒绝服务的错误响应。 因此, 这个阶段实际上用于给NGX_HTTP_ACCESS_PHASE阶段收尾

NGX_HTTP_PRECONTENT_PHASE

http请求内容前置处理

NGX_HTTP_CONTENT_PHASE

用于处理HTTP请求内容的阶段, 这是大部分

HTTP模块最愿意介入的阶段

NGX_HTTP_LOG_PHASE

处理完请求后记录日志的阶段

2.9 阶段处理器的初始化

ngx_http_init_phases初始化以下阶段的handlers

  • NGX_HTTP_POST_READ_PHASE

  • NGX_HTTP_SERVER_REWRITE_PHASE

  • NGX_HTTP_REWRITE_PHASE

  • NGX_HTTP_PREACCESS_PHASE

  • NGX_HTTP_ACCESS_PHASE

  • NGX_HTTP_PRECONTENT_PHASE

  • NGX_HTTP_CONTENT_PHASE

  • NGX_HTTP_LOG_PHASE

static ngx_int_t
ngx_http_init_phases(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf)
{if (ngx_array_init(&cmcf->phases[NGX_HTTP_POST_READ_PHASE].handlers,cf->pool, 1, sizeof(ngx_http_handler_pt))!= NGX_OK){return NGX_ERROR;}if (ngx_array_init(&cmcf->phases[NGX_HTTP_SERVER_REWRITE_PHASE].handlers,cf->pool, 1, sizeof(ngx_http_handler_pt))!= NGX_OK){return NGX_ERROR;}if (ngx_array_init(&cmcf->phases[NGX_HTTP_REWRITE_PHASE].handlers,cf->pool, 1, sizeof(ngx_http_handler_pt))!= NGX_OK){return NGX_ERROR;}if (ngx_array_init(&cmcf->phases[NGX_HTTP_PREACCESS_PHASE].handlers,cf->pool, 1, sizeof(ngx_http_handler_pt))!= NGX_OK){return NGX_ERROR;}if (ngx_array_init(&cmcf->phases[NGX_HTTP_ACCESS_PHASE].handlers,cf->pool, 2, sizeof(ngx_http_handler_pt))!= NGX_OK){return NGX_ERROR;}if (ngx_array_init(&cmcf->phases[NGX_HTTP_PRECONTENT_PHASE].handlers,cf->pool, 2, sizeof(ngx_http_handler_pt))!= NGX_OK){return NGX_ERROR;}if (ngx_array_init(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers,cf->pool, 4, sizeof(ngx_http_handler_pt))!= NGX_OK){return NGX_ERROR;}if (ngx_array_init(&cmcf->phases[NGX_HTTP_LOG_PHASE].handlers,cf->pool, 1, sizeof(ngx_http_handler_pt))!= NGX_OK){return NGX_ERROR;}return NGX_OK;
}

2.10 配置后置处理(postconfiguration)

遍历调用http模块的postconfiguration,用来注册阶段的handler

for (m = 0; cf->cycle->modules[m]; m++) {if (cf->cycle->modules[m]->type != NGX_HTTP_MODULE) {continue;}module = cf->cycle->modules[m]->ctx;if (module->postconfiguration) {if (module->postconfiguration(cf) != NGX_OK) {return NGX_CONF_ERROR;}}}

2.11 阶段引擎handler的初始化

将各个不同阶段的handler汇聚成一个处理链表

static ngx_int_t
ngx_http_init_phase_handlers(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf)
{ngx_int_t                   j;ngx_uint_t                  i, n;ngx_uint_t                  find_config_index, use_rewrite, use_access;ngx_http_handler_pt        *h;ngx_http_phase_handler_t   *ph;ngx_http_phase_handler_pt   checker;cmcf->phase_engine.server_rewrite_index = (ngx_uint_t) -1;cmcf->phase_engine.location_rewrite_index = (ngx_uint_t) -1;find_config_index = 0;use_rewrite = cmcf->phases[NGX_HTTP_REWRITE_PHASE].handlers.nelts ? 1 : 0;use_access = cmcf->phases[NGX_HTTP_ACCESS_PHASE].handlers.nelts ? 1 : 0;n = 1                  /* find config phase */+ use_rewrite      /* post rewrite phase */+ use_access;      /* post access phase */for (i = 0; i < NGX_HTTP_LOG_PHASE; i++) {n += cmcf->phases[i].handlers.nelts;}ph = ngx_pcalloc(cf->pool,n * sizeof(ngx_http_phase_handler_t) + sizeof(void *));if (ph == NULL) {return NGX_ERROR;}cmcf->phase_engine.handlers = ph;n = 0;for (i = 0; i < NGX_HTTP_LOG_PHASE; i++) {h = cmcf->phases[i].handlers.elts;switch (i) {case NGX_HTTP_SERVER_REWRITE_PHASE:if (cmcf->phase_engine.server_rewrite_index == (ngx_uint_t) -1) {cmcf->phase_engine.server_rewrite_index = n;}checker = ngx_http_core_rewrite_phase;break;case NGX_HTTP_FIND_CONFIG_PHASE:find_config_index = n;ph->checker = ngx_http_core_find_config_phase;n++;ph++;continue;case NGX_HTTP_REWRITE_PHASE:if (cmcf->phase_engine.location_rewrite_index == (ngx_uint_t) -1) {cmcf->phase_engine.location_rewrite_index = n;}checker = ngx_http_core_rewrite_phase;break;case NGX_HTTP_POST_REWRITE_PHASE:if (use_rewrite) {ph->checker = ngx_http_core_post_rewrite_phase;ph->next = find_config_index;n++;ph++;}continue;case NGX_HTTP_ACCESS_PHASE:checker = ngx_http_core_access_phase;n++;break;case NGX_HTTP_POST_ACCESS_PHASE:if (use_access) {ph->checker = ngx_http_core_post_access_phase;ph->next = n;ph++;}continue;case NGX_HTTP_CONTENT_PHASE:checker = ngx_http_core_content_phase;break;default:checker = ngx_http_core_generic_phase;}n += cmcf->phases[i].handlers.nelts;for (j = cmcf->phases[i].handlers.nelts - 1; j >= 0; j--) {ph->checker = checker;ph->handler = h[j];ph->next = n;ph++;}}return NGX_OK;
}

2.12 初始监听端口, 服务以及监听回调

ngx_http_optimize_servers中的ngx_http_add_listening会设置端口的回调

ls->handler = ngx_http_init_connection;

3. 运行时的处理

3.1 accept事件处理

在处理accept连接事件时,会调用ngx_listening_t的回调handler函数ngx_http_init_connection

对于新分配的连接,如果读事件的ready为1,即iocp或者延时的accept事件,在有使用accept锁情况 下,将事件放入posted_events队列中,否则直接调用事件的回调handler

if (rev->ready) {/* the deferred accept(), iocp */if (ngx_use_accept_mutex) {ngx_post_event(rev, &ngx_posted_events);return;}rev->handler(rev);return;
}

如果读事件的ready不为1,则将事件加入定时器的红黑树中。定时器超时后,就会调用它的 handler ngx_http_wait_request_handler 函数。

ngx_add_timer(rev, cscf->client_header_timeout);

将连接设置为可重用,因为该连接上还没有请求到来,所以当连接池中的连接不够用时,就可以重用这个连接。将当前connection添加可重用的连接队列中,同时可重用连接数加1

ngx_reusable_connection(c, 1);void
ngx_reusable_connection(ngx_connection_t *c, ngx_uint_t reusable)
{ngx_log_debug1(NGX_LOG_DEBUG_CORE, c->log, 0,"reusable connection: %ui", reusable);if (c->reusable) {ngx_queue_remove(&c->queue);ngx_cycle->reusable_connections_n--;#if (NGX_STAT_STUB)(void) ngx_atomic_fetch_add(ngx_stat_waiting, -1);
#endif}c->reusable = reusable;if (reusable) {/* need cast as ngx_cycle is volatile */ngx_queue_insert_head((ngx_queue_t *) &ngx_cycle->reusable_connections_queue, &c->queue);ngx_cycle->reusable_connections_n++;#if (NGX_STAT_STUB)(void) ngx_atomic_fetch_add(ngx_stat_waiting, 1);
#endif}
}

ngx_handle_read_event将分配连接的事件添加到事件驱动模块中

ngx_int_t
ngx_handle_read_event(ngx_event_t *rev, ngx_uint_t flags)
{if (ngx_event_flags & NGX_USE_CLEAR_EVENT) {/* kqueue, epoll */if (!rev->active && !rev->ready) {if (ngx_add_event(rev, NGX_READ_EVENT, NGX_CLEAR_EVENT)== NGX_ERROR){return NGX_ERROR;}}return NGX_OK;} else if (ngx_event_flags & NGX_USE_LEVEL_EVENT) {/* select, poll, /dev/poll */if (!rev->active && !rev->ready) {if (ngx_add_event(rev, NGX_READ_EVENT, NGX_LEVEL_EVENT)== NGX_ERROR){return NGX_ERROR;}return NGX_OK;}if (rev->active && (rev->ready || (flags & NGX_CLOSE_EVENT))) {if (ngx_del_event(rev, NGX_READ_EVENT, NGX_LEVEL_EVENT | flags)== NGX_ERROR){return NGX_ERROR;}return NGX_OK;}} else if (ngx_event_flags & NGX_USE_EVENTPORT_EVENT) {/* event ports */if (!rev->active && !rev->ready) {if (ngx_add_event(rev, NGX_READ_EVENT, 0) == NGX_ERROR) {return NGX_ERROR;}return NGX_OK;}if (rev->oneshot && rev->ready) {if (ngx_del_event(rev, NGX_READ_EVENT, 0) == NGX_ERROR) {return NGX_ERROR;}return NGX_OK;}}/* iocp */return NGX_OK;
}

3.2 首次可读事件处理

是通过ngx_http_wait_request_handler来处理

首先从网络上读取数据到连接中的buffer

ngx_connection_t          *c;
ngx_buf_t                 *b;
c = rev->data;
b = c->buffer;
if (b == NULL) {b = ngx_create_temp_buf(c->pool, size);if (b == NULL) {ngx_http_close_connection(c);return;}c->buffer = b;} else if (b->start == NULL) {b->start = ngx_palloc(c->pool, size);if (b->start == NULL) {ngx_http_close_connection(c);return;}b->pos = b->start;b->last = b->start;b->end = b->last + size;
}
n = c->recv(c, b->last, size);
b->last += n;

在可重用连接中删除当前连接

ngx_reusable_connection(c, 0);

创建http_request,在创建请求中,会将上面读取的缓冲区放在ngx_http_request_t中的header_in用于处理请求头

c->data = ngx_http_create_request(c);

处理请求头,同时将当前连接读事件的回调函数设置为ngx_http_process_request_line,用于处理单次接收的数据不完整

rev->handler = ngx_http_process_request_line;
ngx_http_process_request_line(rev);

3.3 请求行的处理

是通过ngx_http_process_request_line来处理的

先解析请求行

rc = ngx_http_parse_request_line(r, r->header_in);
http://www.tj-hxxt.cn/news/24008.html

相关文章:

  • 网络营销的看法和理解湖南企业竞价优化
  • 做批发上哪个网站好网络服务费计入什么科目
  • 做网站哪里最便宜网络营销渠道可分为
  • 学习网站开发技术百度网页推广
  • 建网站的论坛app推广渠道在哪接的单子
  • 大连网站建设怎么做百度seo价格查询
  • 网站项目的流程房管局备案查询网站
  • 杭州网站建设网络东莞网站推广优化公司
  • 做打折的淘宝小卖家的网站建网站费用
  • 岳阳做网站云南新闻最新消息今天
  • 个人装修接活群搜索引擎优化的主要策略
  • 学做日本蛋糕网站软文范例大全800字
  • 乌鲁木齐哪里做网站30个免费货源网站
  • 网站建设jsp百度的合作网站有哪些
  • 禹城有做网站seo关键词优化的技巧
  • 无锡哪家网站做的比较好湖南seo优化报价
  • 济南网站建设方案报价seo首页排名优化
  • 网站中嵌入地图最常用的搜索引擎有哪些
  • linux网站建设模板seo查询爱站
  • 英语作文网站公司网站制作流程
  • 网站怎么做能提升ip流量谷歌seo搜索优化
  • 手机如何翻到国外网站淘宝推广怎么推
  • 青岛做网站建设的公司排名企业营销平台
  • 网站多少钱一年建设网站需要多少钱
  • 免费公众号开发平台seo优化行业
  • 网站做跳转怎么做提高工作效率8个方法
  • 最好设计网站建设淘宝运营一般要学多久
  • app开发公司 弙东深圳龙岗区优化防控措施
  • 赤峰做网站的云盘搜
  • 网站开发 验收模板网络营销运营推广