ant-loiter · worker nginx ngx_http_lua_api rbtree ngx_stream_limit_conn_module...

37
Nginx

Upload: others

Post on 27-Jun-2020

16 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Ant-loiter · worker nginx Ngx_http_lua_api rbtree Ngx_stream_limit_conn_module Ngx_http_limit_conn_module Ngx_stream_limit_req_module http cache ssl Ngx_http_upstream_zone_module

Nginx ����

����

Page 2: Ant-loiter · worker nginx Ngx_http_lua_api rbtree Ngx_stream_limit_conn_module Ngx_http_limit_conn_module Ngx_stream_limit_req_module http cache ssl Ngx_http_upstream_zone_module

WEB, EMAIL�

TCP�

!�����"���

�������������

������

������ HTTP��� MAIL���

Access����Error����

FastCGI, uWSGI. SCGI, memcached��

HTTP, Mail �stream(TCP) ��

Nginx����

Page 3: Ant-loiter · worker nginx Ngx_http_lua_api rbtree Ngx_stream_limit_conn_module Ngx_http_limit_conn_module Ngx_stream_limit_req_module http cache ssl Ngx_http_upstream_zone_module

Nginx����

Master��Worker�� Cache manager

Cache loader

Page 4: Ant-loiter · worker nginx Ngx_http_lua_api rbtree Ngx_stream_limit_conn_module Ngx_http_limit_conn_module Ngx_stream_limit_req_module http cache ssl Ngx_http_upstream_zone_module

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

Page 5: Ant-loiter · worker nginx Ngx_http_lua_api rbtree Ngx_stream_limit_conn_module Ngx_http_limit_conn_module Ngx_stream_limit_req_module http cache ssl Ngx_http_upstream_zone_module

reload��

�master,&.HUP���reload���

master,&���#$�'

master,&�*worker�,&.QUIT��

master,&�1/)+ ���%

master,&"�/)���#worker�,&

*worker,&�0$�����!���-� (�,&

01

02

03

04

05

06

Page 6: Ant-loiter · worker nginx Ngx_http_lua_api rbtree Ngx_stream_limit_conn_module Ngx_http_limit_conn_module Ngx_stream_limit_req_module http cache ssl Ngx_http_upstream_zone_module

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)

Page 7: Ant-loiter · worker nginx Ngx_http_lua_api rbtree Ngx_stream_limit_conn_module Ngx_http_limit_conn_module Ngx_stream_limit_req_module http cache ssl Ngx_http_upstream_zone_module

�����

��Nginx�����Nginx��������

master"���pid����� .oldbin

�!master"��#WINCH� �$!worker

�master"��#USR2�

master"���Nginx�����master"�

����!master�#HUP���master�#QUIT

01

02

03

04

05

06

Page 8: Ant-loiter · worker nginx Ngx_http_lua_api rbtree Ngx_stream_limit_conn_module Ngx_http_limit_conn_module Ngx_stream_limit_req_module http cache ssl Ngx_http_upstream_zone_module

� ����������������

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

Page 9: Ant-loiter · worker nginx Ngx_http_lua_api rbtree Ngx_stream_limit_conn_module Ngx_http_limit_conn_module Ngx_stream_limit_req_module http cache ssl Ngx_http_upstream_zone_module

worker 4 2

01

02

0304

05

worker_shutdown_timeout

3 0

5

1

4

Page 10: Ant-loiter · worker nginx Ngx_http_lua_api rbtree Ngx_stream_limit_conn_module Ngx_http_limit_conn_module Ngx_stream_limit_req_module http cache ssl Ngx_http_upstream_zone_module

54

2

54

8

1

A B3 3

22

54545454

88 88

5 5 5

54

Page 11: Ant-loiter · worker nginx Ngx_http_lua_api rbtree Ngx_stream_limit_conn_module Ngx_http_limit_conn_module Ngx_stream_limit_req_module http cache ssl Ngx_http_upstream_zone_module

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)

Page 12: Ant-loiter · worker nginx Ngx_http_lua_api rbtree Ngx_stream_limit_conn_module Ngx_http_limit_conn_module Ngx_stream_limit_req_module http cache ssl Ngx_http_upstream_zone_module

