Nginx学习之三-ngx_http_request_t结构体

ngx_http_request_s是nginx中非常重要的一个结构体,贯穿于htpp请求处理的整个过程中。

下面解释了ngx_http_request_s结构体中与HTTP框架相关的重要的成员变量。

struct ngx_http_request_s {
    uint32_t                          signature;         /* "HTTP" */

    //请求对应的客户端连接
    ngx_connection_t                 *connection;

    //指向存放所有HTTP模块的上下文结构体的指针数组
    void                            **ctx;
    //指向请求对应的存放main级别配置结构体的指针数组
    void                            **main_conf;
    //指向请求对应的存放srv级别配置结构体的指针数组
    void                            **srv_conf;
    //指向请求对应的存放loc级别配置结构体的指针数组
    void                            **loc_conf;

    /*
     * 在接收完http头部,第一次在业务上处理http请求时,http框架提供的处理方法是ngx_http_process_request。
	 但如果该方法无法一次处理完该请求的全部业务,在归还控制权到epoll时间模块后,该请求再次被回调时,
	 将通过Ngx_http_request_handler方法来处理,而这个方法中对于可读事件的处理就是调用read_event_handler处理请求。
	 也就是说,http模块希望在底层处理请求的读事件时,重新实现read_event_handler方法
    */
    ngx_http_event_handler_pt         read_event_handler;
    //与上面的方法类似
    ngx_http_event_handler_pt         write_event_handler;

#if (NGX_HTTP_CACHE)
    ngx_http_cache_t                 *cache;
#endif

    //upstream机制用到的结构体
    ngx_http_upstream_t              *upstream;
    ngx_array_t                      *upstream_states;
                                         /* of ngx_http_upstream_state_t */

    //这个请求的内存池
    ngx_pool_t                       *pool;
    //用于接收http请求内容的缓冲区,主要接收http头部
    ngx_buf_t                        *header_in;

    //ngx_http_process_request_headers在接收、解析完http请求的头部后,会把解析完的每一个http头部加入到headers_in的headers链表中,同时会构造headers_in中的其他成员
    ngx_http_headers_in_t             headers_in;
    //http模块会把想要发送的http相应信息放到headers_out中,期望http框架将headers_out中的成员序列化为http响应包发送给用户
    ngx_http_headers_out_t            headers_out;

    //接收请求中包体的数据结构
    ngx_http_request_body_t          *request_body;

    //延迟关闭连接的时间
    time_t                            lingering_time;
    //当前请求初始化时的时间
    time_t                            start_sec;
    ngx_msec_t                        start_msec;

    //下面的9个成员是函数ngx_http_process_request_line方法在接收、解析http请求行时解析出的信息
    ngx_uint_t                        method;//方法名
    ngx_uint_t                        http_version;//协议版本

    ngx_str_t                         request_line;
    ngx_str_t                         uri;//用户请求中的uri
    ngx_str_t                         args;//用户请求中的url参数
    ngx_str_t                         exten;//用户请求的文件扩展名
    ngx_str_t                         unparsed_uri;//没有进行URL解码的原始请求

    ngx_str_t                         method_name;//用户请求中的方法名字符串
    ngx_str_t                         http_protocol;//其data成员指向请求中http起始地址

    /*表示需要发送给客户端的http响应。out中保存着由headers_out中序列化后的表示http头部的TCP流。
     * 在调用ngx_http_output_filter方法后,out中还会保存着待发送的http包体,它是实现异步发送http响应的关键。*/
    ngx_chain_t                      *out;
    /*当前请求既有可能是用户发来的请求,也可能是派生出的子请求。
     * 而main标识一系列相关的派生子请求的原始请求。
     * 一般可通过main和当前请求的地址是否相等来判断当前请求是否为用户发来的原始请求。*/
    ngx_http_request_t               *main;
    //当前请求的父请求(不一定是原始请求)
    ngx_http_request_t               *parent;
    //与subrequest子请求相关的功能
    ngx_http_postponed_request_t     *postponed;
    ngx_http_post_subrequest_t       *post_subrequest;
    //所有的子请求都是通过这个单链表链接起来的
    ngx_http_posted_request_t        *posted_requests;

