nginx基础架构

24 篇文章 1 订阅
  •  

1.模块

1.1 ngx_module_t

是ngx_module_s结构体的别名

typedef struct ngx_module_s          ngx_module_t;

 ngx_module_s结构体定义如下

struct ngx_module_s {
    ngx_uint_t            ctx_index;
    ngx_uint_t            index;

    char                 *name;

    ngx_uint_t            spare0;
    ngx_uint_t            spare1;

    ngx_uint_t            version;
    const char           *signature;

    void                 *ctx;
    ngx_command_t        *commands;
    ngx_uint_t            type;

    ngx_int_t           (*init_master)(ngx_log_t *log);

    ngx_int_t           (*init_module)(ngx_cycle_t *cycle);

    ngx_int_t           (*init_process)(ngx_cycle_t *cycle);
    ngx_int_t           (*init_thread)(ngx_cycle_t *cycle);
    void                (*exit_thread)(ngx_cycle_t *cycle);
    void                (*exit_process)(ngx_cycle_t *cycle);

    void                (*exit_master)(ngx_cycle_t *cycle);

    uintptr_t             spare_hook0;
    uintptr_t             spare_hook1;
    uintptr_t             spare_hook2;
    uintptr_t             spare_hook3;
    uintptr_t             spare_hook4;
    uintptr_t             spare_hook5;
    uintptr_t             spare_hook6;
    uintptr_t             spare_hook7;
};

作为nginx模块的通用接口。主要方法有

  • init_master
  • init_module
  • init_process
  • init_thread
  • exit_thread
  • exit_process
  • exit_master

成员有

ctx:void*,提供灵活性

type:模块类型

2.模块子接口 

2.1 ngx_core_module_t

核心模块接口其定义为

typedef struct {
    ngx_str_t             name;
    void               *(*create_conf)(ngx_cycle_t *cycle);
    char               *(*init_conf)(ngx_cycle_t *cycle, void *conf);
} ngx_core_module_t;

2.2 ngx_event_module_t 

事件模块接口其定义为

typedef struct {
    ngx_str_t              *name;

    void                 *(*create_conf)(ngx_cycle_t *cycle);
    char                 *(*init_conf)(ngx_cycle_t *cycle, void *conf);

    ngx_event_actions_t     actions;
} ngx_event_module_t;

2.3 ngx_http_module_t 

http模块接口定义为

typedef struct {
    ngx_int_t   (*preconfiguration)(ngx_conf_t *cf);
    ngx_int_t   (*postconfiguration)(ngx_conf_t *cf);

    void       *(*create_main_conf)(ngx_conf_t *cf);
    char       *(*init_main_conf)(ngx_conf_t *cf, void *conf);

    void       *(*create_srv_conf)(ngx_conf_t *cf);
    char       *(*merge_srv_conf)(ngx_conf_t *cf, void *prev, void *conf);

    void       *(*create_loc_conf)(ngx_conf_t *cf);
    char       *(*merge_loc_conf)(ngx_conf_t *cf, void *prev, void *conf);
} ngx_http_module_t;

2.4 ngx_mail_module_t 

邮件模块接口定义 为

typedef struct {
    ngx_mail_protocol_t        *protocol;

    void                       *(*create_main_conf)(ngx_conf_t *cf);
    char                       *(*init_main_conf)(ngx_conf_t *cf, void *conf);

    void                       *(*create_srv_conf)(ngx_conf_t *cf);
    char                       *(*merge_srv_conf)(ngx_conf_t *cf, void *prev,
                                                  void *conf);
} ngx_mail_module_t;

模块与模块子接口的关系主要是通过模块中的ctx来指定,与继承关系有些类似

3. ngx_command_s

主要用于在解析配置时来设置配置项,其接口定义为

struct ngx_command_s {
    ngx_str_t             name;
    ngx_uint_t            type;
    char               *(*set)(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
    ngx_uint_t            conf;
    ngx_uint_t            offset;
    void                 *post;
};

4.ngx_cycle_t

是ngx_cycle_s结构体的别名,其结构定义如下

struct ngx_cycle_s {
    void                  ****conf_ctx;
    ngx_pool_t               *pool;  //内存池
    
    //日志
    ngx_log_t                *log;  
    ngx_log_t                 new_log;

