2014年3月8日 星期六

web load balance

Nginx 要作到 load balance 很簡單,只要幾個設定就好了。以下面的 Nginx 設定檔為例(來源)。


worker_processes 2;
 
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
 
events {
    worker_connections 1024;
    use epoll;
}
 
http {
    charset utf-8;
 
    # Enumerate all the Tornado servers here
    upstream frontends {
        server 127.0.0.1:8000;
        server 127.0.0.1:8001;
        server 127.0.0.1:8002;
        server 127.0.0.1:8003;
    }
 
    include /path/to/nginx.mime.types;
    default_type application/octet-stream;
 
    access_log /var/log/nginx/access.log;
 
    keepalive_timeout 65;
    proxy_read_timeout 200;
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    gzip on;
    gzip_min_length 1000;
    gzip_proxied any;
    gzip_types text/plain text/css text/xml
               application/x-javascript application/xml
               application/atom+xml text/javascript;
 
    # Only retry if there was a communication error, not a timeout
    # on the Tornado server (to avoid propagating "queries of death"
    # to all frontends)
    proxy_next_upstream error;
 
    server {
        listen 80;
 
        location ^~ /static/ {
            root /path/to/app;
            if ($query_string) {
                expires max;
            }
        }
 
        location / {
            proxy_pass_header Server;
            proxy_set_header Host $http_host;
            proxy_redirect off;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Scheme $scheme;
            proxy_pass http://frontends;
        }
    }
}
只要簡單的設定 upstream frontends(frontends名稱可自訂) 裡的 server ,即可作到 loadbalance。

下面連結有 lighttpd 的 load balance 例子。
http://www.playframework.com/documentation/2.2.x/HTTPServer
設定檔大概就長這樣



server.modules = (
      "mod_access",
      "mod_proxy",
      "mod_accesslog"
)

$HTTP["host"] =~ "www.myapp.com" {
    proxy.balance = "round-robin" proxy.server = ( "/" =>
        ( ( "host" => "127.0.0.1", "port" => 9000 ) ) )
}

$HTTP["host"] =~ "www.loadbalancedapp.com" {
    proxy.balance = "round-robin" proxy.server = ( "/" => (
          ( "host" => "127.0.0.1", "port" => 9001 ),
          ( "host" => "127.0.0.1", "port" => 9002 ) )
    )
}



apache 的 load balancer 的資料在這
http://httpd.apache.org/docs/2.2/mod/mod_proxy_balancer.html

沒有留言:

張貼留言