nginx load balance配置

本文大概描述负载均衡的一般配置。关于upstream和sticky的问题可参看博文

安装nginx:

 
  1. wget http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm  
  2. rpm -ivh nginx-release-centos-6-0.el6.ngx.noarch.rpm  
  3. yum install nginx  

vim /etc/nginx/conf.d/balance.conf

 

 
  1. upstream backend {  
  2.   sticky;  
  3.   check interval=5000 rise=2 fall=5 timeout=1000;  
  4.   check_http_send "GET /health.html HTTP/1.1\r\n\r\n";  
  5.   server        192.168.0.6:8080;  
  6.   server        192.168.2.130:8080;  
  7.   server        192.168.0.4:8080;  
  8.   server        192.168.0.5:8080;  
  9. }  
  10.  
  11. server {  
  12.   listen 80;  
  13.   location /system/console {  
  14.     root /var/www/html/nginx/empty;  
  15.         client_max_body_size    300m;  
  16.   }  
  17.   location /libs {  
  18.     root /var/www/webstatic;  
  19.         client_max_body_size    300m;  
  20.   }  
  21.  
  22.   location / {  
  23.     client_max_body_size 300m; #此处设置的数值决定了nginx上传文件大小的限制 
  24.     proxy_pass http://backend;  
  25.     proxy_set_header  X-Real-IP  $remote_addr;  
  26.     add_header X-Real-IP  $upstream_addr;  
  27.     proxy_buffering off;  
  28.   }  
  29. }  
  30. server  
  31. {  
  32.  listen 80;  
  33.  server_name test.abc.org abc.abc.org www.abc.com abc.com;  
  34.  access_log     /var/log/nginx/xuanran_acc.log;  
  35.  location / {  
  36.  root /home/abc/test;  
  37.  index index.html index.htm index.php;  
  38. }  
  39. }  
  40. server  
  41. {  
  42.  listen 80;  
  43.  server_name www.abc.org abc.org;  
  44.  access_log     /var/log/nginx/abc_acc.log;  
  45.   location / {  
  46.  root /home/abc/;  
  47.  index index.html index.htm index.php;  
  48. }  
  49. }