1. 程式人生 > >電商之梳理Nginx(7層)相關知識---反向代理以及負載均衡

電商之梳理Nginx(7層)相關知識---反向代理以及負載均衡

nginx 編輯
Nginx (“engine x”) 是一個高效能的HTTP和反向代理伺服器,也是一個IMAP/POP3/SMTP伺服器。Nginx是由Igor Sysoev為俄羅斯訪問量第二的Rambler.ru站點開發的,第一個公開版本0.1.0釋出於2004年10月4日。其將原始碼以類BSD許可證的形式釋出,因它的穩定性、豐富的功能集、示例配置檔案和低系統資源的消耗而聞名。2011年6月1日,nginx 1.0.4釋出。
Nginx是一款輕量級的Web 伺服器/反向代理伺服器及電子郵件(IMAP/POP3)代理伺服器,並在一個BSD-like 協議下發行。由俄羅斯的程式設計師Igor Sysoev所開發,供俄國大型的入口網站及搜尋引擎Rambler(俄文:Рамблер)使用。其特點是佔有記憶體少,併發能力強,事實上nginx的併發能力確實在同類型的網頁伺服器中表現較好,中國大陸使用nginx網站使用者有:京東、新浪、網易、騰訊、淘寶等。
外文名 Nginx 軟體許可 BSD許可 軟體型別 開源軟體,網頁伺服器軟體 相容性 Linux系統,Windows NT系統
目錄
1 優點
▪ 伺服器
▪ 程式碼
▪ 代理伺服器
2 功能
▪ 支援作業系統
▪ 結構與擴充套件
▪ 實驗特性
▪ HTTP基礎功能
▪ 其他HTTP功能
3 安裝
▪ 模組依賴性
▪ 編譯安裝包
▪ 原始碼下載
4 使用技巧
5 反向代理實踐
6 Nginx Rewrite
7 配置
8 版本釋出
優點編輯
Nginx 可以在大多數 Unix like OS 上編譯執行,並有 Windows 移植版。 Nginx 的1.4.0穩定版已經於2013年4月24日釋出,一般情況下,對於新建站點,建議使用最新穩定版作為生產版本,已有站點的升級急迫性不高。Nginx 的原始碼使用 2-clause BSD-like license。
Nginx 是一個很強大的高效能Web和反向代理伺服器,它具有很多非常優越的特性:
在高連線併發的情況下,Nginx是Apache伺服器不錯的替代品:Nginx在美國是做虛擬主機生意的老闆們經常選擇的軟體平臺之一。能夠支援高達 50,000 個併發連線數的響應,感謝Nginx為我們選擇了 epoll and kqueue作為開發模型。

