如果Nginx沒有僅僅只能代理一臺服務器的話,那它也不可能像今天這麼火,Nginx可以配置代理多臺服務器,當一臺服務器宕機之後,仍能保持系統可用。具體配置過程如下:
1. 在http節點下,添加upstream節點。
upstream linuxidc {
server 10.0.6.108:7080;
server 10.0.0.85:8980;
}
2. 將server節點下的location節點中的proxy_pass配置爲:http:// + upstream名稱,即“
http://linuxidc”.
location / {
root html;
index index.html index.htm;
proxy_pass http://linuxidc;
}
3. 現在負載均衡初步完成了。upstream按照輪詢(默認)方式進行負載,每個請求按時間順序逐一分配到不同的後端服務器,如果後端服務器down掉,能 自動剔除。雖然這種方式簡便、成本低廉。但缺點是:可靠性低和負載分配不均衡。適用於圖片服務器集羣和純靜態頁面服務器集羣。
除此之外,upstream還有其它的分配策略,分別如下:
weight(權重)
指定輪詢機率,weight和訪問比率成正比,用於後端服務器性能不均的情況。如下所示,10.0.0.88的訪問比率要比10.0.0.77的訪問比率高一倍。
upstream linuxidc{
server 10.0.0.77 weight=5;
server 10.0.0.88 weight=10;
}
ip_hash(訪問ip)
每個請求按訪問ip的hash結果分配,這樣每個訪客固定訪問一個後端服務器,可以解決session的問題。
upstream favresin{
ip_hash;
server 10.0.0.10:8080;
server 10.0.0.11:8080;
}
fair(第三方)
按後端服務器的響應時間來分配請求,響應時間短的優先分配。與weight分配策略類似。
upstream favresin{
server 10.0.0.10:8080;
server 10.0.0.11:8080;
fair;
}
url_hash(第三方)
按訪問url的hash結果來分配請求,使每個url定向到同一個後端服務器,後端服務器爲緩存時比較有效。
注意:在upstream中加入hash語句,server語句中不能寫入weight等其他的參數,hash_method是使用的hash算法。
upstream resinserver{
server 10.0.0.10:7777;
server 10.0.0.11:8888;
hash $request_uri;
hash_method crc32;
}
upstream還可以爲每個設備設置狀態值,這些狀態值的含義分別如下:
down 表示單前的server暫時不參與負載.
weight 默認爲1.weight越大,負載的權重就越大。
max_fails :允許請求失敗的次數默認爲1.當超過最大次數時,返回proxy_next_upstream 模塊定義的錯誤.
fail_timeout : max_fails次失敗後,暫停的時間。
backup: 其它所有的非backup機器down或者忙的時候,請求backup機器。所以這臺機器壓力會最輕。
upstream bakend{ #定義負載均衡設備的Ip及設備狀態
ip_hash;
server 10.0.0.11:9090 down;
server 10.0.0.11:8080 weight=2;
server 10.0.0.11:6060;
server 10.0.0.11:7070 backup;
}
Nginx的配置與部署研究,Upstream負載均衡模塊 http://www.linuxidc.com/Linux/2013-04/82526p2.htm
CentOS 6.2實戰部署Nginx+MySQL+PHP http://www.linuxidc.com/Linux/2013-09/90020.htm
使用Nginx搭建WEB服務器 http://www.linuxidc.com/Linux/2013-09/89768.htm
搭建基於Linux6.3+Nginx1.2+PHP5+MySQL5.5的Web服務器全過程 http://www.linuxidc.com/Linux/2013-09/89692.htm
CentOS 6.3下Nginx性能調優 http://www.linuxidc.com/Linux/2013-09/89656.htm
CentOS 6.3下配置Nginx加載ngx_pagespeed模塊 http://www.linuxidc.com/Linux/2013-09/89657.htm
CentOS 6.4安裝配置Nginx+Pcre+php-fpm http://www.linuxidc.com/Linux/2013-08/88984.htm
Nginx安裝配置使用詳細筆記 http://www.linuxidc.com/Linux/2014-07/104499.htm
Nginx日誌過濾 使用ngx_log_if不記錄特定日誌 http://www.linuxidc.com/Linux/2014-07/104686.htm
Nginx 的詳細介紹:請點這裏
Nginx 的下載地址:請點這裏
用了nginx負載均衡後,在兩臺tomcat正常運行的情況下,訪問http://localhost 速度非常迅速,通過測試程序也可以看出是得到的負載均衡的效果,但是我們試驗性的把其中一臺tomcat(server localhost:8080)關閉後,再查看http://localhost,發現反應呈現了一半反映時間快,一半反映時間非常非常慢的情況,但是最後都能得到正確結果.
解決辦法:
問題解決,主要是proxy_connect_timeout 這個參數, 這個參數是連接的超時時間。 我設置成1,表示是1秒後超時會連接到另外一臺服務器。
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
upstream localhost {
#ip_hash;
server 127.0.0.1:8081;
server 127.0.0.1:8080;
}
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
listen 80;
server_name localhost;
location /{
proxy_pass http://localhost;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 1;
proxy_read_timeout 1;
proxy_send_timeout 1;
}
#charset koi8-r;
#access_log logs/host.access.log main;
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443;
# server_name localhost;
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_timeout 5m;
# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
---------------------
作者:hunhun1122
來源:CSDN
原文:https://blog.csdn.net/hunhun1122/article/details/77396782
版權聲明:本文爲博主原創文章,轉載請附上博文鏈接!