    /*全局的ngx_http_phase_engine_t结构体中定义了一个ngx_http_phase_handler_t回答方法组成的数组。
     * 而phase_handler成员则与该数组配合使用。表示请求下次应当执行phase_handler作为序列号指定的数组中的回调方法*/
    ngx_int_t                         phase_handler;
    //表示NGX_HTTP_CONTENT_PHASE阶段提供给http模块处理请求的一种方式,它指向http模块实现的请求处理方法
    ngx_http_handler_pt               content_handler;
    //在NGX_HTTP_ACCESS_PHASE节点需要判断请求是否具有访问权限时,通过access_code来传递http模块的handler回调方法的返回值,如果为0表示具备权限。否则不具备。
    ngx_uint_t                        access_code;

    ngx_http_variable_value_t        *variables;

#if (NGX_PCRE)
    ngx_uint_t                        ncaptures;
    int                              *captures;
    u_char                           *captures_data;
#endif

    size_t                            limit_rate;

    /* used to learn the Apache compatible response length without a header */
    size_t                            header_size;

    //http请求的全部长度,包括http包体
    off_t                             request_length;

    ngx_uint_t                        err_status;

    ngx_http_connection_t            *http_connection;
#if (NGX_HTTP_SPDY)
    ngx_http_spdy_stream_t           *spdy_stream;
#endif

    ngx_http_log_handler_pt           log_handler;

    //在这个请求中如果打开了某些资源,并需要在请求结束时释放,那么需要把定义的释放资源的方法添加到这个成员
    ngx_http_cleanup_t               *cleanup;

    unsigned                          subrequests:8;
	//引用计数一般都作用于这个请求的原始请求上
    //引用计数,每当派生出子请求时,原始请求的count成员都会加一
    unsigned                          count:8;
    //阻塞标志位,目前仅由aio使用
    unsigned                          blocked:8;

    //标志位:为1表示蛋清请求正在使用异步IO
    unsigned                          aio:1;

    unsigned                          http_state:4;

    /* URI with "/." and on Win32 with "//" */
    unsigned                          complex_uri:1;

    /* URI with "%" */
    unsigned                          quoted_uri:1;

    /* URI with "+" */
    unsigned                          plus_in_uri:1;

    /* URI with " " */
    unsigned                          space_in_uri:1;

    unsigned                          invalid_header:1;

    unsigned                          add_uri_to_alias:1;
    unsigned                          valid_location:1;
    unsigned                          valid_unparsed_uri:1;
    //标志位:为1时表示URL发生过rewrite重写
    unsigned                          uri_changed:1;
    //表示使用rewrite重写URL的次数
    unsigned                          uri_changes:4;

    unsigned                          request_body_in_single_buf:1;
    unsigned                          request_body_in_file_only:1;
    unsigned                          request_body_in_persistent_file:1;
    unsigned                          request_body_in_clean_file:1;
    unsigned                          request_body_file_group_access:1;
    unsigned                          request_body_file_log_level:3;

    unsigned                          subrequest_in_memory:1;
    unsigned                          waited:1;

#if (NGX_HTTP_CACHE)
    unsigned                          cached:1;
#endif

#if (NGX_HTTP_GZIP)
    unsigned                          gzip_tested:1;
    unsigned                          gzip_ok:1;
    unsigned                          gzip_vary:1;
#endif

    unsigned                          proxy:1;
    unsigned                          bypass_cache:1;
    unsigned                          no_cache:1;

    /*
     * instead of using the request context data in
     * ngx_http_limit_conn_module and ngx_http_limit_req_module
     * we use the single bits in the request structure
     */
    unsigned                          limit_conn_set:1;
    unsigned                          limit_req_set:1;

#if 0
    unsigned                          cacheable:1;
#endif

