nginx简单实现负载均衡

本机ip:192.168.88.134

设置两个虚拟ip:

/sbin/ifconfig eth0:1 192.168.88.135 broadcast 192.168.88.255 netmask 255.255.255.0 up
/sbin/route add -host 192.168.88.135 dev eth0:1
/sbin/ifconfig eth0:2 192.168.88.136 broadcast 192.168.88.255 netmask 255.255.255.0 up
/sbin/route add -host 192.168.88.136 dev eth0:2

nginx.conf 配置如下

#user  nobody;
worker_processes  4;

error_log  logs/error.log;
error_log  logs/error.log  notice;
error_log  logs/error.log  info;

pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       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  logs/access.log  main;

    sendfile        on;
    tcp_nopush      on;

	server_names_hash_bucket_size 128;
	client_header_buffer_size 32k;
	large_client_header_buffers 4 32k;
	
    #keepalive_timeout  0;
    keepalive_timeout  90;

	tcp_nodelay on;

	#fastcgi settings
	fastcgi_connect_timeout 300;
	fastcgi_send_timeout 300;
	fastcgi_read_timeout 300;
	fastcgi_buffer_size 64k;
	fastcgi_buffers 4 64k;
	fastcgi_busy_buffers_size 128k;
	fastcgi_temp_file_write_size 128k;
	#gzip settings
    gzip  on;
    gzip_min_length 1k;
	gzip_buffers 4 16k;
	#gzip_http_version 1.1
    gzip_comp_level 2;
	gzip_types text/plain application/x-javascript text/css application/xml
	gzip_vary on;

	#允许客户端请求的最大单个文件字节数
	client_max_body_size 50m;
    #缓冲区代理缓冲用户端请求的最大字节数,可以理解为保存到本地再传给用户
    client_body_buffer_size 256k;
    client_header_timeout 3m;
    client_body_timeout 3m;
    send_timeout 3m;
    proxy_connect_timeout 300s;
    #nginx跟后端服务器连接超时时间(代理连接超时)
    proxy_read_timeout 300s;
    #连接成功后,后端服务器响应时间(代理接收超时)
    proxy_send_timeout 300s;
    proxy_buffer_size 64k;
    #设置代理服务器(nginx)保存用户头信息的缓冲区大小
    proxy_buffers 4 32k;
    #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
    proxy_busy_buffers_size 64k;
    #高负荷下缓冲大小(proxy_buffers*2)
    proxy_temp_file_write_size 64k;
    #设定缓存文件夹大小,大于这个值,将从upstream服务器传递请求,而不缓冲到磁盘
    proxy_ignore_client_abort on;
    #不允许代理端主动关闭连接

	#配置负载均衡实例
	upstream 192.168.88.136   //负载均衡地址
	{
	   server 192.168.88.134:8080;
	   server 192.168.88.135:8080;
	  # server 192.168.88.136:8080;
	}

    server {
        listen       192.168.88.134:8080;
        server_name  192.168.88.134;

        #charset koi8-r;

        access_log  logs/host.access.log  main;

        location / {
            # root   html;
            # index  index.html index.htm index.php;
			#autoindex on;
        }

        #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:8000;
           fastcgi_index  index.php;
           #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
		   fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html/$fastcgi_script_name;
           include        fastcgi_params;
        }
        location ~ \.cgi$ {
           root           html;
           fastcgi_pass   127.0.0.1:7000;
           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;
        #}
    }


    #set localhost server

    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    server {
       listen       192.168.88.135:8080;
       server_name  192.168.88.135;
 
       location / {
           root   html;
           index  index.html index.htm index.php;
       }
	   location ~ \.cgi$ {
           root           html;
           fastcgi_pass   127.0.0.1:7000;
           fastcgi_index  index.php;
           fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
           include        fastcgi_params;
        }

       location ~ \.php$ {
           root           /usr/local/nginx/html2;
           fastcgi_pass   127.0.0.1:8000;
           fastcgi_index  index.php;
           #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
		   fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html/$fastcgi_script_name;
           include        fastcgi_params;
        }

    }
	
    server {
       listen       192.168.88.136:8080;
       server_name  192.168.88.136;
 
       location / {
           root   html;
           index  index.html index.htm index.php;
		     proxy_pass        http://192.168.88.136;  //所有这个server的请求处理都由配置的其他负载处理
             proxy_set_header   Host     $host;
             proxy_set_header   X-Real-IP    $remote_addr;
             proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
       }
	   location ~ \.cgi$ {
           root           html;
           fastcgi_pass   127.0.0.1:7000;
           fastcgi_index  index.php;
           fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
           include        fastcgi_params;
        }

    }
}

解析ip必须开启php-cgi

spawn-fcgi -a 127.0.0.1 -p 8000 -C 10 -u nobody -f /usr/bin/php-cgi

sbin/nginx -s reload

/etc/init.d/nginx restart

test.php:

<?php
  // phpinfo();
  var_dump($_SERVER['SERVER_ADDR']); 
?>

http://192.168.88.136:8080/test.php

打印结果为:

string(14) "192.168.88.134" 或者 string(14) "192.168.88.135" 

两个IP地址的来回切换说明这个负载使用的简单的轮训处理机制


文章参考:

http://www.cnblogs.com/xiaogangqq123/archive/2011/03/04/1971002.html

http://blog.chinaunix.net/uid-301743-id-4801730.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值