nginx源码琐碎

来源:互联网 发布:tpo模考软件mac版下载 编辑:程序博客网 时间:2024/06/05 21:28

推荐两个测试工具(适用于nginx 网络方面的测试):

ectproxy(王晓哲):https://github.com/chaoslawful/etcproxy

mockeagain(章亦春):https://github.com/agentzh/mockeagain

都是国人写的。

nginx源代码地址:http://trac.nginx.org/nginx/browser

 

nginx-core-progress:

 nginx-core-p

 

ngx_http_upstream_t中几个函数指针的作用:

create_request :创建请求时调用(对请求的内容进行处理)
reinit_request :重新请求
process_header:响应请求时调用。 (处理response的内容)

abort_request :放弃请求
finalize_request :完成请求 

1. 在ngx_http_request_t结构中包含upstream

2.upstream相关结构:


当前upstream各个节点为链表结构

struct ngx_http_upstream_rr_peers_s {
    ngx_uint_t                      single;        /* unsigned  single:1; */
    ngx_uint_t                      number;
    ngx_uint_t                      last_cached;


 /* ngx_mutex_t                    *mutex; */
    ngx_connection_t              **cached;


    ngx_str_t                      *name;


    ngx_http_upstream_rr_peers_t   *next;


    ngx_http_upstream_rr_peer_t     peer[1];
};


typedef struct {
    ngx_http_upstream_rr_peers_t   *peers;
    ngx_uint_t                      current;
    uintptr_t                      *tried;
    uintptr_t                       data;
} ngx_http_upstream_rr_peer_data_t;


typedef struct {
    ngx_http_upstream_init_pt        init_upstream;
    ngx_http_upstream_init_peer_pt   init;

//目前为ngx_http_upstream_rr_peers_t 
    void                            *data;
} ngx_http_upstream_peer_t;


struct ngx_http_upstream_srv_conf_s {


    ngx_http_upstream_peer_t         peer;
    void                           **srv_conf;


    ngx_array_t                     *servers;  /* ngx_http_upstream_server_t */


    ngx_uint_t                       flags;
    ngx_str_t                        host;
    u_char                          *file_name;
    ngx_uint_t                       line;
    in_port_t                        port;
    in_port_t                        default_port;
};

typedef struct {

  //当前站点upstream配置信息
    ngx_http_upstream_srv_conf_t    *upstream;


    ngx_msec_t                       connect_timeout;
    ngx_msec_t                       send_timeout;
    ngx_msec_t                       read_timeout;
    ngx_msec_t                       timeout;


    size_t                           send_lowat;
    size_t                           buffer_size;


    size_t                           busy_buffers_size;
    size_t                           max_temp_file_size;
    size_t                           temp_file_write_size;


    size_t                           busy_buffers_size_conf;
    size_t                           max_temp_file_size_conf;
    size_t                           temp_file_write_size_conf;


    ngx_bufs_t                       bufs;


    ngx_uint_t                       ignore_headers;
    ngx_uint_t                       next_upstream;
    ngx_uint_t                       store_access;
    ngx_flag_t                       buffering;
    ngx_flag_t                       pass_request_headers;
    ngx_flag_t                       pass_request_body;


    ngx_flag_t                       ignore_client_abort;
    ngx_flag_t                       intercept_errors;
    ngx_flag_t                       cyclic_temp_file;


    ngx_path_t                      *temp_path;


    ngx_hash_t                       hide_headers_hash;
    ngx_array_t                     *hide_headers;
    ngx_array_t                     *pass_headers;


    ngx_addr_t                      *local;


#if (NGX_HTTP_CACHE)
    ngx_shm_zone_t                  *cache;


    ngx_uint_t                       cache_min_uses;
    ngx_uint_t                       cache_use_stale;
    ngx_uint_t                       cache_methods;


    ngx_array_t                     *cache_valid;
    ngx_array_t                     *cache_bypass;
    ngx_array_t                     *no_cache;
#endif


    ngx_array_t                     *store_lengths;
    ngx_array_t                     *store_values;


    signed                           store:2;
    unsigned                         intercept_404:1;
    unsigned                         change_buffering:1;


#if (NGX_HTTP_SSL)
    ngx_ssl_t                       *ssl;
    ngx_flag_t                       ssl_session_reuse;
#endif


    ngx_str_t                        module;
} ngx_http_upstream_conf_t;


