使用nginx做负载均衡

请参考

    • Using nginx as HTTP load balancer

 

    NGINX Load Balancing | NGINX

将CentOS6安装

    CentOS6.xにてnginxの最新版をインストールする手順 – Qiita
rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
yum install -y nginx
service nginx start

安装后的初始设置

    長くなるのでコメントは削除してます
user  nginx;
worker_processes  1;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    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  /var/log/nginx/access.log  main;
    sendfile        on;
    #tcp_nopush     on;
    keepalive_timeout  65;
    #gzip  on;
    include /etc/nginx/conf.d/*.conf;
}
server {
    listen       80;
    server_name  localhost;
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}
    /usr/share/nginx/html/index.html が 最初に使われるindex.html

负载均衡器配置

http {
    upstream backend {
        server yahoo.co.jp;
        server google.co.jp;
        server 192.168.0.4;
    }
    server {
        location / {
            proxy_pass http://backend;
        }
    }
}

确认在访问nginx多次后,可以显示yahoo、google和192.168.0.4的网页内容。

请参考手册以获取更多信息。

广告
将在 10 秒后关闭
bannerAds