nginx简单实现负载均衡

原创 2015年07月08日 11:00:23

本机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

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Nginx简单实现反向代理和负载均衡

Nginx安装好之后,开始使用它来简单实现反向代理与负载均衡的功能。在这之前,首先得脑补一下什么是反向代理和负载均衡。   反向代理:是指以代理服务器来接收Internet上的请求,然后将请求转发到...

学习Nginx反向代理实现简单负载均衡

学习Nginx反向代理实现简单负载均衡

nginx 负载均衡简单实现

nginx 负载均衡简单实现 nginx能够实现负载均衡,在被反向代理的服务器中进行按需的负载均衡配置: 下面是一个配置文件: #user nobody; worker_processes 1; ...

nginx服务器安装并简单实现负载均衡

简介 Nginx("engine x")是一款是由俄罗斯的程序设计师Igor Sysoev所开发高性能的 Web和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 在高连接...

使用Nginx反向代理来实现简单的负载均衡

Nginx反向代理的原理优点: 反向代理可以简单的理解为:代理服务器来接收internet上的服务器请求,然后将请求转发给内部的服务器上,然后将结果返回给internet上请求的客户端,所以代理服务...

简单东西-Nginx实现Tomcat负载均衡

基本概念百度百科上定义Nginx是这样的: Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。由俄罗斯的程序设...

使用nginx简单实现反向代理和负载均衡

nginx 这个轻量级、高性能的 web server 主要可以干两件事情:   〉直接作为http server(代替apache,对PHP需要FastCGI处理器支持);   〉另外一个功...

利用nginx和docker实现一个简单负载均衡

测试步骤:1.在服务器中搭建一个nginx服务器并启动 2.在docker中从源中拉一下nginx的官方镜像,留以docker容器运行//下载过程可能会有点慢,不如吃点零食 docker pull ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)