Nginx with upstream module for simple load Balancing
Within the upstream block, define a list of servers, the default is polling, if you want to determine that the same visitor's request is always handled by a back-end server, you can set the Ip_hash,
Such as:
Upstream cctest1.com {ip_hash; Server 127.0.0.1:9080 weight=5; Server 127.0.0.1:8080 weight=5; Server 127.0.0.1:1111; } server {location/{Proxy_pass http://cctest1.com; } }
Note: This method is essentially polling, and because the client's IP may be constantly changing, such as dynamic IP, proxy, flip wall and so on, so Ip_hash does not fully guarantee that the same client always by the same
A server to handle.
Instructions
Ip_hash
This command distributes the request based on the IP address of the client connection . The hash keyword is the client's Class C network address, which ensures that the client request is always forwarded to a server, but if the server is unavailable, the request is forwarded to the other server, which guarantees a large probability that a client is always linked to a single server.
You cannot use weights (weght) in conjunction with Ip_hash to distribute the links. If a server is unavailable, you must mark it as "down", as follows:
Upstream backend {ip_hash; Server backend1.example.com; Server backend2.example.com; Server backend3.example.com down; Server backend4.example.com;}
Attention:
1, to ensure the same client's request, forwarded to the same server processing, to ensure the consistency of the session. (Invalid for client through proxy, flip wall, etc.)
2. True load balancing is not guaranteed here
3, for the same client request, forwarded to the same server processing, if this server fails, will not be forwarded, an error occurred.
Server
Specify the name of the backend server and some parameters that can be used with the domain name, IP, port, or UNIX socket
Syntax: server name [para]
Parameters:
Weight = number
Set the server's weight, default to 1
Max_fails = number
In a certain time (this time love Fail_timeout parameters set) to check whether this server is available when the maximum number of failed requests, the default is 1, set it to 0 can
Checks are turned off, and these errors are defined in Proxy_next_upstaream or Fastcgi_next_uppstaream (404 error will not be added max_fails).
Fail_timeout = time
This server is not available after a failed attempt to connect to the size set by max_fails at this time, and it also specifies the time at which the server is unavailable (at the next taste
Test connection request), the default is 10 seconds, Fial_timeout is not directly related to the front-end response time, you can not use Proxy_connect_timeout and
Proxy_read_timeou to control
Down
The tag server is offline and is typically used with Ip_hash.
Backup
If all non-backup servers are down or busy, use this server (cannot be used in conjunction with Ip_hash directives).
$upstream _status
Response status of the front-end server
$upstream _response_time
Front-end server response time, accurate to milliseconds, different answers separated by commas and colons
$upstream _http_$header
Arbitrary HTTP protocol headers, such as:
$upstream _http_host
Examples of applications such as:
upstream xiaoliu.com { server 127.0.0.1:8080 weight=5; server 127.0.0.1:9080 weight=10;} Server { listen 8888; server_name testserver1; #access_log logs/host.access.log main; index index.html index.jsp; root /opt/tomcat/webapps/ROOT; location ~* ^/customermger/.*\. (Jpg|jpeg|gif|png|swf|ico) $ { root /opt/tomcat/ Webapps; } location ~* ^/customermger/.*\. (HTML|HTM|JS|CSS) $ { root /opt/tomcat/webapps; } location ~* ^/liuy-manager-web/.*\. (Jpg|jpeg|gif|png|swf|ico) $ {   &NBSp; root /opt/tomcat/webapps; } location ~* ^/liuy-manager-web/.*\. (HTML|HTM|JS|CSS) $ { root /opt/tomcat/webapps; } location ~* .*\. (Jpg|jpeg|gif|png|swf|ico) $ { if (-f $request _ FileName) { #expires 15d; break; } } location ~* .*\. (html|htm|js|css) $ { #expires 1d; } location / { proxy_pass http://xiaoliu.com ; }}
1th Time Request Http://192.168.91.6:8888/liuy-manager-web
650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M01/8F/81/wKioL1jjOjqi731AAAIFHPajiJk569.png "title=" 11. PNG "alt=" Wkiol1jjojqi731aaaifhpajijk569.png "/>
Nth Request Http://192.168.91.6:8888/liuy-manager-web
650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M00/8F/84/wKiom1jjOzzC7f-mAAH2Td89sf0831.png "title=" 111. PNG "alt=" Wkiom1jjozzc7f-maah2td89sf0831.png "/>
Geo and GEIP modules
These two modules are primarily used for global load balancing and can access different servers depending on the client IP, as follows:
http{ geo $geo { default default; 202.103.10.1/24 a; 179.9.0.3/24 B; } upstream default.server{ server 192.168.0.100; } upstream A.server{ server 192.168.0.101; } upstream b.server{ server 192.168.0.102; } server{ listen 80; location / { proxy_pass http://$geo. server$request_uri; } }}
For example: Beijing's clients to Beijing's servers, Shanghai's clients to Shanghai's services
This article is from "I Love Big gold" blog, please be sure to keep this source http://1754966750.blog.51cto.com/7455444/1912805
Linux Learning: nginx--load Balancing-05