伺服器
Nginx作為負載均衡伺服器:Nginx 既可以在內部直接支援 Rails 和 PHP 程式對外進行服務,也可以支援作為 HTTP代理伺服器對外進行服務。Nginx採用C進行編寫,不論是系統資源開銷還是CPU使用效率都比 Perlbal 要好很多。
程式碼
Nginx程式碼完全用C語言從頭寫成,已經移植到許多體系結構和作業系統,包括:Linux、FreeBSD、Solaris、Mac OS X、AIX以及Microsoft Windows。Nginx有自己的函式庫,並且除了zlib、PCRE和OpenSSL之外,標準模組只使用系統C庫函式。而且,如果不需要或者考慮到潛在的授權衝突,可以不使用這些第三方庫。
代理伺服器
作為郵件代理伺服器:Nginx 同時也是一個非常優秀的郵件代理伺服器(最早開發這個產品的目的之一也是作為郵件代理伺服器),Last.fm 描述了成功並且美妙的使用經驗。
Nginx 是一個安裝非常的簡單、配置檔案非常簡潔(還能夠支援perl語法)、Bug非常少的伺服器。Nginx 啟動特別容易,並且幾乎可以做到7*24不間斷執行,即使執行數個月也不需要重新啟動。你還能夠不間斷服務的情況下進行軟體版本的升級。
功能編輯
支援作業系統
FreeBSD 3— 10 / i386; FreeBSD 5— 10 / amd64;
Linux 2.2— 4 / i386; Linux 2.6— 4 / amd64; Linux 3— 4 / armv6l, armv7l, aarch64;
Solaris 9 / i386, sun4u; Solaris 10 / i386, amd64, sun4v;
AIX 7.1 / powerpc;
HP-UX 11.31 / ia64;
Mac OS X / ppc, i386;
Windows XP, Windows Server 2003.
結構與擴充套件
一個主程序和多個工作程序。工作程序是單執行緒的,且不需要特殊授權即可執行;
kqueue (FreeBSD 4.1+),epoll (Linux 2.6+),rt signals (Linux 2.2.19+),/dev/poll (Solaris 7 11/99+),select,以及 poll 支援;
kqueue支援的不同功能包括 EV_CLEAR,EV_DISABLE (臨時禁止事件), NOTE_LOWAT,EV_EOF,有效資料的數目,錯誤程式碼;
sendfile (FreeBSD 3.1+),sendfile (Linux 2.2+),sendfile64 (Linux 2.4.21+),和 sendfilev (Solaris 8 7/01+) 支援;
輸入過濾 (FreeBSD 4.1+) 以及 TCP_DEFER_ACCEPT (Linux 2.4+) 支援;
10,000 非活動的 HTTP keep-alive 連線僅需要 2.5M記憶體。
最小化的資料拷貝操作;
其他HTTP功能:
基於IP 和名稱的虛擬主機服務;
Memcached 的 GET 介面;
支援 keep-alive 和管道連線;
靈活簡單的配置;
重新配置和線上升級而無須中斷客戶的工作程序;
可定製的訪問日誌,日誌寫入快取,以及快捷的日誌回捲;
4xx-5xx錯誤程式碼重定向;
基於 PCRE 的 rewrite 重寫模組;
基於客戶端IP 地址和 HTTP 基本認證的訪問控制;
PUT,DELETE,和 MKCOL 方法;
支援 FLV (Flash 視訊);
頻寬限制。
實驗特性
內嵌的 perl;
通過 aio_read()/aio_write() 的套接字工作的實驗模組,僅在 FreeBSD 下;
對執行緒的實驗化支援,FreeBSD 4.x 的實現基於 rfork();
Nginx 主要的英語站點是

