• nginx基础架构


    •  

    1.模块

    1.1 ngx_module_t

    是ngx_module_s结构体的别名

    typedef struct ngx_module_s          ngx_module_t;

     ngx_module_s结构体定义如下

    1. struct ngx_module_s {
    2. ngx_uint_t ctx_index;
    3. ngx_uint_t index;
    4. char *name;
    5. ngx_uint_t spare0;
    6. ngx_uint_t spare1;
    7. ngx_uint_t version;
    8. const char *signature;
    9. void *ctx;
    10. ngx_command_t *commands;
    11. ngx_uint_t type;
    12. ngx_int_t (*init_master)(ngx_log_t *log);
    13. ngx_int_t (*init_module)(ngx_cycle_t *cycle);
    14. ngx_int_t (*init_process)(ngx_cycle_t *cycle);
    15. ngx_int_t (*init_thread)(ngx_cycle_t *cycle);
    16. void (*exit_thread)(ngx_cycle_t *cycle);
    17. void (*exit_process)(ngx_cycle_t *cycle);
    18. void (*exit_master)(ngx_cycle_t *cycle);
    19. uintptr_t spare_hook0;
    20. uintptr_t spare_hook1;
    21. uintptr_t spare_hook2;
    22. uintptr_t spare_hook3;
    23. uintptr_t spare_hook4;
    24. uintptr_t spare_hook5;
    25. uintptr_t spare_hook6;
    26. uintptr_t spare_hook7;
    27. };

    作为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

    核心模块接口其定义为

    1. typedef struct {
    2. ngx_str_t name;
    3. void *(*create_conf)(ngx_cycle_t *cycle);
    4. char *(*init_conf)(ngx_cycle_t *cycle, void *conf);
    5. } ngx_core_module_t;

    2.2 ngx_event_module_t 

    事件模块接口其定义为

    1. typedef struct {
    2. ngx_str_t *name;
    3. void *(*create_conf)(ngx_cycle_t *cycle);
    4. char *(*init_conf)(ngx_cycle_t *cycle, void *conf);
    5. ngx_event_actions_t actions;
    6. } ngx_event_module_t;

    2.3 ngx_http_module_t 

    http模块接口定义为

    1. typedef struct {
    2. ngx_int_t (*preconfiguration)(ngx_conf_t *cf);
    3. ngx_int_t (*postconfiguration)(ngx_conf_t *cf);
    4. void *(*create_main_conf)(ngx_conf_t *cf);
    5. char *(*init_main_conf)(ngx_conf_t *cf, void *conf);
    6. void *(*create_srv_conf)(ngx_conf_t *cf);
    7. char *(*merge_srv_conf)(ngx_conf_t *cf, void *prev, void *conf);
    8. void *(*create_loc_conf)(ngx_conf_t *cf);
    9. char *(*merge_loc_conf)(ngx_conf_t *cf, void *prev, void *conf);
    10. } ngx_http_module_t;

    2.4 ngx_mail_module_t 

    邮件模块接口定义 为

    1. typedef struct {
    2. ngx_mail_protocol_t *protocol;
    3. void *(*create_main_conf)(ngx_conf_t *cf);
    4. char *(*init_main_conf)(ngx_conf_t *cf, void *conf);
    5. void *(*create_srv_conf)(ngx_conf_t *cf);
    6. char *(*merge_srv_conf)(ngx_conf_t *cf, void *prev,
    7. void *conf);
    8. } ngx_mail_module_t;

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

    3. ngx_command_s

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

    1. struct ngx_command_s {
    2. ngx_str_t name;
    3. ngx_uint_t type;
    4. char *(*set)(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
    5. ngx_uint_t conf;
    6. ngx_uint_t offset;
    7. void *post;
    8. };

    4.ngx_cycle_t

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

    1. struct ngx_cycle_s {
    2. void ****conf_ctx;
    3. ngx_pool_t *pool; //内存池
    4. //日志
    5. ngx_log_t *log;
    6. ngx_log_t new_log;
    7. ngx_uint_t log_use_stderr; /* unsigned log_use_stderr:1; */
    8. //连接池
    9. ngx_connection_t **files;
    10. ngx_connection_t *free_connections;
    11. ngx_uint_t free_connection_n;
    12. //模块数组
    13. ngx_module_t **modules;
    14. ngx_uint_t modules_n;
    15. ngx_uint_t modules_used; /* unsigned modules_used:1; */
    16. ngx_queue_t reusable_connections_queue;
    17. ngx_uint_t reusable_connections_n;
    18. time_t connections_reuse_time;
    19. ngx_array_t listening;//监听端口列表
    20. ngx_array_t paths; //保存所有的操作目录
    21. //dump配置文件
    22. ngx_array_t config_dump;//dump配置文件 动态数组
    23. ngx_rbtree_t config_dump_rbtree;
    24. ngx_rbtree_node_t config_dump_sentinel;
    25. ngx_list_t open_files; //打开的文件列表
    26. ngx_list_t shared_memory;//共享内存列表
    27. ngx_uint_t connection_n;
    28. ngx_uint_t files_n;
    29. ngx_connection_t *connections;//指向当前进程中所有连接对象,与connection_n
    30. ngx_event_t *read_events;//指向当前进程中所有读事件对象
    31. ngx_event_t *write_events;//指向当前进程中所有写事件对象
    32. ngx_cycle_t *old_cycle;//主要是保存启动前期的ngx_cycle_t对象
    33. //配置文件信息
    34. ngx_str_t conf_file;//配置文件相对于安装目录的路径名称
    35. ngx_str_t conf_param;//nginx处理配置文件时需要特殊处理的在命令行携带的参数
    36. ngx_str_t conf_prefix;//nginx配置文件所在目录的路径
    37. ngx_str_t prefix;//nginx安装目录的路径
    38. ngx_str_t error_log;
    39. ngx_str_t lock_file;
    40. ngx_str_t hostname; //主机名
    41. };

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

    1. struct ngx_listening_s {
    2. ngx_socket_t fd;
    3. struct sockaddr *sockaddr;
    4. socklen_t socklen; /* size of sockaddr */
    5. size_t addr_text_max_len;
    6. ngx_str_t addr_text;
    7. int type;
    8. int backlog;
    9. int rcvbuf;
    10. int sndbuf;
    11. #if (NGX_HAVE_KEEPALIVE_TUNABLE)
    12. int keepidle;
    13. int keepintvl;
    14. int keepcnt;
    15. #endif
    16. /* handler of accepted connection */
    17. ngx_connection_handler_pt handler;
    18. void *servers; /* array of ngx_http_in_addr_t, for example */
    19. ngx_log_t log;
    20. ngx_log_t *logp;
    21. size_t pool_size;
    22. /* should be here because of the AcceptEx() preread */
    23. size_t post_accept_buffer_size;
    24. ngx_listening_t *previous;
    25. ngx_connection_t *connection;
    26. ngx_rbtree_t rbtree;
    27. ngx_rbtree_node_t sentinel;
    28. ngx_uint_t worker;
    29. unsigned open:1;
    30. unsigned remain:1;
    31. unsigned ignore:1;
    32. unsigned bound:1; /* already bound */
    33. unsigned inherited:1; /* inherited from previous process */
    34. unsigned nonblocking_accept:1;
    35. unsigned listen:1;
    36. unsigned nonblocking:1;
    37. unsigned shared:1; /* shared between threads or processes */
    38. unsigned addr_ntop:1;
    39. unsigned wildcard:1;
    40. #if (NGX_HAVE_INET6)
    41. unsigned ipv6only:1;
    42. #endif
    43. unsigned reuseport:1;
    44. unsigned add_reuseport:1;
    45. unsigned keepalive:2;
    46. unsigned deferred_accept:1;
    47. unsigned delete_deferred:1;
    48. unsigned add_deferred:1;
    49. #if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)
    50. char *accept_filter;
    51. #endif
    52. #if (NGX_HAVE_SETFIB)
    53. int setfib;
    54. #endif
    55. #if (NGX_HAVE_TCP_FASTOPEN)
    56. int fastopen;
    57. #endif
    58. };

    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博客

  • 相关阅读:
    【go】defer的使用
    SaaSBase:微宏科技是什么?
    Resolving Protobuf Configuration Issue Without Sudo Access
    学历证书查询 易语言代码
    磁性核壳四氧化三铁颗粒负载金纳米星|磁性Fe3O4-POSS-COOH|超顺磁四氧化三铁聚多巴胺核壳结构纳米粒子
    人工智能机器学习底层原理剖析,人造神经元,您一定能看懂,通俗解释把AI“黑话”转化为“白话文”
    SpringBootCMS漏洞复现分析
    apt-get upgrade 和 dist-upgrade 之间的区别
    重识Nginx - 11 使用ngx_http_proxy_module的proxy_cache搭建一个具备缓存功能的反向代理服务
    UWB室内定位技术
  • 原文地址:https://blog.csdn.net/wuli2496/article/details/127954081