找回密码
 立即注册
首页 业界区 业界 Nginx学习笔记(二) Nginx--connection&request ...

Nginx学习笔记(二) Nginx--connection&request

蝙俚 2025-5-29 16:34:57
Nginx--connection&request

  在Nginx中,主要包括了连接与处理两部分。
connection

  在src/core文件夹下包含有connection的源文件,Ngx_connection.h/Ngx_connection.c中可以找到SOCK_STREAM,也就是说Nginx是基于TCP连接的。
连接过程

  对于应用程序,首先第一步肯定是加载并解析配置文件,Nginx同样如此,这样可以获得需要监听的端口和IP地址。之后,Nginx就要创建master进程,并建立socket,这样就可以创建多个worker进程来,每个worker进程都可以accept连接请求。当通过三次握手成功建立一个连接后,nginx的某一个worker进程会accept成功,得到这个建立好的连接的socket,然后创建ngx_connection_t结构体,存储客户端相关内容。
  这样建立好连接后,服务器和客户端就可以正常进行读写事件了。连接完成后就可以释放掉ngx_connection_t结构体了。
  同样,Nginx也可以作为客户端,这样就需要先创建一个ngx_connection_t结构体,然后创建socket,并设置socket的属性( 比如非阻塞)。然后再通过添加读写事件,调用connect/read/write来调用连接,最后关掉连接,并释放ngx_connection_t。
1.gif
2.gif
  1. struct ngx_connection_s {
  2.     void               *data;
  3.     ngx_event_t        *read;
  4.     ngx_event_t        *write;
  5.     ngx_socket_t        fd;
  6.     ngx_recv_pt         recv;
  7.     ngx_send_pt         send;
  8.     ngx_recv_chain_pt   recv_chain;
  9.     ngx_send_chain_pt   send_chain;
  10.     ngx_listening_t    *listening;
  11.     off_t               sent;
  12.     ngx_log_t          *log;
  13.     ngx_pool_t         *pool;
  14.     struct sockaddr    *sockaddr;
  15.     socklen_t           socklen;
  16.     ngx_str_t           addr_text;
  17. #if (NGX_SSL)
  18.     ngx_ssl_connection_t  *ssl;
  19. #endif
  20.     struct sockaddr    *local_sockaddr;
  21.     ngx_buf_t          *buffer;
  22.     ngx_queue_t         queue;
  23.     ngx_atomic_uint_t   number;
  24.     ngx_uint_t          requests;
  25.     unsigned            buffered:8;
  26.     unsigned            log_error:3;     /* ngx_connection_log_error_e */
  27.     unsigned            unexpected_eof:1;
  28.     unsigned            timedout:1;
  29.     unsigned            error:1;
  30.     unsigned            destroyed:1;
  31.     unsigned            idle:1;
  32.     unsigned            reusable:1;
  33.     unsigned            close:1;
  34.     unsigned            sendfile:1;
  35.     unsigned            sndlowat:1;
  36.     unsigned            tcp_nodelay:2;   /* ngx_connection_tcp_nodelay_e */
  37.     unsigned            tcp_nopush:2;    /* ngx_connection_tcp_nopush_e */
  38. #if (NGX_HAVE_IOCP)
  39.     unsigned            accept_context_updated:1;
  40. #endif
  41. #if (NGX_HAVE_AIO_SENDFILE)
  42.     unsigned            aio_sendfile:1;
  43.     ngx_buf_t          *busy_sendfile;
  44. #endif
  45. #if (NGX_THREADS)
  46.     ngx_atomic_t        lock;
  47. #endif
  48. };
复制代码
View Code  
连接池

   在linux系统中,每一个进程能够打开的文件描述符fd是有限的,而每创建一个socket就会占用一个fd,这样创建的socket就会有限的。在Nginx中,采用连接池的方法,可以避免这个问题。
   Nginx在实现时,是通过一个连接池来管理的,每个worker进程都有一个独立的连接池,连接池的大小是worker_connections。这里的连接池里面保存的其实不是真实的连接,它只是一个worker_connections大小的一个ngx_connection_t结构的数组。并且,nginx会通过一个链表free_connections来保存所有的空闲ngx_connection_t,每次获取一个连接时,就从空闲连接链表中获取一个,用完后,再放回空闲连接链表里面(这样就节省了创建与销毁connection结构的开销)。
  所以对于一个Nginx服务器来说,它所能创建的连接数也就是socket连接数目可以达到worker_processes(worker数)*worker_connections。
