什麼是負載均衡php
咱們知道單臺服務器的性能是有上限的,當流量很大時,就須要使用多臺服務器來共同提供服務,這就是所謂的集羣。html
負載均衡服務器,就是用來把通過它的流量,按照某種方法,分配到集羣中的各臺服務器上。這樣一來不只能夠承擔java
更大的流量、下降服務的延遲,還能夠避免單點故障形成服務不可用。通常的反向代理服務器,都具有負載均衡的功能。linux
負載均衡功能能夠由硬件來提供,好比之前的F5設備。也能夠由軟件來提供,LVS能夠提供四層的負載均衡(利用IP和端口),nginx
Haproxy和Nginx能夠提供七層的負載均衡(利用應用層信息)。程序員
來看一個最簡單的Nginx負載均衡配置。web
經過上述配置,Nginx會做爲HTTP反向代理,把訪問本機的HTTP請求,均分到後端集羣的3臺服務器上。正則表達式
此時使用的HTTP反向代理模塊是ngx_http_proxy_module。算法
通常在upstream配置塊中要指明使用的負載均衡算法,好比hash、ip_hash、least_conn。ubuntu
這裏沒有指定,因此使用了默認的HTTP負載均衡算法 - 加權輪詢。
負載均衡流程圖
在描述負載均衡模塊的具體實現前,先來看下它的大體流程:
負載均衡模塊
Nginx目前提供的負載均衡模塊:
ngx_http_upstream_round_robin,加權輪詢,可均分請求,是默認的HTTP負載均衡算法,集成在框架中。
ngx_http_upstream_ip_hash_module,IP哈希,可保持會話。
ngx_http_upstream_least_conn_module,最少鏈接數,可均分鏈接。
ngx_http_upstream_hash_module,一致性哈希,可減小緩存數據的失效。
以上負載均衡模塊的實現,基本上都遵循一套類似的流程。
1. 指令的解析函數
好比least_conn、ip_hash、hash指令的解析函數。
這些函數在解析配置文件時調用,主要用於:
檢查指令參數的合法性
指定peer.init_upstream函數指針的值,此函數用於初始化upstream塊。
2. 初始化upstream塊
在執行完指令的解析函數後,緊接着會調用全部HTTP模塊的init main conf函數。
在執行ngx_http_upstream_module的init main conf函數時,會調用全部upstream塊的初始化函數,
即在第一步中指定的peer.init_upstream,主要用於:
建立和初始化後端集羣,保存該upstream塊的數據
指定peer.init,此函數用於初始化請求的負載均衡數據
來看下ngx_http_upstream_module。
3. 初始化請求的負載均衡數據塊
當收到一個請求後,通常使用的反向代理模塊(upstream模塊)爲ngx_http_proxy_module,
其NGX_HTTP_CONTENT_PHASE階段的處理函數爲ngx_http_proxy_handler,在初始化upstream機制的
函數ngx_http_upstream_init_request中,調用在第二步中指定的peer.init,主要用於:
建立和初始化該請求的負載均衡數據塊
指定r->upstream->peer.get,用於從集羣中選取一臺後端服務器(這是咱們最爲關心的)
指定r->upstream->peer.free,當不用該後端時,進行數據的更新(無論成功或失敗都調用)
請求的負載均衡數據塊中,通常會有一個成員指向對應upstream塊的數據,除此以外還會有本身獨有的成員。
"The peer initialization function is called once per request.
It sets up a data structure that the module will use as it tries to find an appropriate
backend server to service that request; this structure is persistent across backend re-tries,
so it's a convenient place to keep track of the number of connection failures, or a computed
hash value. By convention, this struct is called ngx_http_upstream_<module_name>_peer_data_t."
4. 選取一臺後端服務器
通常upstream塊中會有多臺後端,那麼對於本次請求,要選定哪一臺後端呢?
這時候第三步中r->upstream->peer.get指向的函數就派上用場了:
採用特定的算法,好比加權輪詢或一致性哈希,從集羣中選出一臺後端,處理本次請求。
選定後端的地址保存在pc->sockaddr,pc爲主動鏈接。
函數的返回值:
NGX_DONE:選定一個後端,和該後端的鏈接已經創建。以後會直接發送請求。
NGX_OK:選定一個後端,和該後端的鏈接還沒有創建。以後會和後端創建鏈接。
NGX_BUSY:全部的後端(包括備份集羣)都不可用。以後會給客戶端發送502(Bad Gateway)。
5. 釋放一臺後端服務器
當再也不使用一臺後端時,須要進行收尾處理,好比統計失敗的次數。
這時候會調用第三步中r->upstream->peer.free指向的函數。
函數參數state的取值:
0,請求被成功處理
NGX_PEER_FAILED,鏈接失敗
NGX_PEER_NEXT,鏈接失敗,或者鏈接成功但後端未能成功處理請求
一個請求容許嘗試的後端數爲pc->tries,在第三步中指定。當state爲後兩個值時:
若是pc->tries不爲0,須要從新選取一個後端,繼續嘗試,此後會重複調用r->upstream->peer.get。
若是pc->tries爲0,便再也不嘗試,給客戶端返回502錯誤碼(Bad Gateway)。
在upstream模塊的回調
負載均衡模塊的功能是從後端集羣中選取一臺後端服務器,而具體的反向代理功能是由upstream模塊實現的,
好比和後端服務器創建鏈接、向後端服務器發送請求、處理後端服務器的響應等。
咱們來看下負載均衡模塊提供的幾個鉤子函數,是在upstream模塊的什麼地方回調的。
Nginx的HTTP反向代理模塊爲ngx_http_proxy_module,其NGX_HTTP_CONTENT_PHASE階段的處理函數爲
ngx_http_proxy_handler,每一個請求的upstream機制是從這裏開始的。
選定後端以後,在和後端通訊的過程當中若是發生了錯誤,會調用ngx_http_upstream_next來繼續嘗試其它的後端。
Reference
[1]. http://www.evanmiller.org/nginx-modules-guide.html#proxying
[2]. http://tengine.taobao.org/book/chapter_05.html#id5
---------------------
下載nginx源碼包,編譯nginx須要指定pcre,zlib,openssl,到官網上下載源代碼包:
http://www.zlib.NET/
http://www.openssl.org/
http://www.pcre.org/
將這三個包下載放到/opt目錄,tar -xzvf *.gz解壓,而後也將nginx-0.6.32的包解壓到/opt目錄下,進入nginx目錄,執行:
#./configure --sbin-path=/usr/local/sbin --with-http_ssl_module --with-pcre=../pcre-7.7 --with-zlib=../zlib-1.2.3 --with-openssl=../openssl-0.9.8g
#make && make install
若是在./configure時出如今錯誤,多是沒有安裝gcc g+編譯器的緣故。單獨安裝這兩個編譯器比較麻煩,ubuntu提供了build-essential來安裝gcc和g++編譯器
apt-get install build-essential
採用aptitude安裝的時候,系統會將全部依賴的包都一併裝上,可是使 用源碼編譯的時候就沒這麼智能了,咱們須要找到須要的依賴包,而後手工安裝,幸運的是,這並不複雜,也很少,例如pcre, ssl 和zlib,安裝方法比較簡單:
sudo aptitude install libpcre3 libpcre3-dev libpcrecpp0 libssl-dev zlib1g-dev
ok,準備工做作完了,是時候開始下載、 安裝Nginx了。
一、下載源代碼
到Nignx官網去下載源代碼
二、 解壓
tar -zxvf nginx-0.6.31.tar.gz
...
cd nginx-0.6.31/
三、選擇須要的編譯選項
Nginx的編譯選項仍是很多 的,基本上都是定製位置和模塊,分別是:
1)--sbin-path=/usr/local/sbin
從源碼編譯Nginx會安裝在/usr/local/nginx目錄下(你可使用參數--prefix=<path>指定本身須要的位置),而後會將bin文件放在/usr/local/nginx/sbin/nginx,雖然比較清晰,可是和咱們通常的習慣不一樣
(做爲程序員的咱們更習慣在/usr/local /sbin下尋找bin文件);
2)--with-http_ssl_module
主要是提供https訪問支持的。
五、 Compile
./configure --sbin-path=/usr/local/sbin --with-http_ssl_module
編 譯的時間不會很長,完成的時候你會在屏幕上看到相似下面的信息:
...
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/sbin"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
...
留 意上面的提示,涉及到nginx的幾個重要文件的路徑信息。
六、Make && make install
差很少快完成了,如今make一下。
make
sudo make install
在使用make install 命令時可能會提示當前執行的命令權限不夠,sudo su命令而後輸入密碼提高下權限就ok
七、chmod(可選)
還須要給bin文件有執行權限,也比較簡單:
chmod +x /usr/local/sbin/nginx
八、Nginx重要文件安裝目錄
默認nginx安裝的目錄在/usr/local/nginx下,包括:
/usr/local/nginx/sbin #nginx啓動文件
/usr/local/nginx/conf #配置文件
/usr/local/nginx/html #默認網頁文件
/usr/local/nginx/logs #日誌文件
安裝完成後,試着使用下。
一、啓動
sudo /usr/local/sbin/nginx
而後把本身的瀏覽器導航到http://IP就可 以看到默認的歡迎界面了
If you see this page, the nginx web server is successfully installed andworking. Further configuration is required.
For online documentation and support please refer tonginx.org.
Commercial support is available atnginx.com.
Thank you for using nginx.
二、中止
sudo /usr/local/sbin/nginx
-s stop
(原文參考:http://wiki.codemongers.com/NginxInstallOptions )
configure 腳本肯定系統所具備一些特性,特別是 nginx 用來處理鏈接的方法。而後,它建立 Makefile 文件。
configure 支持下面的選項:
1)目錄屬性
--prefix=<path> - Nginx安裝路徑。若是沒有指定,默認爲 /usr/local/nginx。
--sbin-path=<path> - Nginx可執行文件安裝路徑。只能安裝時指定,若是沒有指定,默認爲<prefix>/sbin/nginx。
--conf-path=<path> - 在沒有給定-c選項下默認的nginx.conf的路徑。若是沒有指定,默認爲<prefix>/conf/nginx.conf。
--pid-path=<path> - 在nginx.conf中沒有指定pid指令的狀況下,默認的nginx.pid的路徑。若是沒有指定,默認爲 <prefix>/logs/nginx.pid。
--lock-path=<path> - nginx.lock文件的路徑,默認爲<prefix>/logs/nginx.lock
--error-log-path=<path> - 在nginx.conf中沒有指定error_log指令的狀況下,默認的錯誤日誌的路徑。若是沒有指定,默認爲 <prefix>/logs/error.log。
--http-log-path=<path> - 在nginx.conf中沒有指定access_log指令的狀況下,默認的訪問日誌的路徑。若是沒有指定,默認爲 <prefix>/logs/access.log。
--user=<user> - 在nginx.conf中沒有指定user指令的狀況下,默認的nginx使用的用戶。若是沒有指定,默認爲 nobody。
--group=<group> - 在nginx.conf中沒有指定user指令的狀況下,默認的nginx使用的組。若是沒有指定,默認爲 nobody。
--builddir=DIR - 指定編譯的目錄
2)模塊
--with-rtsig_module - 啓用 rtsig 模塊
--with-select_module --without-select_module -容許或不容許開啓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 -開啓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 PAT
---------------------
經過給回源服務器配置緩存的案例,詳細講解一整套緩存配置機制,而且可沿用到其餘任何緩存配置場景中。
源站架構:源站是nginx+PHP的webserver架構,如圖所示:
proxy_cache_path 緩存文件路徑
levels 設置緩存文件目錄層次;levels=1:2 表示兩級目錄
keys_zone 設置緩存名字和共享內存大小
inactive 在指定時間內沒人訪問則被刪除
當配置好以後,重啓nginx,若是不報錯,則配置的proxy_cache會生效
proxy_cache_valid 200 206 304 301 302 10d; 對httpcode爲200…的緩存10天
proxy_cache_key $uri 定義緩存惟一key,經過惟一key來進行hash存取
proxy_set_header 自定義http header頭,用於發送給後端真實服務器。
參數 | 正常請求 | range請求 |
返回過時時間 | 返回 | 不返回 |