nginx負載均衡簡單配置

nginx負載均衡簡單配置

準備三臺虛擬機來做這個實驗:

192.168.232.132        web服務器
192.168.232.133        web服務器
192.168.232.134        負載均衡服務器

首先三臺電腦預裝nginx軟件:


1、導入外部軟件庫
[plain] view plain copy
 print?
  1. rpm -Uvh http://dl.iuscommunity.org/pub/ius/stable/Redhat/6/i386/epel-release-6-5.noarch.rpm  
  2. rpm -Uvh http://dl.iuscommunity.org/pub/ius/stable/Redhat/6/i386/ius-release-1.0-10.ius.el6.noarch.rpm  
  3. rpm -Uvh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm  


以下添加註釋
[plain] view plain copy
 print?
  1. mirrorlist=http://dmirr.iuscommunity.org/mirrorlist?repo=ius-el6&arch=$basearch  


以下刪除註釋
[plain] view plain copy
 print?
  1. #baseurl=http://dl.iuscommunity.org/pub/ius/stable/Redhat/5/$basearch  


2、yum安裝nginx

yum install nginx  

3、啓動nginx

chkconfig nginx on  
service nginx start

向web服務器中放入測試文件:
[html] view plain copy
 print?
  1. <html>    
  2. <head>    
  3. <title>Welcome to nginx!</title>    
  4. </head>    
  5. <body bgcolor="white" text="black">    
  6. <center><h1>Welcome to nginx! 192.168.232.132</h1></center>    
  7. </body>    
  8. </html>  



配置負載均衡服務器:

vi /etc/nginx/nginx.conf

內容如下:

[plain] view plain copy
 print?
  1. user  nginx;  
  2. worker_processes  1;  
  3.   
  4. error_log  /var/log/nginx/error.log warn;  
  5. pid        /var/run/nginx.pid;  
  6.   
  7.   
  8. events {  
  9.     worker_connections  1024;  
  10. }  
  11.   
  12.   
  13. http {  
  14.     include       /etc/nginx/mime.types;  
  15.     default_type  application/octet-stream;  
  16.   
  17.     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '  
  18.                       '$status $body_bytes_sent "$http_referer" '  
  19.                       '"$http_user_agent" "$http_x_forwarded_for"';  
  20.   
  21.     access_log  /var/log/nginx/access.log  main;  
  22.   
  23.     sendfile        on;  
  24.     #tcp_nopush     on;  
  25.   
  26.     keepalive_timeout  65;  
  27.   
  28.     #gzip  on;  
  29.     upstream test.miaohr.com {  
  30.     server 192.168.232.132:80;  
  31.     server 192.168.232.133:80;  
  32.     }  
  33.       
  34.   
  35.     server {     
  36.         listen       80;     
  37.         server_name  test.miaohr.com;     
  38.         charset utf-8;     
  39.         location / {     
  40.             root   html;     
  41.             index  index.html index.htm;     
  42.             proxy_pass        http://test.miaohr.com;     
  43.             proxy_set_header  X-Real-IP  $remote_addr;     
  44.             client_max_body_size  100m;  
  45.         }     
  46.     
  47.     
  48.         location ~ ^/(WEB-INF)/ {      
  49.         deny all;      
  50.         }      
  51.     
  52.         error_page   500 502 503 504  /50x.html;     
  53.         location = /50x.html {     
  54.             root   /var/www/html/;     
  55.         }     
  56.     }     
  57. }  




下面瀏覽器打開:192.168.232.134,如果132、133交替顯示則表明試驗成功。

拓展:


1、輪詢(默認)
每個請求按時間順序逐一分配到不同的後端服務器,如果後端服務器down掉,能自動剔除。

2、weight
指定輪詢機率,weight和訪問比率成正比,用於後端服務器性能不均的情況。
例如:
upstream bakend {
server 192.168.159.10 weight=10;
server 192.168.159.11 weight=10;
}

3、ip_hash
每個請求按訪問ip的hash結果分配,這樣每個訪客固定訪問一個後端服務器,可以解決session的問題。
例如:
upstream resinserver{
ip_hash;
server 192.168.159.10:8080;
server 192.168.159.11:8080;
}


4、fair(第三方)
按後端服務器的響應時間來分配請求,響應時間短的優先分配。
upstream resinserver{
server server1;
server server2;
fair;
}


5、url_hash(第三方)

按訪問url的hash結果來分配請求,使每個url定向到同一個後端服務器,後端服務器爲緩存時比較有效。
例:在upstream中加入hash語句,server語句中不能寫入weight等其他的參數,hash_method是使用的hash算法

upstream resinserver{
server squid1:3128;
server squid2:3128;
hash $request_uri;
hash_method crc32;
}

tips:

upstream resinserver{#定義負載均衡設備的Ip及設備狀態
ip_hash;
server 127.0.0.1:8000 down;
server 127.0.0.1:8080 weight=2;
server 127.0.0.1:6801;
server 127.0.0.1:6802 backup;
}

在需要使用負載均衡的server中增加
proxy_pass http://resinserver/;


每個設備的狀態設置爲:
1.down 表示單前的server暫時不參與負載
2.weight 默認爲1.weight越大,負載的權重就越大。
3.max_fails :允許請求失敗的次數默認爲1.當超過最大次數時,返回proxy_next_upstream 模塊定義的錯誤
4.fail_timeout:max_fails次失敗後,暫停的時間。
5.backup: 其它所有的非backup機器down或者忙的時候,請求backup機器。所以這臺機器壓力會最輕。

nginx支持同時設置多組的負載均衡,用來給不用的server來使用。

client_body_in_file_only 設置爲On 可以講client post過來的數據記錄到文件中用來做debug
client_body_temp_path 設置記錄文件的目錄 可以設置最多3層目錄
location 對URL進行匹配.可以進行重定向或者進行新的代理 負載均衡
發佈了31 篇原創文章 · 獲贊 7 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章