nginx Is a very good lightweight reverse proxy server , It can be done easily web And be able to load balance .
nginx adopt upstream To achieve load balancing, the specific configuration is as follows :
upstream web_project {
server 127.0.0.1:8080;
server 127.0.0.1:8081;
}
server {
location / {
proxy_pass http://web_project;
}
}
among web_project by upstream The name is arbitrarily named as the service name or project name ,nginx The default load balancing policy is polling
Other strategies include Weighted polling :
upstream web_project {
server 127.0.0.1:8080 weight=1;
server 127.0.0.1:8081 weight=2;
}
among weight Said the weight , The greater the number , The heavier the load . The second service load in the example is the first one 2 times
In such as :IP hash:
upstream web_project {
ip_hash;
server 127.0.0.1:8080;
server 127.0.0.1:8081;
}
Same client IP It will be allocated to the same service, which can be used to solve the problem of distribution session Inconsistency .
In such as fair:
upstream web_project {
server 127.0.0.1:8080;
server 127.0.0.1:8081;
fair;
}
Allocate according to the response time of the service , Priority allocation with short response time
In such as url_hash:
upstream web_project {
server 127.0.0.1:8080;
server 127.0.0.1:8081;
hash $request_uri;
hash_method crc32;
}
URL hash and IP hash be similar , It's just that URL As hash Source , Availability guarantees that the same request will be assigned to the same service , among hash_method Indicates the hash Algorithm .
Finally, let's have a complete configuration
upstream web_project {
server 127.0.0.1:8080;
server 127.0.0.1:8081;
}
server {
listen 80;
server_name www.zux2.com;
root /home/webroot;
location / {
index index.html;
}
location ~.json$ { # Dynamic request ( All with .json The closing request ) Load balancing ,
proxy_pass http://web_project;
}
}
more JAVA problem , Please pay attention to wechat applet :