Nginx 一點一滴 03 - 架構機制

Nginx服務器架構

模塊化結構

Nginx 服務器的開發徹底遵循模塊化設計思想nginx

什麼是模塊化開發?

  1. 單一職責原則,一個模塊只負責一個功能web

  2. 將程序分解,自頂向下,逐步求精正則表達式

  3. 高內聚,低耦合數據庫

Nginx的模塊化結構

  • 核心模塊: Nginx最基本最核心的服務,如進程管理、權限控制、日誌記錄;json

  • 標準HTTP模塊: Nginx服務器的標準HTTP功能;服務器

  • 可選HTTP模塊: 處理特殊的HTTP請求多線程

  • 郵件服務模塊: 郵件服務架構

  • 第三方模塊: 做爲擴展,完成特殊功能負載均衡

Nginx的模塊清單

  • 核心模塊dom

    • ngx_core

    • ngx_errlog

    • ngx_conf

    • ngx_events

    • ngxeventcore

    • ngx_epll

    • ngx_regex

  • 標準HTTP模塊

    • ngx_http

    • ngxhttpcore #配置端口,URI分析,服務器相應錯誤處理,別名控制(alias)等

    • ngxhttplog #自定義access日誌

    • ngxhttpupstream #定義一組服務器,能夠接受來自proxy, Fastcgi,Memcache的重定向;主要用做負載均衡

    • ngxhttpstatic

    • ngxhttpautoindex #自動生成目錄列表

    • ngxhttpindex #處理以/結尾的請求,若是沒有找到index頁,則看是否開啓了random_index;如開啓,則用之,不然用autoindex

    • ngxhttpauthbasic #基於http的身份認證(authbasic)

    • ngxhttpaccess #基於IP地址的訪問控制(deny,allow)

    • ngxhttplimit_conn #限制來自客戶端的鏈接的響應和處理速率

    • ngxhttplimit_req #限制來自客戶端的請求的響應和處理速率

    • ngxhttpgeo

    • ngxhttpmap #建立任意的鍵值對變量

    • ngxhttpsplit_clients

    • ngxhttpreferer #過濾HTTP頭中Referer爲空的對象

    • ngxhttprewrite #經過正則表達式重定向請求

    • ngxhttpproxy

    • ngxhttpfastcgi #支持fastcgi

    • ngxhttpuwsgi

    • ngxhttpscgi

    • ngxhttpmemcached

    • ngxhttpempty_gif #從內存建立一個1×1的透明gif圖片,能夠快速調用

    • ngxhttpbrowser #解析http請求頭部的User-Agent 值

    • ngxhttpcharset #指定網頁編碼

    • ngxhttpupstreamiphash

    • ngxhttpupstreamleastconn

    • ngxhttpupstream_keepalive

    • ngxhttpwrite_filter

    • ngxhttpheader_filter

    • ngxhttpchunked_filter

    • ngxhttprange_header

    • ngxhttpgzip_filter

    • ngxhttppostpone_filter

    • ngxhttpssi_filter

    • ngxhttpcharset_filter

    • ngxhttpuserid_filter

    • ngxhttpheaders_filter #設置http響應頭

    • ngxhttpcopy_filter

    • ngxhttprangebodyfilter

    • ngxhttpnotmodifiedfilter

  • 可選HTTP模塊

    • ngxhttpaddition #在響應請求的頁面開始或者結尾添加文本信息

    • ngxhttpdegradation #在低內存的狀況下容許服務器返回444或者204錯誤

    • ngxhttpperl

    • ngxhttpflv #支持將Flash多媒體信息按照流文件傳輸,能夠根據客戶端指定的開始位置返回Flash

    • ngxhttpgeoip #支持解析基於GeoIP數據庫的客戶端請求

    • ngxgoogleperftools

    • ngxhttpgzip #gzip壓縮請求的響應

    • ngxhttpgzip_static #搜索並使用預壓縮的以.gz爲後綴的文件代替通常文件響應客戶端請求

    • ngxhttpimage_filter #支持改變png,jpeg,gif圖片的尺寸和旋轉方向

    • ngxhttpmp4 #支持.mp4,.m4v,.m4a等多媒體信息按照流文件傳輸,常與ngxhttpflv一塊兒使用

    • ngxhttprandom_index #當收到/結尾的請求時,在指定目錄下隨機選擇一個文件做爲index

    • ngxhttpsecure_link #支持對請求連接的有效性檢查

    • ngxhttpssl #支持https

    • ngxhttpstub_status

    • ngxhttpsub_module #使用指定的字符串替換響應中的信息

    • ngxhttpdav #支持HTTP和WebDAV協議中的PUT/DELETE/MKCOL/COPY/MOVE方法

    • ngxhttpxslt #將XML響應信息使用XSLT進行轉換

  • 郵件服務模塊

    • ngxmailcore

    • ngxmailpop3

    • ngxmailimap

    • ngxmailsmtp

    • ngxmailauth_http

    • ngxmailproxy

    • ngxmailssl

  • 第三方模塊

    • echo-nginx-module #支持在nginx配置文件中使用echo/sleep/time/exec等類Shell命令

    • memc-nginx-module

    • rds-json-nginx-module #使nginx支持json數據的處理

    • lua-nginx-module

Nginx的web請求處理機制

做爲服務器軟件,必須具有並行處理多個客戶端的請求的能力, 工做方式主要如下3種:

  • 多進程(Apache)

    • 優勢: 設計和實現簡單;子進程獨立

    • 缺點: 生成一個子進程要內存複製, 在資源和時間上形成額外開銷

  • 多線程(IIS)

    • 優勢: 開銷小

    • 缺點: 開發者本身要對內存進行管理;線程之間會相互影響

  • 異步方式(Nginx)

常常說道異步非阻塞這個概念, 包含兩層含義:

通訊模式:

  • 同步: 發送方發送完請求後,等待並接受對方的迴應後,再發送下個請求

  • 異步: 發送方發送完請求後,沒必要等待,直接發送下個請求

Nginx的事件驅動模型

相關文章
相關標籤/搜索