upstream 配置php
在http模塊
upstream app {html
server 192.168.0.250:8081 weight=5;
server www.cn;
server unix:/tmp/app;nginx
server w1.cn:8082 backup;
server w2.cn:8083 backup;web
}算法
server配置參數apache
指令後端 |
含義緩存 |
down服務器 |
當前的server暫時不參與負載均衡swoole |
backup |
預留的備份服務器(一旦其它的服務器均宕機沒有存活的了,該標記的機器就會接收請求) |
max_fails |
設置在fail_timeout時間內嘗試對一個服務器鏈接的最大次數,若是超過這個次數,那麼就會標記爲down,即容許請求失敗的次數 |
fail_timeout |
某個server鏈接失敗了max_fails次,則nginx會認爲該server不工做了 同時,在接下來的 fail_timeout時間內,nginx再也不將請求分發給失效的server。過了fail_timeout會再次檢查服務是否恢復。默認:fail_timeout爲10s,max_fails爲1次 |
max_conns |
限制最大的接收的鏈接數 |
nginx負載均衡配置實例
分別監聽8081,8082,8083
code1,code2,code3下分別有
index.php
<?php echo "<pre>"; print_r($_SERVER); ?>
code1.conf
server { listen 8081; server_name 192.168.0.250:8081; root /mnt/hgfs/www/web/code1; autoindex on; access_log /mnt/hgfs/www/log/w1.access.log main; location / { index index.php; autoindex on; autoindex_exact_size off; autoindex_localtime on; } error_log /mnt/hgfs/www/log/w1_err.log info; error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
nginx.conf裏部分
http裏
upstream code{ server 192.168.0.250:8081; server 192.168.0.250:8082; server 192.168.0.250:8083; server 192.168.0.251; }
server { listen 80; server_name 192.168.0.250 ; access_log logs/host.access.log main; location / { proxy_pass http://code; } }
注意輸出的 http_host都是code
[HTTP_HOST] => code
[SERVER_NAME] => 192.168.0.250:8083
[SERVER_PORT] => 8083
[SERVER_ADDR] => 192.168.0.250
默認的方式是輪詢
阻止8082 的 若是用同一進程啓動的,用端口關掉模擬
iptables -I INPUT -p tcp --dport 8082 -j DROP
開啓
iptables -I INPUT -p tcp --dport 8082 -j ACCEPT
這種在從8081到8082切換後有延遲相應
下面這種沒有
或者關閉 192.168.0.251上的機器上Nginx進程
#演示 upstream code{ server 192.168.0.250:8081 down; server 192.168.0.250:8082 down; server 192.168.0.251 max_fails=1 fail_timeout=10s; server 192.168.0.250:8083 backup; }
停掉 server 192.168.0.251
server 192.168.0.250:8083 當即開始服務
此時當即開啓 192.168.0.251 若是還沒到10s,訪問的仍是 192.168.0.250:8083
到10s後 檢測到 192.168.0.251服務恢復,192.168.0.250:8083 轉變爲backup狀態 不在提供服務,由192.168.0.251提供服務
調度算法
輪詢 |
按時間順序逐一分配到不一樣的後端服務器 |
加權輪詢 |
weight值越大,分配到的訪問概率越高 |
ip_hash |
每一個請求按照固定的ip的hash結果分配,這樣來自同一ip的固定訪問到一個後端服務器,確保客戶端均勻地鏈接全部服務器,鍵值基於C類地址 |
url_hash |
按照訪問的url的hash結果分配請求,是每一個url定向到同一個後端服務器 |
least_conn |
最少鏈接數,哪一個機器鏈接數少就分發哪一個機器 |
hash關鍵數值 |
hash自定義key |
#ip_hash 不支持backup等 upstream code{ ip_hash; server 192.168.0.250:8081 ; server 192.168.0.250:8082 ; server 192.168.0.251; # server 192.168.0.250:8083 backup; }
url_hash
Syntax:hash key [consistent];
Default:--
Context:upstream;
This directive appeared in version 1.7.2
upstream code{ hash $request_uri; server 192.168.0.250:8081 ; server 192.168.0.250:8082 ; server 192.168.0.251; } server { listen 80; server_name 192.168.0.250 ; #root /mnt/hgfs/www/web/thread/process_thread_study/swoole; access_log logs/host.access.log main; location / { proxy_pass http://code; } } include ./hosts/*.conf;
同一url訪問到的機器是一致的
http://192.168.0.250/ 訪問到的始終是同一臺 好比 192.168.0.251
http://192.168.0.250/index1.php 訪問到的始終是同一臺 好比 192.168.0.250:8081
keepalive
保持活動鏈接 ,nginx服務器將會爲每個worker進程保持同上遊服務器的鏈接
鏈接緩存在nginx須要同上遊服務器持續保持必定數量的鏈接時很是有用
若是上游服務器經過http進行對話,那麼nginx將會使用http/1.1協議的持久鏈接機制維護這些打開的鏈接
upstream apache {
server 127.0.0.1:8080;
keepalive 32;
}
location / {
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_pass http://apache;
}
初始的時候nginx僅需爲每一個worker打開32個tcp鏈接,而後經過不發送close的connection頭保持這些鏈接的打開
FastCGI上游服務器
upstream fastcgis{
server 10.88.1.10:9000;
server 10.88.1.20:9000;
server 10.88.1.30:9000;
}
location / {
fastcgi_pass fastcgis;
}