【ngnix】进行负载配置

1, 测试环境
a) 3台centos虚拟机IP分别为190,191,192
b) 安装软件三台安装nginx。
在http://nginx.org/下载需要版本
tar -zxvf pcre-8.21.tar.gz
cd pcre-8.21
./configure
make
make install
启动nigix并进行访问。
/usr/local/nginx/sbin/nginx -c /root/nginx-1.6.2/conf/nginx.conf
后面参数为配置文件地址。因为下载地址为root下故在/root/nginx-1.6.2/conf/nginx.conf。
修改vi /usr/local/nginx/html/index.html,用于区分不同的nginx的访问
<title>Welcome to nginx!本地IP地址 web page</title>
将对应IP进行测试访问。
2, 使用191作为主服务器,所有访问均通过191负载到190,191或192上。
修改191上/root/nginx-1.6.2/conf/nginx.conf配置文件
加入
upstream a.com {
      least_conn;    //使用负载的策略
      server  192.168.0.190:80;
      server  192.168.0.192:80;
}
修改:
        location / {
            root   html;
            index  index.html index.htm;
        proxy_pass         http://a.com;
        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        }
注意上面红色部分.
重启
重新加载配置
kill -HUP $nginx_pid
或者
/usr/nginx/sbin/nginx -s reload
但在加载前最好检查一下配置文件:
nginx -t -c /usr/nginx/conf/nginx.conf
或者
/usr/nginx/sbin/nginx -t
访问 http://192.168.0.191进行访问测试。 页面跳转190或192.


3, 对自己进行负载
问题:使用了三台nginx却只有两台可以进行访问,那么还有一台也像作为访问对象怎么办?
在191上加入多端口监听。多一个server配置。
    server {
        listen       8080;
        server_name  192.168.0.200;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #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 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
并upstram配置中:
upstream a.com {
      least_conn;
      server  192.168.0.191:8080;
      server  192.168.0.190:80;
      server  192.168.0.192:80;
}
测试访问。即可。

猜你喜欢

转载自duduli.iteye.com/blog/2123383