Nginx ����
����
/
WEB, EMAIL�
TCP�
!�����"���
�������������
������
������ HTTP��� MAIL���
Access����Error����
FastCGI, uWSGI. SCGI, memcached��
HTTP, Mail �stream(TCP) ��
Nginx����
Nginx����
Master��Worker�� Cache manager
Cache loader
Nginx�������
Master�� Worker�� nginx��
Ø �worker��• CHLD
Ø �worker��Ø ����• TERM, INT• QUIT• HUP• USR1• USR2• WINCH
Ø ����• TERM, INT
• QUIT
• USR1
• WINCH
Ø reload�HUP
Ø reopen�USR1
Ø stop�TERM
Ø quit�QUIT
reload��
�master,&.HUP���reload���
master,&���#$�'
master,&�*worker�,&.QUIT��
master,&�1/)+ ���%
master,&"�/)���#worker�,&
*worker,&�0$�����!���-� (�,&
01
02
03
04
05
06
reload��������
Master Master
W W W W
W W W W
W W
W W
Master.'�%!2)��!&worker.'
#!nginx.conf2) �Master�1SIGHUP ��+�,nginx -s reload
*2)Worker.'������/�"3&0�
������(��-,���$&#��2)
�����
��Nginx�����Nginx��������
master"���pid����� .oldbin
�!master"��#WINCH� �$!worker
�master"��#USR2�
master"���Nginx�����master"�
����!master�#HUP���master�#QUIT
01
02
03
04
05
06
� ����������������
Master�new�
W W
W W
Master�old�
W W
W W
�master����SIGQUIT�����
������
��Nginx��Master����SIGUSR2
��Nginx �������
Master
W W
W W
Master�new�
W W
W W
worker 4 2
01
02
0304
05
worker_shutdown_timeout
3 0
5
1
4
54
2
54
8
1
A B3 3
22
54545454
88 88
5 5 5
54
TCP����
TCP/IP���
DATA
DATA
DATA
DATA�bv. HTTP, SMTP…����
����
�IP����IP��
�MAC����MAC��
�
��
��
���
TCP, UDP
HTTP, SMTP, POP3,IMAP, SSH, DNS
Ethernet, ADSL, Wifi, PPP
IP, ICMP, DHCP, ARP
HEADER(UDP, TCP)
HEADER(IP)
HEADER(Ethernet)
FOOTER(Ethernet)
TCP��������
��
• Accept���
• Read��
���
• Write���
Nginx����
epoll
+
+
+
+
��Ø
��ØØ
��ØØ /ØØ
��
���������
• ����������
• ���������
��������
• ������
• ������������
�� �
�accept��
������
����
������������
'&�accept
'&�accept
�����"
�����"
�accept
�accept
��EAGAIN
�� ��
� ����'&�
� ����'&�
ACCEPT%���
ACCEPT%����
"EAGAIN$�
��� ���ACCEPT%������!�#�
�����������
local client = redis:new()client:set_timeout(30000)local ok,err = client:connect(ip,port)if not ok then
ngx.say("failed: ",err)return
end
Openresty�������
�� ������post_handler�� �
���������
����body���ngx_http_upstream_init �
rc = ngx_http_read_request_body ( r, ngx_http_upstream_init) ;if ( rc�= NGX_HTTP_SPECIAL_RESPONSE) {
return rc ;}
ngx_int_tngx_http_read_client_request_body (ngx_http_request_t * r,
ngx_http_client_body_handler_pt post_handler)
voidngx_http_upstream_init (ngx_http_request_t * r ){
Nginx �
�����ginx
������
�����
�����
Nginx ��7: :
: 3
7:3 : 3 7
7-33
7 77 3 07 37 7-
: 7 7-: 7 3: 7 7
: 3 0 7
773 +3++ 73 7
: 3 7
7 3 +7 3 +
: 3 3 0 7
7 3 +7 3 +
0003
0 33 -3 77
3
: 7 3 0 7
3 + 7 33 7 3 + 7 3
7 3 +7 3 +7 3 +
3 +7 03 3 +
03 3 +
: -77 3 0 7
7:
3 +3 +
: 0 3 + 7: 7
��
Ø
Ø �����
Ø �����• http• event• mail• stream
��
����
coreNGX_CORE_MODULE
event_core
NGX_EVENT_MODULE
epoll
ngx_http_core_module
NGX_HTTP_MODULE
�����
ngx_stream_core_module
NGX_STREAM_MODULE
steam��
upsteam���
������
errlog thread_pool
events http stream
openssl
ngx_conf_moduleNGX_CONF_MODULE
ngx_mail_core_module
NGX_MAIL_MODULE
��
: ( ( ( : ( ( ( : ( ( ( : +( ( ( ( : ( + ( : ( ( ( : ( + ( : ( ( (: : ( ( ( : ( ( : ( ( (: : +( ( ( ( : ( ( ( (
) :
() () (( () (( (( ( ( ( ( : ( (
( :+( :
( ) ( :+ ( ( :
() (
(( ( (
( ( ((
) ) ()) () :
() :) (
+ (
: (
d b (
d b (
d b (
(c a
( ( (( ( (
_b
��
�� �����
�� ����
����struct ngx_event_s {
void *data;
unsigned instance:1;
unsigned timedout:1;
unsigned timer_set:1;
unsigned available:1;
ngx_event_handler_pt handler;
ngx_uint_t index;
ngx_log_t *log;
ngx_rbtree_node_t timer;
ngx_queue_t queue;
… …
};
struct ngx_connection_s {
void *data;
ngx_event_t *read;
ngx_event_t *write; ����
ngx_socket_t fd;
ngx_recv_pt recv;
ngx_send_pt send; � �OS���
off_t sent; <= bytes_sent��ngx_log_t *log;
ngx_pool_t *pool; <= �� connection_pool_size��
int type;
struct sockaddr *sockaddr;
socklen_t socklen;
ngx_str_t addr_text;
ngx_str_t proxy_protocol_addr;
in_port_t proxy_protocol_port;
ngx_buf_t *buffer;
ngx_queue_t queue;
};
����
�����
���
�� ����
������
Nginx ������
�����
������
• �
• ����
• �
• ������ �
������worker������ nginx��������
�������
Ngx_http_lua_api
rbtree
���
Ngx_stream_limit_conn_moduleNgx_http_limit_conn_moduleNgx_stream_limit_req_module
http cache
ssl
Ngx_http_upstream_zone_module
Ngx_stream_upstream_zone_module
Ngx_http_file_cacheNgx_http_proxy_moduleNgx_http_scgi_moduleNgx_http_uwsgi_moduleNgx_http_fastcgi_module
Ngx_http_ssl_modulNgx_mail_ssl_moduleNgx_stream_ssl_module
OpenResty��������
Slab����
object
object
object
ngx_slab_page_t
object
object
object
ngx_slab_page_t
ngx_slab_page_t
objectngx_slab_page_t
ngx_slab_page_t
ngx_slab_page_t
ngx_slab_page_t
ngx_slab_page_t
slot:2�
slot:3�
�$&
#
��&�����&
#
���$&
��#
������&��# ���
���slot�
�!1�" ���1�����&%
ngx_slab_page_t
• �������
Bestfit
• �� ��
• ����
• ������
Bestfit
ngx_slab_stat���Slab����
Nginx ��
�
��
�
���
��
���
Nginx ���
�����
Bucket size����
Max size
���2 7+ 6 + 2 62 7+ 6 + 25 62 7+ 6 + 1 6
3365 26 2 125 6
2 7+ 6 +6
()
8
��
��
��
��
�
�
�
��
��
��
���
������2�log(n)
����O(n)
�� �����O(log(n))�������
��������
���
ngx_conf_modulengx_event_timer_rbtreeNgx_http_file_cacheNgx_http_geo_moduleNgx_http_limit_conn_moduleNgx_http_limit_req_moduleNgx_http_lua_shdict:ngx.shared.DICTresolver Ngx_stream_geo_moduleNgx_stream_limit_conn_module
ngx_resolver_t
LRU����
config_dump_rbtree
���-�����
load_module modules/ngx_http_image_filter_module.so;
Configure�����
���binary
���������
��load_module��
���� ������
���������
/