SlideShare a Scribd company logo
1 of 10
Download to read offline
Nginx中upstream的设计与实现
           (一)


   simohayha.bobo@gmail.com
    http://www.pagefault.info
            2012/1/5
大纲

upstream的简介

upstream模块相关hook

upstream的no buffering与buffering

upstream的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

More Related Content

Viewers also liked

WSO2 IoTS Device Manufacturer Guide
WSO2 IoTS Device Manufacturer GuideWSO2 IoTS Device Manufacturer Guide
WSO2 IoTS Device Manufacturer Guidehugo lu
 
introduction to linux kernel tcp/ip ptocotol stack
introduction to linux kernel tcp/ip ptocotol stack introduction to linux kernel tcp/ip ptocotol stack
introduction to linux kernel tcp/ip ptocotol stack monad bobo
 
The linux networking architecture
The linux networking architectureThe linux networking architecture
The linux networking architecturehugo lu
 
The TCP/IP Stack in the Linux Kernel
The TCP/IP Stack in the Linux KernelThe TCP/IP Stack in the Linux Kernel
The TCP/IP Stack in the Linux KernelDivye Kapoor
 
Linux architecture
Linux architectureLinux architecture
Linux architecturemcganesh
 
Linux Networking Explained
Linux Networking ExplainedLinux Networking Explained
Linux Networking ExplainedThomas Graf
 
LinuxCon 2015 Linux Kernel Networking Walkthrough
LinuxCon 2015 Linux Kernel Networking WalkthroughLinuxCon 2015 Linux Kernel Networking Walkthrough
LinuxCon 2015 Linux Kernel Networking WalkthroughThomas Graf
 

Viewers also liked (9)

WSO2 IoTS Device Manufacturer Guide
WSO2 IoTS Device Manufacturer GuideWSO2 IoTS Device Manufacturer Guide
WSO2 IoTS Device Manufacturer Guide
 
introduction to linux kernel tcp/ip ptocotol stack
introduction to linux kernel tcp/ip ptocotol stack introduction to linux kernel tcp/ip ptocotol stack
introduction to linux kernel tcp/ip ptocotol stack
 
Linux kernel architecture
Linux kernel architectureLinux kernel architecture
Linux kernel architecture
 
The linux networking architecture
The linux networking architectureThe linux networking architecture
The linux networking architecture
 
The TCP/IP Stack in the Linux Kernel
The TCP/IP Stack in the Linux KernelThe TCP/IP Stack in the Linux Kernel
The TCP/IP Stack in the Linux Kernel
 
Linux architecture
Linux architectureLinux architecture
Linux architecture
 
Architecture Of The Linux Kernel
Architecture Of The Linux KernelArchitecture Of The Linux Kernel
Architecture Of The Linux Kernel
 
Linux Networking Explained
Linux Networking ExplainedLinux Networking Explained
Linux Networking Explained
 
LinuxCon 2015 Linux Kernel Networking Walkthrough
LinuxCon 2015 Linux Kernel Networking WalkthroughLinuxCon 2015 Linux Kernel Networking Walkthrough
LinuxCon 2015 Linux Kernel Networking Walkthrough
 

Upstream design and_implementation_in_nginx

  • 1. Nginx中upstream的设计与实现 (一) simohayha.bobo@gmail.com http://www.pagefault.info 2012/1/5
  • 3. Upstream的简介 ● 什么是upstream ○ upstream是Nginx和后端进行通信的一种机制 ○ Nginx作为一个桥梁 ○ content handle ● 应用场景 ○ 需要后端产生内容 ■ memcache/fastcgi... ○ 协议 ■ 一发一收 ■ 类HTTP ■ 控制信息都包含在header中,body就是纯粹数据. ○  单一server类型
  • 4. 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
  • 5. upstream的no buffering与buffering ● 区别主要是指从后端接收数据,然后发送给client的过程. ● 相同点 ○ 都需要接收并解析header完毕之后,才会进入发送流程. ● no buffering ○ 尽量的将从后端接收到的body立即传递给client. ● buffering ○ 会将从后端接收到的body,尽量缓存起来,然后再发送.   ● subrequest_in_memory ○ body太大导致出错 ○ ngx_http_upstream_process_body_in_memory
  • 6. 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(回调设置)
  • 7.                                                   upstream的no buffering接收 示意图
  • 8.                                                    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 ■ 共用数据  
  • 9.                                                    upstream的no buffering发送 ● 发送数据 ○ ngx_http_upstream_process_non_buffered_request ■ 读写回调都会调用(参数控制) ■ do_write ■ 有读到数据,就发送 ■ body只是纯粹数据 ○ 对于body没有任何拷贝 ○ 结束标记 ■ upstream->read->eof ■ upstream->read->error ■ u->length  ○ ○