http://sysoev. ru/en/;
英語文件草稿由 Aleksandar Lazic 完成 點選。
HTTP基礎功能
處理靜態檔案,索引檔案以及自動索引;
反向代理加速(無快取),簡單的負載均衡和容錯;
FastCGI,簡單的負載均衡和容錯;
模組化的結構。過濾器包括gzipping,byte ranges,chunked responses,以及 SSI-filter。在SSI過濾器中,到同一個 proxy 或者 FastCGI 的多個子請求併發處理;
SSL 和 TLS SNI 支援;
IMAP/POP3代理服務功能:
使用外部 HTTP 認證伺服器重定向使用者到 IMAP/POP3 後端;
使用外部 HTTP 認證伺服器認證使用者後連線重定向到內部的 SMTP 後端;
其他HTTP功能
基於名稱和基於IP的虛擬伺服器;
Keep-alive and pipelined connections support;保持活動和支援管線連線;
Flexible configuration;靈活的配置;
Reconfiguration and online upgrade without interruption of the client processing;過載配置,無間斷程序升級;
Access log formats,bufferred log writing,and quick log rotation;訪問日誌格式,bufferred日誌寫,快速登入旋轉;
3xx-5xx error codes redirection; 3xx的- 5xx錯誤程式碼重定向;
The rewrite module;重寫模組;
Access control based on client IP address and HTTP Basic authentication;基於客戶端IP地址訪問控制和HTTP基本認證;
The PUT,DELETE,MKCOL,COPY and MOVE methods; 提交,刪除,MKCOL,複製和移動方法;
FLV streaming;FLV視訊流;
Speed limitation;速度限制;
Limitation of simultaneous connections or requests from one address.限制同個IP地址請求數量。
Embedded perl.嵌入式的Perl。
郵件代理伺服器功能
使用者重定向到IMAP/POP3後端使用外部HTTP認證伺服器;
User authentication using an external HTTP authentication server and connection redirection to internal SMTP backend;使用者身份驗證使用外部HTTP認證伺服器和連線重定向到內部的SMTP後端;
Authentication methods:驗證方法:
POP3: USER/PASS,APOP,AUTH LOGIN/PLAIN/CRAM-MD5;的POP3:使用者名稱/密碼,的APOP,AUTH的LOGIN/PLAIN/CRAM-MD5;
IMAP: LOGIN,AUTH LOGIN/PLAIN/CRAM-MD5; IMAP的:登入,AUTH的LOGIN/PLAIN/CRAM-MD5;
SMTP: AUTH LOGIN/PLAIN/CRAM-MD5;的SMTP:AUTH的LOGIN/PLAIN/CRAM-MD5;
SSL support; SSL支援;
STARTTLS and STLS support. STARTTLS的和補充的支援。
認證方法
POP3: POP3 USER/PASS,APOP,AUTH LOGIN PLAIN CRAM-MD5;
IMAP: IMAP LOGIN;
SMTP: AUTH LOGIN PLAIN CRAM-MD5;
SSL 支援;
在 IMAP 和 POP3 模式下的 STARTTLS 和 STLS 支援。
安裝編輯
模組依賴性
gzip模組需要 zlib 庫
rewrite模組需要 pcre 庫
ssl 功能需要openssl庫
編譯安裝包
Nginx在一些Linux發行版和BSD的各個變種版本的安裝包倉庫中都會有,通過各個系統自帶的軟體包管理方法即可安裝。需要注意的是,很多預先編譯好的安裝包都比較陳舊,大多數情況下還是推薦直接從原始碼編譯。
原始碼下載
特定平臺的安裝和記錄
Nginx 在 Slackware 上的編譯安裝指令碼
Nginx 在ubuntu和debian上的安裝及指令碼,debian針對nginx包的官方下載站點
使用原始碼進行構建
Nginx 使用 Unix 下常用的 ‘./configure && make && make install’ 過程來編譯安裝。
configure指令碼確定系統所具有一些特性,特別是 nginx 用來處理連線的方法。然後,它建立 Makefile 檔案。
configure 支援下面的選項:
–prefix= - Nginx安裝路徑。如果沒有指定,預設為 /usr/local/nginx。
–sbin-path= - Nginx可執行檔案安裝路徑。只能安裝時指定,如果沒有指定,預設為/sbin/nginx。
–conf-path= - 在沒有給定-c選項下預設的nginx.conf的路徑。如果沒有指定,預設為/conf/nginx.conf。
–pid-path= - 在nginx.conf中沒有指定pid指令的情況下,預設的nginx.pid的路徑。如果沒有指定,預設為 /logs/nginx.pid。
–lock-path= - nginx.lock檔案的路徑。
–error-log-path= - 在nginx.conf中沒有指定error_log指令的情況下,預設的錯誤日誌的路徑。如果沒有指定,預設為 /logs/error.log。
–http-log-path= - 在nginx.conf中沒有指定access_log指令的情況下,預設的訪問日誌的路徑。如果沒有指定,預設為 /logs/access.log。
–user= - 在nginx.conf中沒有指定user指令的情況下,預設的nginx使用的使用者。如果沒有指定,預設為 nobody。
–group= - 在nginx.conf中沒有指定user指令的情況下,預設的nginx使用的組。如果沒有指定,預設為 nobody。
–builddir=DIR - 指定編譯的目錄
–with-rtsig_module - 啟用 rtsig模組
–with-select_module –without-select_module - Whether or not to enable the select module. This module is enabled by default if a more suitable method such as kqueue,epoll,rtsig or /dev/poll is not discovered by configure.
//允許或不允許開啟SELECT模式,如果 configure 沒有找到更合適的模式,比如:kqueue(sun os),epoll (linux kenel 2.6+),rtsig(實時訊號)或者/dev/poll(一種類似select的模式,底層實現與SELECT基本相 同,都是採用輪訓方法) SELECT模式將是預設安裝模式
–with-poll_module –without-poll_module - Whether or not to enable the poll module. This module is enabled by default if a more suitable method such as kqueue,epoll,rtsig or /dev/poll is not discovered by configure.
–with-http_ssl_module - Enable ngx_http_ssl_module. Enables SSL support and the ability to handle HTTPS requests. Requires OpenSSL. On Debian,this is libssl-dev.
//開啟HTTP SSL模組,使NGINX可以支援HTTPS請求。這個模組需要已經安裝了OPENSSL,在DEBIAN上是libssl
–with-http_realip_module - 啟用 ngx_http_realip_module
–with-http_addition_module - 啟用 ngx_http_addition_module
–with-http_sub_module - 啟用 ngx_http_sub_module
–with-http_dav_module - 啟用 ngx_http_dav_module
–with-http_flv_module - 啟用 ngx_http_flv_module
–with-http_stub_status_module - 啟用 “server status” 頁
–without-http_charset_module - 禁用 ngx_http_charset_module
–without-http_gzip_module - 禁用 ngx_http_gzip_module. 如果啟用,需要 zlib。
–without-http_ssi_module - 禁用 ngx_http_ssi_module
–without-http_userid_module - 禁用 ngx_http_userid_module
–without-http_access_module - 禁用 ngx_http_access_module
–without-http_auth_basic_module - 禁用 ngx_http_auth_basic_module
–without-http_autoindex_module - 禁用 ngx_http_autoindex_module
–without-http_geo_module - 禁用 ngx_http_geo_module
–without-http_map_module - 禁用 ngx_http_map_module
–without-http_referer_module - 禁用 ngx_http_referer_module
–without-http_rewrite_module - 禁用 ngx_http_rewrite_module. 如果啟用需要 PCRE。
–without-http_proxy_module - 禁用 ngx_http_proxy_module
–without-http_fastcgi_module - 禁用 ngx_http_fastcgi_module
–without-http_memcached_module - 禁用 ngx_http_memcached_module
–without-http_limit_zone_module - 禁用 ngx_http_limit_zone_module
–without-http_empty_gif_module - 禁用 ngx_http_empty_gif_module
–without-http_browser_module - 禁用 ngx_http_browser_module
–without-http_upstream_ip_hash_module - 禁用 ngx_http_upstream_ip_hash_module
–with-http_perl_module - 啟用 ngx_http_perl_module
–with-perl_modules_path=PATH - 指定 perl模組的路徑
–with-perl=PATH - 指定 perl 執行檔案的路徑
–http-log-path=PATH - Set path to the http access log
–http-client-body-temp-path=PATH - Set path to the http client request body temporary files
–http-proxy-temp-path=PATH - Set path to the http proxy temporary files
–http-fastcgi-temp-path=PATH - Set path to the http fastcgi temporary files
–without-http - 禁用 HTTP server
–with-mail - 啟用 IMAP4/POP3/SMTP 代理模組
–with-mail_ssl_module - 啟用 ngx_mail_ssl_module
–with-cc=PATH - 指定 C編譯器的路徑
–with-cpp=PATH - 指定 C前處理器的路徑
–with-cc-opt=OPTIONS - Additional parameters which will be added to the variable CFLAGS. With the use of the system library PCRE in FreeBSD,it is necessary to indicate –with-cc-opt=”-I /usr/local/include”. If we are using select() and it is necessary to increase the number of file descriptors,then this also can be assigned here: –with-cc-opt=”-D FD_SETSIZE=2048”.
–with-ld-opt=OPTIONS - Additional parameters passed to the linker. With the use of the system library PCRE in FreeBSD,it is necessary to indicate –with-ld-opt=”-L /usr/local/lib”.
–with-cpu-opt=CPU - 為特定的 CPU 編譯,有效的值包括:pentium,pentiumpro,pentium3,pentium4,athlon,opteron,amd64,sparc32,sparc64,ppc64
–without-pcre - 禁止 PCRE 庫的使用。同時也會禁止 HTTP rewrite 模組。在 “location” 配置指令中的正則表示式也需要 PCRE。
–with-pcre=DIR - 指定 PCRE 庫的原始碼的路徑。
–with-pcre-opt=OPTIONS - Set additional options for PCRE building.
–with-md5=DIR - Set path to md5 library sources.
–with-md5-opt=OPTIONS - Set additional options for md5 building.
–with-md5-asm - Use md5 assembler sources.
–with-sha1=DIR - Set path to sha1 library sources.
–with-sha1-opt=OPTIONS - Set additional options for sha1 building.
–with-sha1-asm - Use sha1 assembler sources.
–with-zlib=DIR - Set path to zlib library sources.
–with-zlib-opt=OPTIONS - Set additional options for zlib building.
–with-zlib-asm=CPU - Use zlib assembler sources optimized for specified CPU,valid values are: pentium,pentiumpro
–with-openssl=DIR - Set path to OpenSSL library sources
–with-openssl-opt=OPTIONS - Set additional options for OpenSSL building
–with-debug - 啟用除錯日誌
–add-module=PATH - Add in a third-party module found in directory PATH
在不同版本間,選項可能會有些許變化,請總是使用 ./configure –help 命令來檢查一下當前的選項列表。
使用技巧編輯
對於chroot的支援是否在計劃之中
在什麼情況下使用Nginx比使用squid要好? 反之亦然。
大體上來說nginx主要用於反向加速代理而不是像squid那樣作為常規代理伺服器。Nginx的最大優勢在於高負載情況下記憶體和CPU的低消耗。我不認為squid能給你帶來比nginx更好的效能。
依照 [NginxImapProxyExample] 開始你的配置. 關於不同配置引數的具體資訊,請檢視 [NginxMailCoreModule] 頁。
示例1: 用運行於apache上的php指令碼做後端驗證
示例2: 使用運行於同一個伺服器的 nginx-embedded-perl模組作為 imap/pop代理和認證後端
某些東東不工作
(URL重寫,代理,路徑,…)
例如:如URL重寫(rewrite)不工作了或者是unix的路徑(/$PATH)的問題云云…
請仔細閱讀 [NginxDebugging] 並且 逐行 檢視錯誤日誌。
如果你沒找到錯誤 打起精神 試著到IRC或郵件列表裡說明一下你碰到的問題。
有沒有其它類似的Web伺服器
Cherokee
Lighttpd (Lighty)
thttpd
關於各自的優缺點請使用自己喜歡的搜尋引擎查詢
讓Nginx成為以postfix做為後端的SMTP代理
Nginx使用什麼演算法來實現負載均衡它能實現基於連線數的負載均衡嗎?
Nginx使用簡單的輪巡演算法,所以無法做基本連結計數的負載均衡。這個可能會在將來的版本中有所改變。
我能關閉從代理伺服器到後端伺服器的快取嗎或者使用上傳進度特性?
反向代理實踐編輯
. nginx + substitutions 安裝
  nginx 自帶一個Substitution模組,但該模組只能寫一行,所以我們改用 substitutions
  下面是安裝一些預備軟體