竞争问题

  对于多个worker进程同时accpet时产生的竞争,有可能导致某一worker进程accept了大量的连接,而其他worker进程却没有几个连接,这样就导致了负载不均衡,对于负载重的worker进程中的连接响应时间必然会增大。很显然,这是不公平的,有的进程有空余连接,却没有处理机会,有的进程因为没有空余连接,却人为地丢弃连接。
  nginx中存在accept_mutex选项,只有获得了accept_mutex的进程才会去添加accept事件,也就是说,nginx会控制进程是否添加accept事件。nginx使用一个叫ngx_accept_disabled的变量来控制进程是否去竞争accept_mutex锁。
  1. ngx_accept_disabled = ngx_cycle->connection_n / 8 - ngx_cycle->free_connection_n;  //可以看出来随着空余连接的增加,disabled的值降低
复制代码
  1. if (ngx_use_accept_mutex) {
  2.         if (ngx_accept_disabled > 0) {           //当disabled的值大于0时,禁止竞争,但每次-1
  3.             ngx_accept_disabled--;
  4.         } else {
  5.             if (ngx_trylock_accept_mutex(cycle) == NGX_ERROR) {
  6.                 return;
  7.             }
  8.        if (ngx_accept_mutex_held) {
  9.                 flags |= NGX_POST_EVENTS;
  10.             } else {
  11.                 if (timer == NGX_TIMER_INFINITE
  12.                     || timer > ngx_accept_mutex_delay) {
  13.                     timer = ngx_accept_mutex_delay;
  14.                 }
  15.             }
  16.         }<br>}
复制代码
request

  在nginx中,request是http请求,具体到nginx中的数据结构是ngx_http_request_t。ngx_http_request_t是对一个http请求的封装。 