    unsigned                          pipeline:1;
    unsigned                          chunked:1;
    unsigned                          header_only:1;
    //标志位,为1表示当前请求时keepalive请求
    unsigned                          keepalive:1;
    //延迟关闭标志位
    unsigned                          lingering_close:1;
    //标志位:为1表示正在丢弃http请求中的包体
    unsigned                          discard_body:1;
    //标志位:为1表示请求的当前状态是在做内部跳转
    unsigned                          internal:1;
    unsigned                          error_page:1;
    unsigned                          ignore_content_encoding:1;
    unsigned                          filter_finalize:1;
    unsigned                          post_action:1;
    unsigned                          request_complete:1;
    unsigned                          request_output:1;
    //标志位:为1表示发生给客户端的http响应头已经发送
    unsigned                          header_sent:1;
    unsigned                          expect_tested:1;
    unsigned                          root_tested:1;
    unsigned                          done:1;
    unsigned                          logged:1;

    //标志位,表示缓冲中是否有待发送内容
    unsigned                          buffered:4;

    unsigned                          main_filter_need_in_memory:1;
    unsigned                          filter_need_in_memory:1;
    unsigned                          filter_need_temporary:1;
    unsigned                          allow_ranges:1;

#if (NGX_STAT_STUB)
    unsigned                          stat_reading:1;
    unsigned                          stat_writing:1;
#endif

    /* used to parse HTTP headers */

    //状态机解析http时使用state来表示当前的解析状态,需要检查是否构成完成的http请求行
    ngx_uint_t                        state;

    ngx_uint_t                        header_hash;
    ngx_uint_t                        lowcase_index;
    u_char                            lowcase_header[NGX_HTTP_LC_HEADER_LEN];

    u_char                           *header_name_start;
    u_char                           *header_name_end;
    u_char                           *header_start;
    u_char                           *header_end;

    /*
     * a memory that can be reused after parsing a request line
     * via ngx_http_ephemeral_t
     */

    u_char                           *uri_start;
    u_char                           *uri_end;
    u_char                           *uri_ext;
    u_char                           *args_start;
    u_char                           *request_start;
    u_char                           *request_end;
    u_char                           *method_end;
    u_char                           *schema_start;
    u_char                           *schema_end;
    u_char                           *host_start;
    u_char                           *host_end;
    u_char                           *port_start;
    u_char                           *port_end;

