perrynzhou

专注于系统组件研发

0%

nginx 简单介绍

nginx 简单介绍

什么是nginx?

  • nginx是用纯C开发的一套web服务器软件,运行于用户态度
  • nginx 是一款web服务器,类似的产品还有apache

    nginx 能做什么?

  • 反向代理:后端服务器被代理的过程叫反向代理过程,代理后端服务器的节点叫做反向代理节点。举个例子,现在我们业务搭建了4个节点的服务集群,这4个节点是对等的,业务的一次请求不会请求4个节点的IP来完成任务,对外提供的只有一个公网IP,公网IP是请求流量的入口,这个时候就需要另外一个节点来代理内网中的4个节点集群服务,用户请求流量都是通过这个公网IP的节点进入,用nginx来做方向代理,把请求流量转发到服务集群中。
  • 正向代理: 客户端被代理的过程叫正向代理,比如我们使用企业的VPN,安装VPN后,浏览器请求google,这个时候可以使用nginx来代理你的客户端来请求google的服务。
  • 负载均衡:在反向代理中,每个被代理服务器可以设置不同的权重,用户请求到反向代理服务节点上会根据权重,把请求下发到不同权重的后端服务器上。比如每个服务节点的配置不同,为了达到配置比较好的节点能处理更多流量,配置低的节点处理少量流量的目的,我们使用nginx来为每个被代理节点设置权重来达到负载均衡。

nginx 应用场景有哪些?

  • 静态资源:访问的资源和nginx部署的节点是在同一个服务器上。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// 路径为:/home/perrynzhou/images下面有2张png图片
//修改配置文件nginx.conf
server {
listen 80;
server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
root /home/perrynzhou/images/;
index index.html index.htm;
}
// nginx -s reload
// http://172.25.14.71/2.png 即可访问图片
  • API服务:代理后端API服务,比如代理redis api服务
  • 反向代理: 代理后端服务器
  • 缓存加速:nginx能否缓存请求的资源
  • 负载均衡: nginx按照权重设置代理服务或者节点的优先级,按照优先级转发请求的流量

nginx的同类产品分析

  • F5:硬件F5负载均衡器工作在数据链路层,基于mac地址的负载均衡。
  • lvs: 软件层的负载均衡,工作在网络层(IP层),基于IP地址的负载均衡。
  • haproxy:工作在传输层,基于tcp/ip的聚在均衡器
  • nginx:工作在应用层的HTTP协议,基于http的聚在负载均衡器。

nginx 进程结构

  • nginx进程结构分为master和worker进程,master进程和worker进程是1对多的关系。

  • master进程:主要用来管理worker进程,接收来自外界的信号,向各个worker进程发送信号,监控worker进程的运行状态当worker进程退出后,会自动情动新的worker进程,master进程扮演用户和worker进程的交互接口角色,同时对进程进行监护,他不需要处理网络事件,不负责业务执行,只会通过worker进程来实现重启服务,平滑升级,更换日志文件,配置文件生效等功能

  • worker进程 : worker进程相互之间隔离和对等的,具有相同的几率去处理请求,所有的worker进程都是从master进程fork出来的,所有worker进程的listenfd会在新连接到来时变得可读,为保证只有一个进程处理该连接,所有worker进程在注册listenfd读事件前抢accept_mutex,抢到互斥锁的那个进程注册listenfd读事件,在读事件里调用accept接受该连接。当一个worker进程在accept这个连接之后,就开始读取请求,解析请求,处理请求,产生数据后,再返回给客户端,最后才断开连接,这样一个完整的请求就是这样的了。一个请求,完全由worker进程来处理,而且只在一个worker进程中处理。

