The Coding World

其实生命就是一个过程,到最后一天你啥也带不走,精彩的是你留下什么。

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

阅读更多
文章标签: ngnix 负载均衡
个人分类: c/c++ linux学习
上一篇nginx解析php文件及问题
下一篇nginx实现简单模块开发
想对作者说点什么? 我来说一句

Nginx负载均衡和反向代理

2018年01月17日 200KB 下载

nginx负载均衡 反向代理

2018年05月09日 3.06MB 下载

Nginx负载均衡入门

2011年06月16日 583KB 下载

负载均衡Nginx

2012年10月26日 3.37MB 下载

nginx 配置指南

2008年11月19日 510KB 下载

基于lua的nginx自定义负载均衡

2018年05月08日 38.38MB 下载

没有更多推荐了,返回首页

关闭
关闭