Upstream design and_implementation_in_nginx
Upcoming SlideShare
Loading in...5
×
 

Upstream design and_implementation_in_nginx

on

  • 11,619 views

 

Statistics

Views

Total Views
11,619
Views on SlideShare
2,081
Embed Views
9,538

Actions

Likes
11
Downloads
78
Comments
1

9 Embeds 9,538

http://www.pagefault.info 9483
http://xianguo.com 34
http://www.zhuaxia.com 8
http://localhost 7
http://www.sogou.com 2
http://www.pagefault.info&_=1326206442046 HTTP 1
http://www.pagefault.info.sixxs.org 1
http://webcache.googleusercontent.com 1
http://www.linkedin.com 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

11 of 1

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Upstream design and_implementation_in_nginx Upstream design and_implementation_in_nginx Presentation Transcript

    • Nginx中upstream的设计与实现 (一) simohayha.bobo@gmail.com http://www.pagefault.info 2012/1/5
    • 大纲upstream的简介upstream模块相关hookupstream的no buffering与bufferingupstream的no buffering接收upstream的no buffering发送资源
    • Upstream的简介● 什么是upstream ○ upstream是Nginx和后端进行通信的一种机制 ○ Nginx作为一个桥梁 ○ content handle● 应用场景 ○ 需要后端产生内容 ■ memcache/fastcgi... ○ 协议 ■ 一发一收 ■ 类HTTP ■ 控制信息都包含在header中,body就是纯粹数据. ○  单一server类型
    • upstream模块相关hook● 编写upstream模块 ○ 回调函数 ■ create_reqeustreinit_request ■ 创建重新初始化请求 ■ process_header ■ 处理upstream过来的头 ■ finalize_request  ■ 对request进行清理 ■ input_filter/input_filter_init/(upstream) ■ 用于non buffering ■ 将读取的u->buffer拷贝到u->out_bufs  ■ input_filter/input_filter_init(event_pipe) ■ 用于buffering
    • upstream的no buffering与buffering ● 区别主要是指从后端接收数据,然后发送给client的过程. ● 相同点 ○ 都需要接收并解析header完毕之后,才会进入发送流程. ● no buffering ○ 尽量的将从后端接收到的body立即传递给client. ● buffering ○ 会将从后端接收到的body,尽量缓存起来,然后再发送.  ● subrequest_in_memory ○ body太大导致出错 ○ ngx_http_upstream_process_body_in_memory
    • upstream的no buffering header的接收● ngx_http_upstream_process_header ● xxx_buffer_size 设置读取的header大小. ○ 超过限制,直接出错.● 流式处理 ○ 接收多少处理多少(u->process_header).● 模块控制header是否结束● 处理客户端错误以及后端错误 ○ xxx_intercept_errors(默认0) ○ xxx_ignore_client_abort(默认0)● header的处理 ○ u->headers_in(回调设置)
    •                                                   upstream的no buffering接收示意图
    •                                                    upstream的no buffering发送● ngx_http_upstream_send_response ○ 设置r->headers_out  ○ 设置u->out_bufs ○ 发送数据● 发送长度 ○ u->length(u->headers_in.content_len)● buffer相关 ○ 发送buf(u->out_bufs) ○ 接收buf(u->buffer) ○ buf重用(u->free_bufs) ○ 接收buf与发送buf的连接在u->input_filter ■ 共用数据  
    •                                                    upstream的no buffering发送● 发送数据 ○ ngx_http_upstream_process_non_buffered_request ■ 读写回调都会调用(参数控制) ■ do_write ■ 有读到数据,就发送 ■ body只是纯粹数据 ○ 对于body没有任何拷贝 ○ 结束标记 ■ upstream->read->eof ■ upstream->read->error ■ u->length  ○ ○ 
    • 资源● www.nginx.org● www.pagefault.info● wiki.nginx.org