Web Server distributed services: Nginx Load Balancing

  Nginx is a lightweight Web server / reverse proxy server and e-mail (IMAP / POP3) proxy server. The program developed by Russian designer Igor Sysoev, Russia for large-scale web portal and search engine Rambler use. It features occupy less memory, high concurrency.
      
  Nginx is a very powerful, high-performance Web server and reverse proxy, it has a lot of very excellent characteristics at high concurrent connections, Nginx is a good alternative to the Apache server, Nginx virtual host in the United States is doing business owner one often choose software platform. In response to support up to 50,000 concurrent connections.

  In some large-scale projects, Nginx is often used as load balancing, Nginx Web Server requests according to certain rules distributed to distributed, this would solve the problem the project Web Server performance bottlenecks, so that they constitute LNMPA architecture, that Linux + Nginx + Mysql + PHP + Apache, LVS also has the same functions, but have advantages and disadvantages, F5 hardware is the best, but the price is very expensive;
while Nginx is also very suitable for handling static pages, file upload and download the server, in these businesses, other server software is second to none;
in fact, there are many projects to use Nginx as a Web server directly, Nginx to do a Web server in the handling of PHP business logic can not Apache so powerful, if alone Nginx can not to meet the needs of your project, it is separated from the business, Nginx to do load balancing, handling static pages, is responsible for uploading and downloading files, PHP business logic to the Apache.

Reverse proxy and load balancing configuration:

Web Server1:       172.22.28.237:80

Web Server2:       172.22.28.235:80

Web Server3:       172.22.28.234:80

Domain Name: test.box.com           

Nginx Server:      47.252.83.223:80 

 

1.Nginx Configuration

user www; # user operation 
worker_processes   . 1 ; # number of work processes, preferably the number of CPU cores 

Events { 
    worker_connections   1024 ; 
} 


HTTP { 
    the include the mime.types; 
    default_type file application / OCTET - Uninterpreted Stream; 
    charset UTF - . 8 ;         
    the sendfile ON; 
    keepalive_timeout   65 ; 


    Server { 
        the listen        80 ; 80 # listening port 
        server_name test1.box.com; 
        the root / SRV / Box / Site1; #web directory 
        LOCATION / {
             index index.php index.html index.htm;
        }
        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }


    server {
        listen       80;
        server_name test2.box.com
        root /srv/box/site2;  #web目录
        location / {  
           index index.php index.html index.htm; 
        } 
        location ~ \.php$ { 
             fastcgi_pass 127.0.0.1:9000; 
             fastcgi_index index.php; 
             fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
             include fastcgi_params; 
        } 
    }
    
    server {
        listen       80;
        server_name test3.box.com
        root /srv/box/site3;  #web目录
        location / {  
           index index.php index.html index.htm; 
        } 
        location ~ \.php$ { 
             fastcgi_pass 127.0.0.1:9000; 
             fastcgi_index index.php; 
             fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
             include fastcgi_params; 
        } 
    }

} 

2. Create a directory in the web are site1 and site2 directories, each directory are created index.php file, php codes are as follows:

site1
<?php
   echo json_encode(['type'=>'200','data'=>'test']);
?>

site2
<?php
   echo json_encode(['type'=>'200','data'=>'test2']);
?>

site3
<?php
   echo json_encode(['type'=>'200','data'=>'test3']);
?>

The two directory permissions to www www user group:
chown -R & lt www: www Site1 /
chown -R & lt www: www Site2 /
chown -R & lt www: www site3 /

 

3 Nginx reverse proxy server and a load balancing configuration: '

# Load balancing configuration, the right to re-distribute the way 
upstream test.box.com { 
   Server 172.22 . 28.237 weight = . 5 ; 
   Server 172.22 . 28.235 weight = . 5 ; 
   Server 172.22 . 28.234 weight = . 5 ; 
} 

# reverse proxy configuration 
Server { 
 the listen        80 ; 
 server_name test.box.com; 
 charset UTF - . 8 ; 
 LOCATION / { 
    proxy_pass HTTP: // test.box.com; 
} 
}

4, restart the service service nginx restart Nginx

5, browser access test.box.com, multi-access times, so that we can see, will be replaced in accordance with the weight display different content.

So far, Nginx load balancing, reverse proxy configured.

Nginx way to distribute load balancing There are four:

1. poll, taken this way by default, Nginx will distribute polling requests in the order of time, if a station Web Server downtime, Nginx automatically remove.

2.weight, weight, i.e. the probability of the polling, the larger the value, the greater the possibility of being distributed, for the case where unevenness backend server performance.

3.ip_hash, each request assigned by hash result Access ip so that each visitor to access a fixed back-end server, can solve the problem of sharing session.

4. Custom Rule
Example:
upstream test.box.com {
Server Down 172.22.28.237;
Server 172.22.28.235 weight = 2;
Server 172.22.28.236;
Server 172.22.28.234 Backup;
}

Detailed configuration:

down indicates that the current Web Server is temporarily not participate in load
weight by default 1.weight greater, the greater the weight load of weight.
backup: All other non-backup Server down or busy, request backup machine. So this machine will be the lightest pressure.

backup using nginx hot standby function, which is also one of the important benefits of the most typical application brings, when the non-backup Server can provide good services for the Client, backup Server does not provide services outside world, this time in a backup Server cold state; when all non-backup Server can not be good for the Client to provide services, backup Server Client for the provision of services, so that the hot backup, or a table of all non-backup Web Server downtime will not affect access throughout the Web project , Web project can still provide services to the Client.


Nginx做负载均衡,对Web Server的操作系统和语言环境没有特殊要求,Web Server的操作系统可以是Linux也可以是Windows Server,Web程序是Java、PHP、.Net等均可以。

Guess you like

Origin www.cnblogs.com/tonyjude/p/11696591.html