3.gif
4.gif
  1. struct ngx_http_request_s {
  2.     uint32_t                          signature;         /* "HTTP" */
  3.     ngx_connection_t                 *connection;
  4.     void                            **ctx;
  5.     void                            **main_conf;
  6.     void                            **srv_conf;
  7.     void                            **loc_conf;
  8.     ngx_http_event_handler_pt         read_event_handler;
  9.     ngx_http_event_handler_pt         write_event_handler;
  10. #if (NGX_HTTP_CACHE)
  11.     ngx_http_cache_t                 *cache;
  12. #endif
  13.     ngx_http_upstream_t              *upstream;
  14.     ngx_array_t                      *upstream_states;
  15.                                          /* of ngx_http_upstream_state_t */
  16.     ngx_pool_t                       *pool;
  17.     ngx_buf_t                        *header_in;
  18.     ngx_http_headers_in_t             headers_in;
  19.     ngx_http_headers_out_t            headers_out;
  20.     ngx_http_request_body_t          *request_body;
  21.     time_t                            lingering_time;
  22.     time_t                            start_sec;
  23.     ngx_msec_t                        start_msec;
  24.     ngx_uint_t                        method;
  25.     ngx_uint_t                        http_version;
  26.     ngx_str_t                         request_line;
  27.     ngx_str_t                         uri;
  28.     ngx_str_t                         args;
  29.     ngx_str_t                         exten;
  30.     ngx_str_t                         unparsed_uri;
  31.     ngx_str_t                         method_name;
  32.     ngx_str_t                         http_protocol;
  33.     ngx_chain_t                      *out;
  34.     ngx_http_request_t               *main;
  35.     ngx_http_request_t               *parent;
  36.     ngx_http_postponed_request_t     *postponed;
  37.     ngx_http_post_subrequest_t       *post_subrequest;
  38.     ngx_http_posted_request_t        *posted_requests;
  39.     ngx_int_t                         phase_handler;
  40.     ngx_http_handler_pt               content_handler;
  41.     ngx_uint_t                        access_code;
  42.     ngx_http_variable_value_t        *variables;
  43. #if (NGX_PCRE)
  44.     ngx_uint_t                        ncaptures;
  45.     int                              *captures;
  46.     u_char                           *captures_data;
  47. #endif
  48.     size_t                            limit_rate;
  49.     /* used to learn the Apache compatible response length without a header */
  50.     size_t                            header_size;
  51.     off_t                             request_length;
  52.     ngx_uint_t                        err_status;
  53.     ngx_http_connection_t            *http_connection;
  54. #if (NGX_HTTP_SPDY)
  55.     ngx_http_spdy_stream_t           *spdy_stream;
  56. #endif
  57.     ngx_http_log_handler_pt           log_handler;
  58.     ngx_http_cleanup_t               *cleanup;
  59.     unsigned                          subrequests:8;
  60.     unsigned                          count:8;
  61.     unsigned                          blocked:8;
  62.     unsigned                          aio:1;
  63.     unsigned                          http_state:4;
  64.     /* URI with "/." and on Win32 with "//" */
  65.     unsigned                          complex_uri:1;
  66.     /* URI with "%" */
  67.     unsigned                          quoted_uri:1;
  68.     /* URI with "+" */
  69.     unsigned                          plus_in_uri:1;
  70.     /* URI with " " */
  71.     unsigned                          space_in_uri:1;
  72.     unsigned                          invalid_header:1;
  73.     unsigned                          add_uri_to_alias:1;
  74.     unsigned                          valid_location:1;
  75.     unsigned                          valid_unparsed_uri:1;
  76.     unsigned                          uri_changed:1;
  77.     unsigned                          uri_changes:4;
  78.     unsigned                          request_body_in_single_buf:1;
  79.     unsigned                          request_body_in_file_only:1;
  80.     unsigned                          request_body_in_persistent_file:1;
  81.     unsigned                          request_body_in_clean_file:1;
  82.     unsigned                          request_body_file_group_access:1;
  83.     unsigned                          request_body_file_log_level:3;
  84.     unsigned                          subrequest_in_memory:1;
  85.     unsigned                          waited:1;
  86. #if (NGX_HTTP_CACHE)
  87.     unsigned                          cached:1;
  88. #endif
  89. #if (NGX_HTTP_GZIP)
  90.     unsigned                          gzip_tested:1;
  91.     unsigned                          gzip_ok:1;
  92.     unsigned                          gzip_vary:1;
  93. #endif
  94.     unsigned                          proxy:1;
  95.     unsigned                          bypass_cache:1;
  96.     unsigned                          no_cache:1;
  97.     /*
  98.      * instead of using the request context data in
  99.      * ngx_http_limit_conn_module and ngx_http_limit_req_module
  100.      * we use the single bits in the request structure
  101.      */
  102.     unsigned                          limit_conn_set:1;
  103.     unsigned                          limit_req_set:1;
  104. #if 0
  105.     unsigned                          cacheable:1;
  106. #endif
  107.     unsigned                          pipeline:1;
  108.     unsigned                          chunked:1;
  109.     unsigned                          header_only:1;
  110.     unsigned                          keepalive:1;
  111.     unsigned                          lingering_close:1;
  112.     unsigned                          discard_body:1;
  113.     unsigned                          internal:1;
  114.     unsigned                          error_page:1;
  115.     unsigned                          ignore_content_encoding:1;
  116.     unsigned                          filter_finalize:1;
  117.     unsigned                          post_action:1;
  118.     unsigned                          request_complete:1;
  119.     unsigned                          request_output:1;
  120.     unsigned                          header_sent:1;
  121.     unsigned                          expect_tested:1;
  122.     unsigned                          root_tested:1;
  123.     unsigned                          done:1;
  124.     unsigned                          logged:1;
  125.     unsigned                          buffered:4;
  126.     unsigned                          main_filter_need_in_memory:1;
  127.     unsigned                          filter_need_in_memory:1;
  128.     unsigned                          filter_need_temporary:1;
  129.     unsigned                          allow_ranges:1;
  130. #if (NGX_STAT_STUB)
  131.     unsigned                          stat_reading:1;
  132.     unsigned                          stat_writing:1;
  133. #endif
  134.     /* used to parse HTTP headers */
  135.     ngx_uint_t                        state;
  136.     ngx_uint_t                        header_hash;
  137.     ngx_uint_t                        lowcase_index;
  138.     u_char                            lowcase_header[NGX_HTTP_LC_HEADER_LEN];
  139.     u_char                           *header_name_start;
  140.     u_char                           *header_name_end;
  141.     u_char                           *header_start;
  142.     u_char                           *header_end;
  143.     /*
  144.      * a memory that can be reused after parsing a request line
  145.      * via ngx_http_ephemeral_t
  146.      */
  147.     u_char                           *uri_start;
  148.     u_char                           *uri_end;
  149.     u_char                           *uri_ext;
  150.     u_char                           *args_start;
  151.     u_char                           *request_start;
  152.     u_char                           *request_end;
  153.     u_char                           *method_end;
  154.     u_char                           *schema_start;
  155.     u_char                           *schema_end;
  156.     u_char                           *host_start;
  157.     u_char                           *host_end;
  158.     u_char                           *port_start;
  159.     u_char                           *port_end;
  160.     unsigned                          http_minor:16;
  161.     unsigned                          http_major:16;
  162. };