    ngx_uint_t                log_use_stderr;  /* unsigned  log_use_stderr:1; */
    
    //连接池
    ngx_connection_t        **files;
    ngx_connection_t         *free_connections;
    ngx_uint_t                free_connection_n;

    //模块数组
    ngx_module_t            **modules;
    ngx_uint_t                modules_n;
    ngx_uint_t                modules_used;    /* unsigned  modules_used:1; */

    ngx_queue_t               reusable_connections_queue;
    ngx_uint_t                reusable_connections_n;
    time_t                    connections_reuse_time;

    ngx_array_t               listening;//监听端口列表
    ngx_array_t               paths; //保存所有的操作目录

    //dump配置文件 
    ngx_array_t               config_dump;//dump配置文件 动态数组
    ngx_rbtree_t              config_dump_rbtree;
    ngx_rbtree_node_t         config_dump_sentinel;

    ngx_list_t                open_files; //打开的文件列表
    ngx_list_t                shared_memory;//共享内存列表

    ngx_uint_t                connection_n;
    ngx_uint_t                files_n;

    ngx_connection_t         *connections;//指向当前进程中所有连接对象,与connection_n
    ngx_event_t              *read_events;//指向当前进程中所有读事件对象
    ngx_event_t              *write_events;//指向当前进程中所有写事件对象

    ngx_cycle_t              *old_cycle;//主要是保存启动前期的ngx_cycle_t对象
    
    //配置文件信息
    ngx_str_t                 conf_file;//配置文件相对于安装目录的路径名称
    ngx_str_t                 conf_param;//nginx处理配置文件时需要特殊处理的在命令行携带的参数
    ngx_str_t                 conf_prefix;//nginx配置文件所在目录的路径
    ngx_str_t                 prefix;//nginx安装目录的路径
    ngx_str_t                 error_log;
    ngx_str_t                 lock_file;
    ngx_str_t                 hostname; //主机名
};

 listening是数组ngx_array_t,其元素类型为ngx_listening_s,表示服务器监听的一个端口。其结构体定义为

struct ngx_listening_s {
    ngx_socket_t        fd;

    struct sockaddr    *sockaddr;
    socklen_t           socklen;    /* size of sockaddr */
    size_t              addr_text_max_len;
    ngx_str_t           addr_text;

    int                 type;

    int                 backlog;
    int                 rcvbuf;
    int                 sndbuf;
#if (NGX_HAVE_KEEPALIVE_TUNABLE)
    int                 keepidle;
    int                 keepintvl;
    int                 keepcnt;
#endif

    /* handler of accepted connection */
    ngx_connection_handler_pt   handler;

    void               *servers;  /* array of ngx_http_in_addr_t, for example */

    ngx_log_t           log;
    ngx_log_t          *logp;

    size_t              pool_size;
    /* should be here because of the AcceptEx() preread */
    size_t              post_accept_buffer_size;

    ngx_listening_t    *previous;
    ngx_connection_t   *connection;

    ngx_rbtree_t        rbtree;
    ngx_rbtree_node_t   sentinel;

    ngx_uint_t          worker;

    unsigned            open:1;
    unsigned            remain:1;
    unsigned            ignore:1;

    unsigned            bound:1;       /* already bound */
    unsigned            inherited:1;   /* inherited from previous process */
    unsigned            nonblocking_accept:1;
    unsigned            listen:1;
    unsigned            nonblocking:1;
    unsigned            shared:1;    /* shared between threads or processes */
    unsigned            addr_ntop:1;
    unsigned            wildcard:1;

#if (NGX_HAVE_INET6)
    unsigned            ipv6only:1;
#endif
    unsigned            reuseport:1;
    unsigned            add_reuseport:1;
    unsigned            keepalive:2;

    unsigned            deferred_accept:1;
    unsigned            delete_deferred:1;
    unsigned            add_deferred:1;
#if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)
    char               *accept_filter;
#endif
#if (NGX_HAVE_SETFIB)
    int                 setfib;
#endif

#if (NGX_HAVE_TCP_FASTOPEN)
    int                 fastopen;
#endif

};

5.五大类型模块

5.1 核心模块(NGX_CORE_MODULE)

  1. ngx_core_module