struct ngx_http_upstream_s {
    ngx_http_upstream_handler_pt     read_event_handler;
    ngx_http_upstream_handler_pt     write_event_handler;


    ngx_peer_connection_t            peer;


    ngx_event_pipe_t                *pipe;


    ngx_chain_t                     *request_bufs;


    ngx_output_chain_ctx_t           output;
    ngx_chain_writer_ctx_t           writer;

//upstream配置信息
    ngx_http_upstream_conf_t        *conf;


    ngx_http_upstream_headers_in_t   headers_in;


    ngx_http_upstream_resolved_t    *resolved;


    ngx_buf_t                        buffer;
    off_t                            length;


    ngx_chain_t                     *out_bufs;
    ngx_chain_t                     *busy_bufs;
    ngx_chain_t                     *free_bufs;


    ngx_int_t                      (*input_filter_init)(void *data);
    ngx_int_t                      (*input_filter)(void *data, ssize_t bytes);
    void                            *input_filter_ctx;


#if (NGX_HTTP_CACHE)
    ngx_int_t                      (*create_key)(ngx_http_request_t *r);
#endif
    ngx_int_t                      (*create_request)(ngx_http_request_t *r);
    ngx_int_t                      (*reinit_request)(ngx_http_request_t *r);
    ngx_int_t                      (*process_header)(ngx_http_request_t *r);
    void                           (*abort_request)(ngx_http_request_t *r);
    void                           (*finalize_request)(ngx_http_request_t *r,
                                         ngx_int_t rc);
    ngx_int_t                      (*rewrite_redirect)(ngx_http_request_t *r,
                                         ngx_table_elt_t *h, size_t prefix);


    ngx_msec_t                       timeout;


    ngx_http_upstream_state_t       *state;


    ngx_str_t                        method;
    ngx_str_t                        schema;
    ngx_str_t                        uri;


    ngx_http_cleanup_pt             *cleanup;


    unsigned                         store:1;
    unsigned                         cacheable:1;
    unsigned                         accel:1;
    unsigned                         ssl:1;
#if (NGX_HTTP_CACHE)
    unsigned                         cache_status:3;
#endif


    unsigned                         buffering:1;
    unsigned                         keepalive:1;


    unsigned                         request_sent:1;
    unsigned                         header_sent:1;
};

ngx_http.c文件中的ngx_http_block函数功能:

1.初始化各http模块的配置信息

2 对main_conf,srv_conf,loc_conf进行赋值。

    main_conf:用于处理所有配置信息的函数指针。

    srv_conf:用于处理server中的配置信息的函数指针。

    loc_conf:用于处理server中的配置信息的函数指针。

3.合并配置文件中http{} 的main conf,server {}的 src config以及loc config(ngx_http文件中ngx_http_merge_servers)

4.建立location和upstream等的映射。

ngx_http_conf_get_module_main_conf,ngx_http_conf_get_module_src_conf ,ngx_http_conf_get_module_loc_conf用于获取各个模块相应的配置信息。

在ngx_http_request这个核心文件里,找到ngx_http_init_request函数,在该函数里,可以看到:

r->main_conf = cscf->ctx->main_conf;                                                                                                                        
 r->srv_conf = cscf->ctx->srv_conf;
r->loc_conf = cscf->ctx->loc_conf;

以上cscf为对应server的配置信息,r为request指针

请求流程(在ngx_http_request文件里):

      ngx_http_init_request=>ngx_http_process_request_headers(header处理完毕后)=>ngx_http_process_request=> ngx_http_handler=>ngx_http_core_run_phases(核心模块的处理)


在http redis模块的ngx_http_redis_merge_loc_conf函数里可以获取到该模块所要用到的upstream。





 

原创粉丝点击