复制代码
View Code  
HTTP

   这里需要复习下Http协议了。
  http请求是典型的请求-响应类型的的网络协议,需要一行一行的分析请求行与请求头,以及输出响应行与响应头。
  Request 消息分为3部分,第一部分叫请求行requset line, 第二部分叫http header, 第三部分是body. header和body之间有个空行。
  Response消息的结构, 和Request消息的结构基本一样。 同样也分为三部分,第一部分叫response line, 第二部分叫response header,第三部分是body. header和body之间也有个空行。
  分别为Request和Response消息结构图:
5.png

6.png

处理流程

  worker进程负责业务处理。在worker进程中有一个函数ngx_worker_process_cycle(),执行无限循环,不断处理收到的来自客户端的请求,并进行处理,直到整个nginx服务被停止。
  一个HTTP Request的处理过程: 

  • 初始化HTTP Request(读取来自客户端的数据,生成HTTP Requst对象,该对象含有该请求所有的信息)。
  • 处理请求头。
  • 处理请求体。
  • 如果有的话,调用与此请求(URL或者Location)关联的handler
  • 依次调用各phase handler进行处理。
  一个phase handler的执行过程:

  • 获取location配置。
  • 产生适当的响应。
  • 发送response header.
  • 发送response body.
  这里直接上taobao团队的给出的Nginx流程图了。
7.png

  从这个图中可以清晰的看到解析http消息每个部分的不同模块。
keepalive长连接

  长连接的定义:所谓长连接,指在一个连接上可以连续发送多个数据包,在连接保持期间,如果没有数据包发送,需要双方发链路检测包。
  在这里,http请求是基于TCP协议之上的,所以建立需要三次握手,关闭需要四次握手。而http请求是请求应答式的,如果我们能知道每个请求头与响应体的长度,那么我们是可以在一个连接上面执行多个请求的,这就需要在请求头中指定content-length来表明body的大小。在http1.0与http1.1中稍有不同,具体情况如下:
8.gif
9.gif
  1.     对于http1.0协议来说,如果响应头中有content-length头,则以content-length的长度就可以知道body的长度了,客户端在接收body时,就可以依照这个长度来接收数据,接收完后,就表示这个请求完成了。而如果没有content-length头,则客户端会一直接收数据,直到服务端主动断开连接,才表示body接收完了。
  2.     而对于http1.1协议来说,如果响应头中的Transfer-encoding为chunked传输,则表示body是流式输出,body会被分成多个块,每块的开始会标识出当前块的长度,此时,body不需要通过长度来指定。如果是非chunked传输,而且有content-length,则按照content-length来接收数据。否则,如果是非chunked,并且没有content-length,则客户端接收数据,直到服务端主动断开连接。
复制代码
Http1.0与Http1.1 length 
  当客户端的一次访问,需要多次访问同一个server时,打开keepalive的优势非常大,比如图片服务器,通常一个网页会包含很多个图片。打开keepalive也会大量减少time-wait的数量。
pipeline管道线

   管道技术是基于长连接的,目的是利用一个连接做多次请求。
  keepalive采用的是串行方式,而pipeline也不是并行的,但是它可以减少两个请求间的等待的事件。nginx在读取数据时,会将读取的数据放到一个buffer里面,所以,如果nginx在处理完前一个请求后,如果发现buffer里面还有数据,就认为剩下的数据是下一个请求的开始,然后就接下来处理下一个请求,否则就设置keepalive。
lingering_close延迟关闭

   当Nginx要关闭连接时,并非立即关闭连接,而是再等待一段时间后才真正关掉连接。目的在于读取客户端发来的剩下的数据。
  如果服务器直接关闭,恰巧客户端刚发送消息,那么就不会有ACK,导致出现没有任何错误信息的提示。
  Nginx通过设置一个读取客户数据的超时事件lingering_timeout来防止以上问题的发生。
 
  参考:http://tengine.taobao.org/book/#id2
 

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册