We all know that. A domain name corresponds to an IP address, and a website application (or location) of the corresponding port service on the corresponding IP address. And the large-scale site of the large number of concurrent visits, how these sites on a webserver to achieve load balancing it?
I believe very many people will have the same doubts with me, but in fact the mature solution has been put into use on a large scale. The reverse proxy method is often used.
The reverse proxy method is a proxy server that accepts connection requests on the Internet and then forwards the requests to the server on the internal network (Reverse proxies). and returns the results from the server to the client requesting a connection on the Internet, at which point the proxy server behaves as a server. The basic structure can be seen, for example, in the following:
From this figure it can be seen that the user access to the webserver is actually a proxy server, and the actual processing of content (such as a series of complex operations) is completed by the wall of the server. This not only reduces the pressure on the agent server. At the same time, the in-wall server can be isolated from the client. Prevent the occurrence of some security problems. At this point, you just need to add the in-Wall server. Although the user is visiting the same address, the actual server that may be interviewed is completely different.
These are just a few theories. How to achieve it? Using Nginx can easily complete this function.
1, first, (for Windows) download Nginx http://nginx.org/en/download.html. Software size and software itself is a super-lightweight thing. To be able to say it almost doesn't feel like it exists.
Of course, first of all, a simple look at nginx.
Nginx("Engine X") is a high-performance, lightweightHTTP WebServer andReverse proxy Serverand e-mailIMAP/POP3/SMTPProxy Server.
First, it is a webserver, but also a good performance of the reverse proxy server
After download, unzip, directly open Nginx.exe can directly execute nginxserver. At this point the browser input localhost can see the interface.
2, configuration files.
Conf/nginx.conf
The default configuration is as follows. It's easy to see
#user Nobody;worker_processes 1, #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; #keepalive_timeout 0; Keepalive_timeout 65; #gzip on; server {Listen 80; server_name localhost; #charset Koi8-r; #access_log Logs/host.access.log Main; Location/{root HTML; Index index.html index.htm; } }}
The overall configuration is slightly similar to Apache, assuming that the relevant log parameters can be opened when developing, Error_log and Access_log. The contents of the server are displayed, a server is defined, the listener is 80port, and the location is/html/*. The home page is index.html and redirects the 50X error to 50x.html.
can also define VirtualHost. and position the location to a different path.
3, different address of the agent.
Nginx can be implemented to different addresses of the reverse proxy (here is not referring to different IP or domain name address, but the path), such as want to all display part (/VIEW/XXX) to Aserver, the address is x.x.x.101. The Logic Processing Section (/CONTROL/XXX) is given to bserver. The address is x.x.x.102. Ability to join for example the following configuration
Location ~ \/view/[\s]+.php$ { proxy_pass http://x.x.x.101;} Location ~ \/control/[\s]+.php$ { proxy_pass http://x.x.x.102;}
~ Indicates the use of a regular table to match, if the location after the addition of ^~ means that does not match the regular, the above configuration will all/view/*.php the request to http://x.x.x.101, and/control/*.php's request was given tohttp://x.x.x.102. Logically implement proxy and detach.
Of course on a, bserver, can build their own needs such as apacheserver or Tomcat or even Nodejs
4, the same address load balancing.
Assume that there is a huge amount of concurrent access to the same address. Load balancing is required. For example, a snapping page or a certain type of page. Ability to join for example the following configuration defines a webserver cluster:
At the same time. Configure a location for the cluster, as in the same way:
Location ~ \/views/[\s]+php${proxy_pass http://webCluster; proxy_redirect off; Proxy_set_header Host $host; proxy_set_h Eader X-real-ip $remote _addr; Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for; }
Again, all the folders under/views will be distributed evenly to all the machines in the cluster.
There are no weights for all the addresses in the cluster, the default weight=1, and of course the ability to define the weights themselves, so that some of the high-processing servers get many other requests:
This altogether weight = 5,101 server will have 2/5 weight, the other is 1/5.
The above is the implementation of large-scale site load balancing on the Web, the implementation method is the cheapest and efficient. Of course. There are also a number of other ways to implement load balancing, such as IP redirection using some large hardware, and so on.
Build Load balancer server with Nginx