    unsigned                          http_minor:16;
    unsigned                          http_major:16;
};



  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,请允许我一一解释这些编译参数的含义。 1. --prefix=${PATH_INSTALL}/nginx 这个参数指定了 nginx 安装的路径。在这里,${PATH_INSTALL} 是一个变量,代表 nginx 安装的根目录。例如,如果 ${PATH_INSTALL} 被设置为 /usr/local,则 nginx 将会被安装到 /usr/local/nginx 目录下。 2. --user=nginx 这个参数指定了 nginx 进程运行的用户。在这里,nginx 进程将会以 nginx 用户的身份运行。 3. --group=nginx 这个参数指定了 nginx 进程运行的用户组。在这里,nginx 进程将会以 nginx 用户组的身份运行。 4. --with-http_ssl_module 这个参数启用了 nginx 的 SSL 功能,使得 nginx 能够处理 HTTPS 请求。 5. --with-http_realip_module 这个参数启用了 nginx 的真实 IP 功能,使得 nginx 能够获取客户端的真实 IP 地址。 6. --with-http_addition_module 这个参数启用了 nginx 的响应内容添加功能,使得 nginx 能够在 HTTP 响应中添加额外的内容。 7. --with-http_sub_module 这个参数启用了 nginx 的响应内容替换功能,使得 nginx 能够在 HTTP 响应中替换指定的内容。 8. --with-http_dav_module 这个参数启用了 nginx 的 WebDAV 功能,使得 nginx 能够处理 WebDAV 请求。 9. --with-http_flv_module 这个参数启用了 nginx 的 FLV 视频流功能,使得 nginx 能够处理 FLV 视频流请求。 10. --with-http_mp4_module 这个参数启用了 nginx 的 MP4 视频流功能,使得 nginx 能够处理 MP4 视频流请求。 11. --with-http_gunzip_module 这个参数启用了 nginx 的 Gzip 解压缩功能,使得 nginx 能够解压缩 Gzip 压缩的响应内容。 12. --with-http_gzip_static_module 这个参数启用了 nginx 的 Gzip 静态文件压缩功能,使得 nginx 能够对静态文件进行 Gzip 压缩。 13. --with-http_random_index_module 这个参数启用了 nginx 的随机索引功能,使得 nginx 能够在目录索引中随机显示文件。 14. --with-http_secure_link_module 这个参数启用了 nginx 的安全链接功能,使得 nginx 能够生成基于时间戳的安全链接。 15. --with-http_stub_status_module 这个参数启用了 nginx 的状态页面功能,使得 nginx 能够在浏览器中显示当前服务器的状态信息。 16. --with-http_auth_request_module 这个参数启用了 nginx 的认证请求功能,使得 nginx 能够向另一个服务器发送认证请求。 17. --with-threads 这个参数启用了 nginx 的线程池功能,使得 nginx 能够处理并发请求。 18. --with-stream 这个参数启用了 nginx 的流模块功能,使得 nginx 能够处理 TCP 和 UDP 流量。 19. --with-stream_ssl_module 这个参数启用了 nginx 的流 SSL 功能,使得 nginx 能够处理加密的 TCP 流量。 20. --with-http_slice_module 这个参数启用了 nginx 的分片功能,使得 nginx 能够将大文件分成多个小块进行传输。 21. --with-file-aio 这个参数启用了 nginx 的异步文件 I/O 功能,使得 nginx 能够异步读取文件。 22. --with-http_v2_module 这个参数启用了 nginxHTTP/2 功能,使得 nginx 能够处理 HTTP/2 请求。 23. --with-pcre 这个参数启用了 PCRE 库,使得 nginx 能够使用正则表达式进行匹配操作。 24. --with-openssl=/www/server/nginx/src/openssl 这个参数指定了 OpenSSL 库的路径,使得 nginx 能够使用 OpenSSL 库进行加密操作。 25. --with-stream_ssl_preread_module 这个参数启用了 nginx 的流 SSL 预读功能,使得 nginx 能够在客户端发送 SSL 握手之前识别出 SSL 流量。 26. --with-http_image_filter_module 这个参数启用了 nginx 的图像处理功能,使得 nginx 能够处理图像文件。 27. --with-ipv6 这个参数启用了 IPv6 功能,使得 nginx 能够处理 IPv6 地址。 28. --with-ld-opt=-Wl,-E 这个参数指定了链接器的选项,使得 nginx 能够在运行时动态链接库。 29. --with-cc-opt=-Wno-error 这个参数指定了编译器的选项,禁止编译器将警告视为错误。 30. --with-ld-opt=-ljemalloc 这个参数指定了链接器的选项,使得 nginx 能够使用 jemalloc 内存分配器。 31. --add-module=/www/server/nginx/src/ngx_devel_kit 这个参数添加了一个模块,这个模块是 ngx_devel_kit,它提供了一些开发工具和库,使得开发 nginx 模块更加容易。 32. --add-module=/www/server/nginx/src/lua_nginx_module 这个参数添加了一个模块,这个模块是 lua_nginx_module,它使得 nginx 能够使用 Lua 脚本进行定制化操作。 33. --add-module=/www/server/nginx/src/ngx_cache_purge 这个参数添加了一个模块,这个模块是 ngx_cache_purge,它使得 nginx 能够手动清除缓存。 34. --add-module=/www/server/nginx/src/ngx_http_substitutions_filter_module-master 这个参数添加了一个模块,这个模块是 ngx_http_substitutions_filter_module-master,它使得 nginx 能够在 HTTP 响应中替换指定的内容。 以上就是这些编译参数的详细说明。希望对您有所帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

没有昵称阿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值