TCP��������

��

• Accept���

• Read��

���

• Write���

Page 13: Ant-loiter · worker nginx Ngx_http_lua_api rbtree Ngx_stream_limit_conn_module Ngx_http_limit_conn_module Ngx_stream_limit_req_module http cache ssl Ngx_http_upstream_zone_module

Nginx����

Page 14: Ant-loiter · worker nginx Ngx_http_lua_api rbtree Ngx_stream_limit_conn_module Ngx_http_limit_conn_module Ngx_stream_limit_req_module http cache ssl Ngx_http_upstream_zone_module
Page 15: Ant-loiter · worker nginx Ngx_http_lua_api rbtree Ngx_stream_limit_conn_module Ngx_http_limit_conn_module Ngx_stream_limit_req_module http cache ssl Ngx_http_upstream_zone_module

epoll

+

+

+

+

��Ø

��ØØ

��ØØ /ØØ

Page 16: Ant-loiter · worker nginx Ngx_http_lua_api rbtree Ngx_stream_limit_conn_module Ngx_http_limit_conn_module Ngx_stream_limit_req_module http cache ssl Ngx_http_upstream_zone_module

��

���������

• ����������

• ���������

��������

• ������

• ������������

Page 17: Ant-loiter · worker nginx Ngx_http_lua_api rbtree Ngx_stream_limit_conn_module Ngx_http_limit_conn_module Ngx_stream_limit_req_module http cache ssl Ngx_http_upstream_zone_module

�� �

�accept��

������

Page 18: Ant-loiter · worker nginx Ngx_http_lua_api rbtree Ngx_stream_limit_conn_module Ngx_http_limit_conn_module Ngx_stream_limit_req_module http cache ssl Ngx_http_upstream_zone_module

����

������������

'&�accept

'&�accept

�����"

�����"

�accept

�accept

��EAGAIN

�� ��

� ����'&�

� ����'&�

ACCEPT%���

ACCEPT%����

"EAGAIN$�

��� ���ACCEPT%������!�#�

Page 19: Ant-loiter · worker nginx Ngx_http_lua_api rbtree Ngx_stream_limit_conn_module Ngx_http_limit_conn_module Ngx_stream_limit_req_module http cache ssl Ngx_http_upstream_zone_module

