Nginx學習之三-ngx_http_request_t結構體

來源:互聯網
上載者:User
摘自: http://blog.csdn.net/xiajun07061225/article/details/9189505 江雨煙雲的部落格

ngx_http_request_s是nginx中非常重要的一個結構體,貫穿於htpp請求處理的整個過程中。

下面解釋了ngx_http_request_s結構體中與HTTP架構相關的重要的成員變數。

[cpp] view plain copy print?

  1. struct ngx_http_request_s {
  2. uint32_t signature; /* "HTTP" */
  3. //請求對應的用戶端串連
  4. ngx_connection_t *connection;
  5. //指向存放所有HTTP模組的上下文結構體的指標數組
  6. void **ctx;
  7. //指向請求對應的存放main層級配置結構體的指標數組
  8. void **main_conf;
  9. //指向請求對應的存放srv層級配置結構體的指標數組
  10. void **srv_conf;
  11. //指向請求對應的存放loc層級配置結構體的指標數組
  12. void **loc_conf;
  13. /*
  14. * 在接收完http頭部,第一次在業務上處理http請求時,http架構提供的處理方法是ngx_http_process_request。
  15. 但如果該方法無法一次處理完該請求的全部業務,在歸還控制權到epoll時間模組後,該請求再次被回調時,
  16. 將通過Ngx_http_request_handler方法來處理,而這個方法中對於可讀事件的處理就是調用read_event_handler處理請求。
  17. 也就是說,http模組希望在底層處理請求的讀事件時,重新實現read_event_handler方法
  18. */
  19. ngx_http_event_handler_pt read_event_handler;
  20. //與上面的方法類似
  21. ngx_http_event_handler_pt write_event_handler;
  22. #if (NGX_HTTP_CACHE)
  23. ngx_http_cache_t *cache;
  24. #endif
  25. //upstream機制用到的結構體
  26. ngx_http_upstream_t *upstream;
  27. ngx_array_t *upstream_states;
  28. /* of ngx_http_upstream_state_t */
  29. //這個請求的記憶體池
  30. ngx_pool_t *pool;
  31. //用於接收http請求內容的緩衝區,主要接收http頭部
  32. ngx_buf_t *header_in;
  33. //ngx_http_process_request_headers在接收、解析完http請求的頭部後,會把解析完的每一個http頭部加入到headers_in的headers鏈表中,同時會構造headers_in中的其他成員
  34. ngx_http_headers_in_t headers_in;
  35. //http模組會把想要發送的http相應資訊放到headers_out中,期望http架構將headers_out中的成員序列化為http響應包發送給使用者
  36. ngx_http_headers_out_t headers_out;
  37. //接收請求中包體的資料結構
  38. ngx_http_request_body_t *request_body;
  39. //延遲關閉串連的時間
  40. time_t lingering_time;
  41. //當前請求初始化時的時間
  42. time_t start_sec;
  43. ngx_msec_t start_msec;
  44. //下面的9個成員是函數ngx_http_process_request_line方法在接收、解析http請求行時解析出的資訊
  45. ngx_uint_t method;//方法名
  46. ngx_uint_t http_version;//協議版本
  47. ngx_str_t request_line;
  48. ngx_str_t uri;//使用者請求中的uri
  49. ngx_str_t args;//使用者請求中的url參數
  50. ngx_str_t exten;//使用者請求的副檔名
  51. ngx_str_t unparsed_uri;//沒有進行URL解碼的原始請求
  52. ngx_str_t method_name;//使用者請求中的方法名字串
  53. ngx_str_t http_protocol;//其data成員指向請求中http起始地址
  54. /*表示需要發送給用戶端的http響應。out中儲存著由headers_out中序列化後的表示http頭部的TCP流。
  55. * 在調用ngx_http_output_filter方法後,out中還會儲存著待發送的http包體,它是實現非同步發送http響應的關鍵。*/
  56. ngx_chain_t *out;
  57. /*當前請求既有可能是使用者發來的請求,也可能是派生出的子請求。
  58. * 而main標識一系列相關的派生子請求的原始請求。
  59. * 一般可通過main和當前請求的地址是否相等來判斷當前請求是否為使用者發來的原始請求。*/
  60. ngx_http_request_t *main;
  61. //當前請求的父請求(不一定是原始請求)
  62. ngx_http_request_t *parent;
  63. //與subrequest子請求相關的功能
  64. ngx_http_postponed_request_t *postponed;
  65. ngx_http_post_subrequest_t *post_subrequest;
  66. //所有的子請求都是通過這個單鏈錶鏈接起來的
  67. ngx_http_posted_request_t *posted_requests;
  68. /*全域的ngx_http_phase_engine_t結構體中定義了一個ngx_http_phase_handler_t回答方法組成的數組。
  69. * 而phase_handler成員則與該數組配合使用。表示請求下次應當執行phase_handler作為序號指定的數組中的回調方法*/
  70. ngx_int_t phase_handler;
  71. //表示NGX_HTTP_CONTENT_PHASE階段提供給http模組處理請求的一種方式,它指向http模組實現的請求處理方法
  72. ngx_http_handler_pt content_handler;
  73. //在NGX_HTTP_ACCESS_PHASE節點需要判斷請求是否具有存取權限時,通過access_code來傳遞http模組的handler回調方法的返回值,如果為0表示具備許可權。否則不具備。
  74. ngx_uint_t access_code;
  75. ngx_http_variable_value_t *variables;
  76. #if (NGX_PCRE)
  77. ngx_uint_t ncaptures;
  78. int *captures;
  79. u_char *captures_data;
  80. #endif
  81. size_t limit_rate;
  82. /* used to learn the Apache compatible response length without a header */
  83. size_t header_size;
  84. //http請求的全部長度,包括http包體
  85. off_t request_length;
  86. ngx_uint_t err_status;
  87. ngx_http_connection_t *http_connection;
  88. #if (NGX_HTTP_SPDY)
  89. ngx_http_spdy_stream_t *spdy_stream;
  90. #endif
  91. ngx_http_log_handler_pt log_handler;
  92. //在這個請求中如果開啟了某些資源,並需要在請求結束時釋放,那麼需要把定義的釋放資源的方法添加到這個成員
  93. ngx_http_cleanup_t *cleanup;
  94. unsigned subrequests:8;
  95. //引用計數一般都作用於這個請求的原始請求上
  96. //引用計數,每當派生出子請求時,原始請求的count成員都會加一
  97. unsigned count:8;
  98. //阻塞標誌位,目前僅由aio使用
  99. unsigned blocked:8;
  100. //標誌位:為1表示蛋清請求正在使用非同步IO
  101. unsigned aio:1;
  102. unsigned http_state:4;
  103. /* URI with "/." and on Win32 with "//" */
  104. unsigned complex_uri:1;
  105. /* URI with "%" */
  106. unsigned quoted_uri:1;
  107. /* URI with "+" */
  108. unsigned plus_in_uri:1;
  109. /* URI with " " */
  110. unsigned space_in_uri:1;
  111. unsigned invalid_header:1;
  112. unsigned add_uri_to_alias:1;
  113. unsigned valid_location:1;
  114. unsigned valid_unparsed_uri:1;
  115. //標誌位:為1時表示URL發生過rewrite重寫
  116. unsigned uri_changed:1;
  117. //表示使用rewrite重寫URL的次數
  118. unsigned uri_changes:4;
  119. unsigned request_body_in_single_buf:1;
  120. unsigned request_body_in_file_only:1;
  121. unsigned request_body_in_persistent_file:1;
  122. unsigned request_body_in_clean_file:1;
  123. unsigned request_body_file_group_access:1;
  124. unsigned request_body_file_log_level:3;
  125. unsigned subrequest_in_memory:1;
  126. unsigned waited:1;
  127. #if (NGX_HTTP_CACHE)
  128. unsigned cached:1;
  129. #endif
  130. #if (NGX_HTTP_GZIP)
  131. unsigned gzip_tested:1;
  132. unsigned gzip_ok:1;
  133. unsigned gzip_vary:1;
  134. #endif
  135. unsigned proxy:1;
  136. unsigned bypass_cache:1;
  137. unsigned no_cache:1;
  138. /*
  139. * instead of using the request context data in
  140. * ngx_http_limit_conn_module and ngx_http_limit_req_module
  141. * we use the single bits in the request structure
  142. */
  143. unsigned limit_conn_set:1;
  144. unsigned limit_req_set:1;
  145. #if 0
  146. unsigned cacheable:1;
  147. #endif
  148. unsigned pipeline:1;
  149. unsigned chunked:1;
  150. unsigned header_only:1;
  151. //標誌位,為1表示當前請求時keepalive請求
  152. unsigned keepalive:1;
  153. //延遲關閉標誌位
  154. unsigned lingering_close:1;
  155. //標誌位:為1表示正在丟棄http請求中的包體
  156. unsigned discard_body:1;
  157. //標誌位:為1表示請求的目前狀態是在做內部跳轉
  158. unsigned internal:1;
  159. unsigned error_page:1;
  160. unsigned ignore_content_encoding:1;
  161. unsigned filter_finalize:1;
  162. unsigned post_action:1;
  163. unsigned request_complete:1;
  164. unsigned request_output:1;
  165. //標誌位:為1表示發生給用戶端的http回應標頭已經發送
  166. unsigned header_sent:1;
  167. unsigned expect_tested:1;
  168. unsigned root_tested:1;
  169. unsigned done:1;
  170. unsigned logged:1;
  171. //標誌位,表示緩衝中是否有待發送內容
  172. unsigned buffered:4;
  173. unsigned main_filter_need_in_memory:1;
  174. unsigned filter_need_in_memory:1;
  175. unsigned filter_need_temporary:1;
  176. unsigned allow_ranges:1;
  177. #if (NGX_STAT_STUB)
  178. unsigned stat_reading:1;
  179. unsigned stat_writing:1;
  180. #endif
  181. /* used to parse HTTP headers */
  182. //狀態機器解析http時使用state來表示當前的解析狀態,需要檢查是否構成完成的http請求行
  183. ngx_uint_t state;
  184. ngx_uint_t header_hash;
  185. ngx_uint_t lowcase_index;
  186. u_char lowcase_header[NGX_HTTP_LC_HEADER_LEN];
  187. u_char *header_name_start;
  188. u_char *header_name_end;
  189. u_char *header_start;
  190. u_char *header_end;
  191. /*
  192. * a memory that can be reused after parsing a request line
  193. * via ngx_http_ephemeral_t
  194. */
  195. u_char *uri_start;
  196. u_char *uri_end;
  197. u_char *uri_ext;
  198. u_char *args_start;
  199. u_char *request_start;
  200. u_char *request_end;
  201. u_char *method_end;
  202. u_char *schema_start;
  203. u_char *schema_end;
  204. u_char *host_start;
  205. u_char *host_end;
  206. u_char *port_start;
  207. u_char *port_end;
  208. unsigned http_minor:16;
  209. unsigned http_major:16;
  210. };

以上就介紹了 Nginx學習之三-ngx_http_request_t結構體,包括了方面的內容,希望對PHP教程有興趣的朋友有所協助。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.