yum -y –noplugins install wget zip
yum -y –noplugins install unzip
yum -y –noplugins install gcc
yum -y –noplugins install make
yum -y –noplugins install pcre-devel
yum -y –noplugins install openssl-devel
編譯軟體
tar zxf nginx-1.0.8.tar.gz
cd nginx-1.0.8
./configure ./configure –add-module=path/substitutions4nginx-read-only //注意這裡的path是相對應的真實路徑
make
make install
配置 nginx.conf
  此時,nginx應該安裝在於 /usr/local/nginx 下面
server_name servername;
location / {
subs_filter ca-pub-**
ca-pub-**; //把google ad 的使用者號 ca-pub-9805743306566114 改成你自己的,比如 ca-pub-****
subs_filter ** **; //把google ad 的廣告號 **改成你自己的,比如 **,你懂的!:D
proxy_pass 這裡是填寫需要代理的網址; //反向代理站。
index index.html index.htm;
}
記住subs_filter 命令的格式即可隨意發揮想象力替換你想替換的廣告、超連結,等等。

  
基於太多人詢問下面的問題:
我能為了得到上傳進度而關閉代理的快取嗎
使用nginx我怎麼才能給使用者顯示上傳進度
到目前為止 (2007-Apr-26) 還沒有辦法關閉到後端伺服器的快取.
Nginx Rewrite編輯
1.Nginx Rewrite 基本標記(flags)複製內容到剪貼簿程式碼:last – 基本上都用這個Flag[1] 。
break – 中止Rewirte,不在繼續匹配
redirect – 返回臨時重定向的HTTP狀態302
permanent – 返回永久重定向的HTTP狀態301
2. 正則表示式匹配,其中:程式碼:
CODE:
~ 為區分大小寫匹配
~* 為不區分大小寫匹配
!~和!~*分別為區分大小寫不匹配及不區分大小寫不匹配
3. 檔案及目錄匹配,其中:程式碼:
CODE:
-f和!-f用來判斷是否存在檔案
-d和!-d用來判斷是否存在目錄
-e和!-e用來判斷是否存在檔案或目錄
-x和!-x用來判斷檔案是否可執行
4.Nginx 的一些可用的全域性變數,可用做條件判斷:
程式碼:
CODE:
argscontent_length
contenttypedocument_root
documenturihost
httpuseragenthttp_cookie
limitraterequest_body_file
requestmethodremote_addr
remoteportremote_user
requestfilenamerequest_uri
querystringscheme
serverprotocolserver_addr
servernameserver_port
$uri
配置編輯
cls
@ECHO OFF
SET NGINX_PATH=D:
SET NGINX_DIR=D:\nginx-1.3.6\
color 0a
TITLE Nginx 管理程式 Power By ChenJianxiang
GOTO MENU
:MENU
CLS
ECHO.
ECHO. * * * * Nginx 管理程式 Power By ChenJianxiang * * *
ECHO. * *
ECHO. * 1 啟動Nginx *
ECHO. * *
ECHO. * 2 關閉Nginx *
ECHO. * *
ECHO. * 3 重啟Nginx *
ECHO. * *
ECHO. * 4 退 出 *
ECHO. * *
ECHO. * * * * * * * * * * * * * * * * * * * * * * * *
ECHO.
ECHO.請輸入選擇專案的序號:
set /p ID=
IF “%id%”==”1” GOTO cmd1
IF “%id%”==”2” GOTO cmd2
IF “%id%”==”3” GOTO cmd3
IF “%id%”==”4” EXIT
PAUSE
:cmd1
ECHO.
ECHO.啟動Nginx……
IF NOT EXIST %NGINX_DIR%nginx.exe ECHO %NGINX_DIR%nginx.exe不存在
%NGINX_PATH%
cd %NGINX_DIR%
IF EXIST %NGINX_DIR%nginx.exe start %NGINX_DIR%nginx.exe
ECHO.OK
PAUSE
GOTO MENU
:cmd2
ECHO.
ECHO.關閉Nginx……
taskkill /F /IM nginx.exe > nul
ECHO.OK
PAUSE
GOTO MENU
:cmd3
ECHO.
ECHO.關閉Nginx……
taskkill /F /IM nginx.exe > nul
ECHO.OK
GOTO cmd1
GOTO MENU
版本釋出編輯
2012年08月22日,Nginx 1.3.5 開發版釋出。
2012年09月12日,Nginx 1.3.6 開發版釋出。
2012年12月11日,Nginx 1.2.6 穩定版釋出
2013年02月12日,Nginx 1.2.7 穩定版釋出
2013年02月19日,Nginx 1.3.13 開發版釋出
2013年04月03日,Nginx 1.2.8 穩定版釋出。
2013年04月16日,Nginx 1.3.16 開發者版本釋出。
2013年04月24日,Nginx 1.4.0 穩定版釋出。
2013年05月07日,Nginx 1.4.1 穩定版釋出。
2013年05月07日,Nginx 1.5.0 開發版本釋出。
2013年06月04日,Nginx 1.5.1 主版本釋出。
2013年07月02日,Nginx 1.5.2 開發版釋出。
2013年07月17日,Nginx 1.4.2 穩定版釋出。
2013年07月30日,Nginx 1.5.3 開發版本釋出。
2013年08月27日,Nginx 1.5.4 開發版本釋出。
2013年09月17日,Nginx 1.5.5 開發版本釋出。[1-2]
2015年10月27日,Nginx 1.9.6 主線版本釋出。
2016年02月09日,Nginx 1.9.11開發版本釋出。[3]