简单了解一下Nginx 架构

135 阅读1分钟

一、核心架构

1. 进程模型

graph TB
    subgraph Master进程
        M[Master Process]
    end
    
    subgraph Worker进程
        W1[Worker 1]
        W2[Worker 2]
        W3[Worker 3]
    end
    
    subgraph Cache进程
        C1[Cache Loader]
        C2[Cache Manager]
    end
    
    M --> W1
    M --> W2
    M --> W3
    M --> C1
    M --> C2
    
    classDef master fill:#f96,stroke:#333
    classDef worker fill:#9cf,stroke:#333
    classDef cache fill:#fcf,stroke:#333
    
    class M master
    class W1,W2,W3 worker
    class C1,C2 cache

2. 核心组件

以下是 Nginx 的大致完整核心类结构,仅供参考,与实际类图不是完全一致。

主要包含:

  • 请求处理相关的核心类 (ngx_http_request_t)
  • 连接管理相关类 (ngx_connection_t, ngx_event_t)
  • HTTP/2 相关类 (ngx_http2_stream_t, ngx_http2_frame_t)
  • 上游代理相关类 (ngx_http_upstream_t, ngx_peer_connection_t)
classDiagram
    class ngx_http_request_t {
        fill:#e6f3ff
        +ngx_connection_t *connection
        +ngx_http_upstream_t *upstream
        +ngx_buf_t *response_body
        +ngx_http_core_module *core_module
        +ngx_array_t *headers_in
        +ngx_array_t *headers_out
        +ngx_str_t method
        +ngx_str_t uri
        +ngx_str_t request_body
        +ngx_int_t status
        +ngx_str_t response_header
        +ngx_http2_stream_t *http2_stream
        +process_request()
        +send_response()
    }
    
    note for ngx_http_request_t "处理 HTTP 请求的核心类
    - 管理请求的整个生命周期
    - 包含请求头、URI、请求体等信息
    - 支持 HTTP/1 和 HTTP/2"
    
    class ngx_connection_t {
        +int fd
        +ngx_sockaddr_t *sockaddr
        +ngx_event_t *read_event
        +ngx_event_t *write_event
        +read_data()
        +write_data()
        +void *data
    }
    
    note for ngx_connection_t "表示客户端连接
    - 管理套接字和事件
    - 处理数据读写"
    
    class ngx_http_upstream_t {
        +ngx_peer_connection_t *peer
        +ngx_http_request_t *request
        +ngx_str_t upstream_uri
        +ngx_int_t connect_timeout
        +ngx_int_t read_timeout
        +send_request()
        +receive_response()
    }

    note for ngx_http_upstream_t "处理与上游服务器的交互
    - 管理代理连接
    - 处理超时和重试"
    
    class ngx_event_t {
        +int event_type
        +ngx_connection_t *connection
        +handle_event()
    }
    
    class ngx_buf_t {
        +char *data
        +size_t length
        +size_t capacity
        +append_data()
    }
    
    note for ngx_buf_t "缓冲区管理
    - 处理请求和响应数据
    - 支持内存和文件映射"
    
    class ngx_peer_connection_t {
        +ngx_str_t host
        +int port
        +int connection_fd
        +connect_to_peer()
    }
    
    note for ngx_peer_connection_t "上游连接管理
    - 维护与上游服务器的连接
    - 处理连接建立和断开"
    
    class ngx_http2_stream_t {
        +int stream_id
        +ngx_http_request_t *request
        +ngx_http2_frame_t *frame
        +handle_frame()
        +send_data()
    }
    
    note for ngx_http2_stream_t "HTTP/2 流管理
    - 处理 HTTP/2 多路复用
    - 管理流的生命周期"
    
    class ngx_http2_frame_t {
        +int type
        +int length
        +char *data
        +parse_frame()
    }
    
    note for ngx_http2_frame_t "HTTP/2 帧处理
    - 解析和生成 HTTP/2 帧
    - 支持各种帧类型"
    
    ngx_http_request_t --> ngx_connection_t
    ngx_http_request_t --> ngx_http_upstream_t
    ngx_http_request_t --> ngx_buf_t
    ngx_http_request_t --> ngx_http2_stream_t
    ngx_http2_stream_t --> ngx_http2_frame_t
    ngx_connection_t --> ngx_event_t
    ngx_connection_t --> ngx_peer_connection_t
    ngx_http_upstream_t --> ngx_peer_connection_t
    

二、请求处理流程

1. HTTP/1.1 请求流程

sequenceDiagram
    participant Client
    participant Worker
    participant Request
    participant Upstream
    
    Client->>Worker: 发送 HTTP/1.1 请求
    Worker->>Request: 创建 ngx_http_request_t
    Request->>Upstream: 创建 upstream 连接
    Upstream->>Request: 返回响应
    Request->>Worker: 处理响应
    Worker->>Client: 发送响应

2. HTTP/2 请求流程

sequenceDiagram
    participant Client
    participant Worker
    participant Stream
    participant Request
    participant Upstream
    
    Client->>Worker: 发送 HTTP/2 请求
    Worker->>Stream: 创建 HTTP/2 流
    Stream->>Request: 创建 ngx_http_request_t
    Request->>Upstream: 创建 upstream 连接
    Upstream->>Request: 返回响应
    Request->>Stream: 处理响应
    Stream->>Client: 通过流发送响应

三、模块化架构

graph TB
    subgraph Core[核心模块]
        Event[事件模块]
        HTTP[HTTP模块]
        Stream[Stream模块]
    end
    
    subgraph Handlers[处理器]
        Static[静态文件]
        Proxy[代理]
        FastCGI[FastCGI]
    end
    
    subgraph Filters[过滤器]
        Headers[头部过滤]
        Gzip[压缩]
        SSL[SSL]
    end
    
    Core --> Handlers
    Core --> Filters

四、性能优化机制

1. 事件处理

graph LR
    subgraph EventLoop[事件循环]
        Epoll[epoll/kqueue]
        Events[事件队列]
        Handler[事件处理器]
    end
    
    subgraph Connection[连接管理]
        Pool[连接池]
        Cache[缓存]
    end
    
    Epoll --> Events
    Events --> Handler
    Handler --> Pool

2. 缓存系统

graph TB
    subgraph Cache[缓存系统]
        Manager[缓存管理器]
        Memory[内存缓存]
        Disk[磁盘缓存]
    end
    
    subgraph Policy[缓存策略]
        LRU[LRU]
        TTL[TTL]
    end
    
    Manager --> Memory
    Manager --> Disk
    Policy --> Manager