�����������

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 ){

Page 20: Ant-loiter · worker nginx Ngx_http_lua_api rbtree Ngx_stream_limit_conn_module Ngx_http_limit_conn_module Ngx_stream_limit_req_module http cache ssl Ngx_http_upstream_zone_module

Nginx �

�����ginx

������

�����

�����

Page 21: Ant-loiter · worker nginx Ngx_http_lua_api rbtree Ngx_stream_limit_conn_module Ngx_http_limit_conn_module Ngx_stream_limit_req_module http cache ssl Ngx_http_upstream_zone_module

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

��

Page 22: Ant-loiter · worker nginx Ngx_http_lua_api rbtree Ngx_stream_limit_conn_module Ngx_http_limit_conn_module Ngx_stream_limit_req_module http cache ssl Ngx_http_upstream_zone_module

����

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

mail

Page 23: Ant-loiter · worker nginx Ngx_http_lua_api rbtree Ngx_stream_limit_conn_module Ngx_http_limit_conn_module Ngx_stream_limit_req_module http cache ssl Ngx_http_upstream_zone_module

��

: ( ( ( : ( ( ( : ( ( ( : +( ( ( ( : ( + ( : ( ( ( : ( + ( : ( ( (: : ( ( ( : ( ( : ( ( (: : +( ( ( ( : ( ( ( (

) :

() () (( () (( (( ( ( ( ( : ( (

( :+( :

( ) ( :+ ( ( :

() (

(( ( (

( ( ((

) ) ()) () :

() :) (

+ (

: (

d b (

d b (

d b (

(c a

( ( (( ( (

_b

��

�� �����

�� ����

Page 24: Ant-loiter · worker nginx Ngx_http_lua_api rbtree Ngx_stream_limit_conn_module Ngx_http_limit_conn_module Ngx_stream_limit_req_module http cache ssl Ngx_http_upstream_zone_module

����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;

};

����

�����

Page 25: Ant-loiter · worker nginx Ngx_http_lua_api rbtree Ngx_stream_limit_conn_module Ngx_http_limit_conn_module Ngx_stream_limit_req_module http cache ssl Ngx_http_upstream_zone_module

���

�� ����

������

Page 26: Ant-loiter · worker nginx Ngx_http_lua_api rbtree Ngx_stream_limit_conn_module Ngx_http_limit_conn_module Ngx_stream_limit_req_module http cache ssl Ngx_http_upstream_zone_module

Nginx ������

�����

������

• �

• ����

• �

• ������ �

Page 27: Ant-loiter · worker nginx Ngx_http_lua_api rbtree Ngx_stream_limit_conn_module Ngx_http_limit_conn_module Ngx_stream_limit_req_module http cache ssl Ngx_http_upstream_zone_module

������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

Page 28: Ant-loiter · worker nginx Ngx_http_lua_api rbtree Ngx_stream_limit_conn_module Ngx_http_limit_conn_module Ngx_stream_limit_req_module http cache ssl Ngx_http_upstream_zone_module

OpenResty��������

Page 29: Ant-loiter · worker nginx Ngx_http_lua_api rbtree Ngx_stream_limit_conn_module Ngx_http_limit_conn_module Ngx_stream_limit_req_module http cache ssl Ngx_http_upstream_zone_module

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

Page 30: Ant-loiter · worker nginx Ngx_http_lua_api rbtree Ngx_stream_limit_conn_module Ngx_http_limit_conn_module Ngx_stream_limit_req_module http cache ssl Ngx_http_upstream_zone_module

ngx_slab_stat���Slab����

Page 31: Ant-loiter · worker nginx Ngx_http_lua_api rbtree Ngx_stream_limit_conn_module Ngx_http_limit_conn_module Ngx_stream_limit_req_module http cache ssl Ngx_http_upstream_zone_module

Nginx ��

��

���

��

���

Page 32: Ant-loiter · worker nginx Ngx_http_lua_api rbtree Ngx_stream_limit_conn_module Ngx_http_limit_conn_module Ngx_stream_limit_req_module http cache ssl Ngx_http_upstream_zone_module

Nginx ���

Page 33: Ant-loiter · worker nginx Ngx_http_lua_api rbtree Ngx_stream_limit_conn_module Ngx_http_limit_conn_module Ngx_stream_limit_req_module http cache ssl Ngx_http_upstream_zone_module

�����

Bucket size����

Max size

Page 34: Ant-loiter · worker nginx Ngx_http_lua_api rbtree Ngx_stream_limit_conn_module Ngx_http_limit_conn_module Ngx_stream_limit_req_module http cache ssl Ngx_http_upstream_zone_module

���2 7+ 6 + 2 62 7+ 6 + 25 62 7+ 6 + 1 6

3365 26 2 125 6

2 7+ 6 +6

()

8

��

��

��

��

��

��

��

Page 35: Ant-loiter · worker nginx Ngx_http_lua_api rbtree Ngx_stream_limit_conn_module Ngx_http_limit_conn_module Ngx_stream_limit_req_module http cache ssl Ngx_http_upstream_zone_module

���

������2�log(n)

����O(n)

�� �����O(log(n))�������

Page 36: Ant-loiter · worker nginx Ngx_http_lua_api rbtree Ngx_stream_limit_conn_module Ngx_http_limit_conn_module Ngx_stream_limit_req_module http cache ssl Ngx_http_upstream_zone_module

��������

���

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

Page 37: Ant-loiter · worker nginx Ngx_http_lua_api rbtree Ngx_stream_limit_conn_module Ngx_http_limit_conn_module Ngx_stream_limit_req_module http cache ssl Ngx_http_upstream_zone_module

���-�����

load_module modules/ngx_http_image_filter_module.so;

Configure�����

���binary

���������

��load_module��

���� ������

���������