nginx的核心模块

  • nginx 采用的模块方式组装整个nginx的功能,在编译阶段会产生一个ngx_modules.c的文件,该文件中定义了所有nginx的处理模块,其中有一个ngx_modules的数组。

  • 当一个请求同时符合多个模块的处理规则时候,按照ngx_modules数组中的顺序选择最靠前的模块优先处理。

  • 针对http的过滤模块而言则是相反的,因为http框架在初始化时候,会在ngx_modules的数组中将过滤模块按先后顺序向filter list中添加,每次添加都是添加到表头,因此针对http模块,越是靠后的模块越是优先响应http.

  • ngx_modules.c 定义

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    #include <ngx_config.h>
    #include <ngx_core.h>



    extern ngx_module_t ngx_core_module;
    extern ngx_module_t ngx_errlog_module;
    extern ngx_module_t ngx_conf_module;
    extern ngx_module_t ngx_regex_module;
    extern ngx_module_t ngx_events_module;
    extern ngx_module_t ngx_event_core_module;
    extern ngx_module_t ngx_epoll_module;
    extern ngx_module_t ngx_http_module;
    extern ngx_module_t ngx_http_core_module;
    extern ngx_module_t ngx_http_log_module;
    extern ngx_module_t ngx_http_upstream_module;
    extern ngx_module_t ngx_http_static_module;
    extern ngx_module_t ngx_http_autoindex_module;
    extern ngx_module_t ngx_http_index_module;
    extern ngx_module_t ngx_http_mirror_module;
    extern ngx_module_t ngx_http_try_files_module;
    extern ngx_module_t ngx_http_auth_basic_module;
    extern ngx_module_t ngx_http_access_module;
    extern ngx_module_t ngx_http_limit_conn_module;
    extern ngx_module_t ngx_http_limit_req_module;
    extern ngx_module_t ngx_http_geo_module;
    extern ngx_module_t ngx_http_map_module;
    extern ngx_module_t ngx_http_split_clients_module;
    extern ngx_module_t ngx_http_referer_module;
    extern ngx_module_t ngx_http_rewrite_module;
    extern ngx_module_t ngx_http_proxy_module;
    extern ngx_module_t ngx_http_fastcgi_module;
    extern ngx_module_t ngx_http_uwsgi_module;
    extern ngx_module_t ngx_http_scgi_module;
    extern ngx_module_t ngx_http_memcached_module;
    extern ngx_module_t ngx_http_empty_gif_module;
    extern ngx_module_t ngx_http_browser_module;
    extern ngx_module_t ngx_http_upstream_hash_module;
    extern ngx_module_t ngx_http_upstream_ip_hash_module;
    extern ngx_module_t ngx_http_upstream_least_conn_module;
    extern ngx_module_t ngx_http_upstream_random_module;
    extern ngx_module_t ngx_http_upstream_keepalive_module;
    extern ngx_module_t ngx_http_upstream_zone_module;
    extern ngx_module_t ngx_http_write_filter_module;
    extern ngx_module_t ngx_http_header_filter_module;
    extern ngx_module_t ngx_http_chunked_filter_module;
    extern ngx_module_t ngx_http_range_header_filter_module;
    extern ngx_module_t ngx_http_gzip_filter_module;
    extern ngx_module_t ngx_http_postpone_filter_module;
    extern ngx_module_t ngx_http_ssi_filter_module;
    extern ngx_module_t ngx_http_charset_filter_module;
    extern ngx_module_t ngx_http_userid_filter_module;
    extern ngx_module_t ngx_http_headers_filter_module;
    extern ngx_module_t ngx_http_copy_filter_module;
    extern ngx_module_t ngx_http_range_body_filter_module;
    extern ngx_module_t ngx_http_not_modified_filter_module;

    ngx_module_t *ngx_modules[] = {
    &ngx_core_module,
    &ngx_errlog_module,
    &ngx_conf_module,
    &ngx_regex_module,
    &ngx_events_module,
    &ngx_event_core_module,
    &ngx_epoll_module,
    &ngx_http_module,
    &ngx_http_core_module,
    &ngx_http_log_module,
    &ngx_http_upstream_module,
    &ngx_http_static_module,
    &ngx_http_autoindex_module,
    &ngx_http_index_module,
    &ngx_http_mirror_module,
    &ngx_http_try_files_module,
    &ngx_http_auth_basic_module,
    &ngx_http_access_module,
    &ngx_http_limit_conn_module,
    &ngx_http_limit_req_module,
    &ngx_http_geo_module,
    &ngx_http_map_module,
    &ngx_http_split_clients_module,
    &ngx_http_referer_module,
    &ngx_http_rewrite_module,
    &ngx_http_proxy_module,
    &ngx_http_fastcgi_module,
    &ngx_http_uwsgi_module,
    &ngx_http_scgi_module,
    &ngx_http_memcached_module,
    &ngx_http_empty_gif_module,
    &ngx_http_browser_module,
    &ngx_http_upstream_hash_module,
    &ngx_http_upstream_ip_hash_module,
    &ngx_http_upstream_least_conn_module,
    &ngx_http_upstream_random_module,
    &ngx_http_upstream_keepalive_module,
    &ngx_http_upstream_zone_module,
    &ngx_http_write_filter_module,
    &ngx_http_header_filter_module,
    &ngx_http_chunked_filter_module,
    &ngx_http_range_header_filter_module,
    &ngx_http_gzip_filter_module,
    &ngx_http_postpone_filter_module,
    &ngx_http_ssi_filter_module,
    &ngx_http_charset_filter_module,
    &ngx_http_userid_filter_module,
    &ngx_http_headers_filter_module,
    &ngx_http_copy_filter_module,
    &ngx_http_range_body_filter_module,
    &ngx_http_not_modified_filter_module,
    NULL
    };

    char *ngx_module_names[] = {
    "ngx_core_module",
    "ngx_errlog_module",
    "ngx_conf_module",
    "ngx_regex_module",
    "ngx_events_module",
    "ngx_event_core_module",
    "ngx_epoll_module",
    "ngx_http_module",
    "ngx_http_core_module",
    "ngx_http_log_module",
    "ngx_http_upstream_module",
    "ngx_http_static_module",
    "ngx_http_autoindex_module",
    "ngx_http_index_module",
    "ngx_http_mirror_module",
    "ngx_http_try_files_module",
    "ngx_http_auth_basic_module",
    "ngx_http_access_module",
    "ngx_http_limit_conn_module",
    "ngx_http_limit_req_module",
    "ngx_http_geo_module",
    "ngx_http_map_module",
    "ngx_http_split_clients_module",
    "ngx_http_referer_module",
    "ngx_http_rewrite_module",
    "ngx_http_proxy_module",
    "ngx_http_fastcgi_module",
    "ngx_http_uwsgi_module",
    "ngx_http_scgi_module",
    "ngx_http_memcached_module",
    "ngx_http_empty_gif_module",
    "ngx_http_browser_module",
    "ngx_http_upstream_hash_module",
    "ngx_http_upstream_ip_hash_module",
    "ngx_http_upstream_least_conn_module",
    "ngx_http_upstream_random_module",
    "ngx_http_upstream_keepalive_module",
    "ngx_http_upstream_zone_module",
    "ngx_http_write_filter_module",
    "ngx_http_header_filter_module",
    "ngx_http_chunked_filter_module",
    "ngx_http_range_header_filter_module",
    "ngx_http_gzip_filter_module",
    "ngx_http_postpone_filter_module",
    "ngx_http_ssi_filter_module",
    "ngx_http_charset_filter_module",
    "ngx_http_userid_filter_module",
    "ngx_http_headers_filter_module",
    "ngx_http_copy_filter_module",
    "ngx_http_range_body_filter_module",
    "ngx_http_not_modified_filter_module",
    NULL
    };