Home > Backend Development > PHP Tutorial > 'In-depth Understanding of Nginx' Notes: 11 Processing Stages of HTTP Requests

'In-depth Understanding of Nginx' Notes: 11 Processing Stages of HTTP Requests

WBOY
Release: 2016-08-08 09:19:05
Original
1263 people have browsed it

11 processing stages of HTTP request

<code>typedef <span>enum</span> {
    <span>// 接收到完整的HTTP头部后处理阶段</span>
    NGX_HTTP_POST_READ_PHASE = <span>0</span>,

    <span>// 将请求URI与location表达式匹配前,修改URI,即重定向阶段</span>
    NGX_HTTP_SERVER_REWRITE_PHASE,

    <span>// 只能由ngx_http_core_module模块实现,用于根据请求URI寻找location表达式</span>
    NGX_HTTP_FIND_CONFIG_PHASE,

    <span>// 上一过程结束后修改URI</span>
    NGX_HTTP_REWRITE_PHASE,

    <span>// 为了防止rewrite造成死循环(一个请求执行10次会被Nginx认定为死循环)</span>
    NGX_HTTP_POST_REWRITE_PHASE,

    <span>// 在“决定请求访问权限”阶段前</span>
    NGX_HTTP_PREACCESS_PHASE,

    <span>// 决定访问权阶段,判断该请求是否可以访问Nginx服务器</span>
    NGX_HTTP_ACCESS_PHASE,

    <span>// 当然请求不被允许访问Nginx服务器时,该阶段负责向用户返回错误响应</span>
    NGX_HTTP_POST_ACCESS_PHASE,

    <span>// 用try_files配置项。顺序访问多个静态文件资源阶段</span>
    NGX_HTTP_TRY_FILES_PHASE,

    <span>// 处理HTTP请求内容阶段,这是大部分HTTP模块介入的阶段</span>
    NGX_HTTP_CONTENT_PHASE,

    <span>// 记录日志阶段</span>
    NGX_HTTP_LOG_PHASE
} ngx_http_phases;</code>
Copy after login

Data structure of processing stage

<code><span>typedef</span><span>struct</span> ngx_http_phase_handler_s  ngx_http_phase_handler_t;

<span>// 一个HTTP阶段的checker检查方法,仅有HTTP框架实现</span><span>typedef</span> ngx_int_t (*ngx_http_phase_handler_pt)(ngx_http_request_t *r,
    ngx_http_phase_handler_t *ph);

<span>// 由HTTP模块实现的handler处理方法</span><span>typedef</span> ngx_int_t (*ngx_http_handler_pt)(ngx_http_request_t *r);

<span>struct</span> ngx_http_phase_handler_s {
     <span>// 在处理一个HTTP阶段时,最先调用这个checker检查方法,在checker方法中调用模块实现的handler方法</span>
    ngx_http_phase_handler_pt  checker;

    <span>// 用于定义HTTP模块处理方法</span>
    ngx_http_handler_pt        handler;

    <span>// 将要执行的下一个HTTP处理阶段序号,可以不按顺序执行(这些处理阶段结构体都被存放在一个数组中,所以可以有下标序号</span>
    ngx_uint_t                 next;
};</code>
Copy after login

After an http{} block is parsed, an array consisting of ngx_http_phase_handler_t (the above structure) will be generated according to the configuration items in nginx.conf. When processing HTTP requests, execute them in the order of next. This composed array is used as another structure:

ngx_http_phase_engine_t

<code><span>typedef</span><span>struct</span> {
    <span>// 由ngx_http_phase_handler_t构成的数组首地址,表示一个请求可能经历的所有ngx_http_handler_pt处理方法</span>
    ngx_http_phase_handler_t  *handlers;

    <span>// 表示NGX_HTTP_SERVER_REWRITE_PHASE阶段第一个ngx_http_phase_handler_t处理方法在handlers数组中的序号</span>
    ngx_uint_t                 server_rewrite_index;

    <span>// 表示NGX_HTTP_REWRITE_PHASE阶段第一个ngx_http_phase_handler_t处理方法在handlers数组中的序号</span>
    ngx_uint_t                 location_rewrite_index;
} ngx_http_phase_engine_t;</code>
Copy after login

This array is saved in the ngx_http_core_main_conf_t structure:

<code>typedef struct {
    ngx_http_phase_engine_t         phase_engine;
    <span>...</span>
    // 在HTTP框架初始化时帮助各个HTTP模块在任意阶段中添加HTTP处理方法,它是一个有<span>11</span>个ngx_http_phase_t成员的数组,在初始化完毕后是没用的
    ngx_http_phase_t                    phases[NGX_HTTP_LOG_PHASE + <span>1</span>];
    <span>...</span>
} ngx_http_core_main_conf_t;</code>
Copy after login

ngx_http_phase_t

<code><span>typedef</span><span>struct</span> {
    <span>// handlers动态数组保存着每一个HTTP模块初始化时添加到当前阶段处理方法</span>
    ngx_array_t                     handlers;
}   ngx_http_phase_t;</code>
Copy after login

Copyright statement: Pain is just in your mind.

The above introduces the 11 processing stages of HTTP requests in the notes of "In-depth Understanding of Nginx", including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template