  2. ngx_events_module

  3. ngx_openssl_module

  4. ngx_http_module

  5.  ngx_mail_module

  6. ngx_errlog_module

  7. ngx_regex_module,

  8. ngx_stream_module

  9. ngx_thread_pool_module

  10. ngx_google_perftools_module

5.2 配置模块(NGX_CONF_MODULE)

  1. ngx_conf_module

5.3 事件模块(NGX_EVENT_MODULE)

  1. ngx_event_core_module
  2. ngx_devpoll_module
  3. ngx_epoll_module
  4. ngx_eventport_module
  5. ngx_iocp_module
  6. ngx_kqueue_module
  7. ngx_poll_module
  8. ngx_select_module
  9. ngx_poll_module(win32)
  10. ngx_select_module(win32)

5.4 HTTP模块(NGX_HTTP_MODULE)

  1. ngx_http_core_module
  2. ngx_http_addition_filter_module
  3. ngx_http_auth_basic_module
  4. ngx_http_auth_request_module
  5. ngx_http_autoindex_module
  6. ngx_http_browser_module
  7. ngx_http_charset_filter_module
  8. ngx_http_chunked_filter_module
  9. ngx_http_copy_filter_module
  10. ngx_http_dav_module
  11. ngx_http_degradation_module
  12. ngx_http_empty_gif_module
  13. ngx_http_fastcgi_module
  14. ngx_http_flv_module
  15. ngx_http_geoip_module
  16. ngx_http_geo_module
  17. ngx_http_grpc_module
  18. ngx_http_gunzip_filter_module
  19. ngx_http_gzip_filter_module
  20. ngx_http_gzip_static_module
  21. ngx_http_headers_filter_module
  22. ngx_http_header_filter_module
  23. ngx_http_image_filter_module
  24. ngx_http_index_module
  25. ngx_http_limit_conn_module
  26. ngx_http_limit_req_module
  27. ngx_http_log_module
  28. ngx_http_map_module
  29. ngx_http_memcached_module
  30. ngx_http_mirror_module
  31. ngx_http_mp4_module
  32. ngx_http_not_modified_filter_module
  33. ngx_http_perl_module
  34. ngx_http_postpone_filter_module
  35. ngx_http_proxy_module
  36. ngx_http_random_index_module
  37. ngx_http_range_header_filter_module
  38. ngx_http_range_body_filter_module
  39. ngx_http_realip_module
  40. ngx_http_referer_module
  41. ngx_http_rewrite_module
  42. ngx_http_scgi_module
  43. ngx_http_secure_link_module
  44. ngx_http_slice_filter_module
  45. ngx_http_split_clients_module
  46. ngx_http_ssi_filter_module
  47. ngx_http_ssl_module
  48. ngx_http_static_module
  49. ngx_http_stub_status_module
  50. ngx_http_sub_filter_module
  51. ngx_http_try_files_module
  52. ngx_http_upstream_module
  53. ngx_http_upstream_hash_module
  54. ngx_http_upstream_ip_hash_module
  55. ngx_http_upstream_keepalive_module
  56. ngx_http_upstream_least_conn_module
  57. ngx_http_upstream_random_module
  58. ngx_http_upstream_zone_module
  59. ngx_http_userid_filter_module
  60. ngx_http_uwsgi_module
  61. ngx_http_v2_filter_module
  62. ngx_http_v2_module
  63. ngx_http_write_filter_module
  64. ngx_http_xslt_filter_module

5.5 mail模块(NGX_MAIL_MODULE)

  1. ngx_mail_core_module
  2. ngx_mail_auth_http_module
  3. ngx_mail_imap_module
  4. ngx_mail_pop3_module
  5. ngx_mail_proxy_module
  6. ngx_mail_realip_module
  7. ngx_mail_smtp_module
  8. ngx_mail_ssl_module

6、事件模块

6.1 基础数据结构

主要包含

  • ngx_event_actions_t:对于不同操作系统中事件管理的抽象
  • ngx_event_module_t:事件模块的抽象
  • ngx_event_t:描述事件数据

 其关系如下:

详细参考:nginx事件模块_kgduu的博客-CSDN博客

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

kgduu

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

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

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

打赏作者

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

抵扣说明:

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

余额充值