Nginx Load Balancing and Forwarding

1,6 kinds of load balancing strategy

1. Polling : default

2, weight : weight way

3, ip_hash : ip based distribution

. 4, least_conn : Minimum connection

. 5, Fair (third party): response time mode

6, url_hash (third party): According to the URL distribution

parameter:

1, fail_timeout: using max_fails binding.

2, max_fails: Set in the time fail_timeout parameter setting the maximum number of failures, if at this time, all requests for that server fail, it is considered that the server is considered to be shut down.

3, fail_time: server would be considered the length of time off, the default is 10s.

4, backup: Mark the server is on standby. When the primary server is stopped, the request will be sent to it here.

5, down: mark permanently shut down the server.

2, nginx load balancing configuration

1, polling

No special configuration, the default configuration is polling policies.

note:

  • In the poll, if the server is down, the server will be automatically rejected.
  • The default configuration is polling policies.
  • This policy is quite suitable for server configuration, stateless and fast track service usage.

2, weight

Weight manner, on the basis of the specified polling on polling strategies chance.

Example:

upstream dynamic_test {
    server localhost:8080   weight=2;  #tomcat 7.0
    server localhost:8081;  #tomcat 8.0
    server localhost:8082   backup;  #tomcat 8.5
    server localhost:8083   max_fails=3 fail_timeout=20s;  #tomcat 9.0
}

 In this example, the probability weight parameter specifies the polling, the default weight value of 1,; weight ratio proportional to the value of the access, such as Tomcat 7.0 is twice the probability of being accessed other servers.

  note:

  • The higher the weight assigned to the request requires more processing.
  • This policy can be combined with least_conn and ip_hash.
  • Hardware configuration differences were larger cases, this strategy is more suitable server.

3、ip_hash

Load balancer according to the specified allocation method based on the client IP, this method ensures that the same client request has been sent to the same server, in order to ensure session session. Visitors are fixed so that each access a back-end server, can not solve the problem session across servers.

{dynamic_test upstream 
    ip_hash;     # ensure that each guest access to a fixed back-end server 
    Server localhost: 8080 weight = 2;   #tomcat 7.0 
    Server localhost: 8081;   #tomcat 8.0 
    Server localhost: 8082;   #tomcat 8.5 
    Server localhost: 8083 max_fails. 3 = 20S = fail_timeout;   #tomcat 9.0 
}

note:

  • Before nginx version 1.3.1, can not be used in ip_hash weight (weight).
  • ip_hash not be used with backup.
  • This policy suitable for state services, such as session.
  • When the server needs to have removed, you must manually goes down.

4、least_conn

Forwards the request to the back-end server fewer connections. Polling algorithm is the average of the request is forwarded to the back-end, so that their load is substantially the same; however, some request takes a long time, it will result in higher in the back-end load. In this case, least_conn this way we can achieve better load balancing effect.

{dynamic_test upstream 
    least_conn;     # forwards the request to the back-end server fewer connections 
    Server localhost: 8080 weight = 2;   #tomcat 7.0 
    Server localhost: 8081;   #tomcat 8.0 
    Server localhost: 8082 Backup;   #tomcat 8.5 
    Server localhost: 8083 20S = =. 3 fail_timeout max_fails;   #tomcat 9.0 
}

note:

  • This load balancing policy for request processing of varying duration cause the server overload.

5, fair ( third-party policy)

Load balancing policies of third-party plug-ins need to install a third-party.

1. Download: https://github.com/gnosek/nginx-upstream-fair

2, extract

3, installation

./configure --user=nginx --group=nginx --prefix=/zjl/program/nginx-1.14.2 --with-http_stub_status_module --with-http_ssl_module --add-module=/zjl/software/nginx-upstream-fair/

make

make install

Note: bold red part of the fair after decompression newly added module path

4, Configuration

According to the response time of the server allocation request, allocating a short response time priority.

{dynamic_test upstream 
    Fair;     # achieve short response time priority allocation 
    Server localhost: 8080;   #tomcat 7.0 
    Server localhost: 8081;   #tomcat 8.0 
    Server localhost: 8082;   #tomcat 8.5 
    Server localhost: 8083;   #tomcat 9.0 
}

6, url_hash (third-party policy)

 

Load balancing policies of third-party plug-ins need to install a third-party.

You need to download: Nginx_upstream_hash plug

Installation as 5: Add --add-module = /     parameter

Configuration:

Press hash to url to access the results of the allocation request, so that each url directed to the same back-end server, to be used with the cache hit. A resource with multiple requests may arrive on different servers, leading to unnecessary multiple downloads, the cache hit rate is not high, and some waste of resources of time. The use url_hash, you can make the same url (that is, with a request for resources) will reach the same server, once lived cache resources, then this request is received, it can be read from the cache.

{dynamic_test upstream 
    the hash $ REQUEST_URI;     # implement each url directed to the same back-end server 
    Server localhost: 8080;   #tomcat 7.0 
    Server localhost: 8081;   #tomcat 8.0 
    Server localhost: 8082;   #tomcat 8.5 
    Server localhost: 8083;   #tomcat 9.0 
}

 

3, forwarding based on URL, client type, file extension

1, according to the forwarding url

Profiles:

http well equipped with:

upstream n2{
    server 192.168.244.130:80;
}
upstream n3{
    server 192.168.244.131:80;
}

in server configuration:

location /n2/ {
    proxy_pass  http://n2;
}
location /n3/ {
    proxy_pass  http://n3;
}

The second server in the configuration:

location / {
    if ( $request_uri ~* "^/n2/(.*)" ) {
        proxy_pass   http://n2/$1;
    }
    if ( $request_uri ~* "^/n3/(.*)" ) {
        proxy_pass   http://n3/$1;
    }
}

And if there are spaces between the left parenthesis, and there is a space between the right parenthesis and the opening brace

access

http://192.168.244.128/n2/zjl.html   ---> 130 in the zjl.html page

http://192.168.244.128/n3/zjl.html   ---> 131 in the zjl.html page

2. The client device ($ HTTP_USER_AGENT)

Profiles:

location   /  {
    if($http_user_agent  ~*   "MSIE"){
        proxy_pass   http://n2;
    }
    if($http_user_agent  ~*   "Chrome"){
         proxy_pass   http://n3;
    }
    proxy_pass  http://n1;
    include  proxy.conf;
}

If the service is ie pool access n2, if n3 browser to access Google services pool, access to the default service if the other pool

 

location   /  {
    if($http_user_agent  ~*   "android"){
        proxy_pass   http://n2;
    }
    if($http_user_agent  ~*   "ipone"){
        proxy_pass   http://n3;
    }
    proxy_pass  http://pc_pools;
    include  proxy.conf;
}

If android, ipone, ipad and other devices to access different services respectively Pool

3, depending on the file suffix forward

Profiles:

location   ~   .*.(gif|jpg|png)${
    proxy_pass   http://static_pools;
    include  proxy.conf;
}

#或location内加入如下内容:
if($request_uri   ~*    ".*\.(php|php5)$"){
    proxy_pass  http://php_server_pools;
}
if($request_uri   ~*    ".*\.(jsp|do|do*)$"){
    proxy_pass  http://java_server_pools;
}

4. Regular Description:

Reprinted from: https://www.cnblogs.com/netsa/p/6383094.html

1, ^: beginning of the string matching;

2, $: matches the end of the string;

.. 3 *: matches any character, * matches the number 0 to positive infinity;

4, \ slash to escape, \ matching special use, remember the memory..;

5, (value 1 | value of 2 | value 3 | 4 values): or pattern matching, for example: (jpg | gif | png | bmp) matching jpg or bmp or gif, or png

6, i case insensitive

One. Regular expression matching, in which:

~ Is a case-sensitive match

~ * Is a case-insensitive match

! ~ And! ~ * Do not match are case-insensitive and case-insensitive mismatch

two. Files and directories matching, which

-f and! -f used to determine whether a file exists

-d and! -d to determine whether there is a directory

-e and! -e used to determine whether there is a file or directory

-x and! -x is used to determine whether an executable file

three. The last instruction is a parameter rewrite flag tag, flag tag are:

1.last equivalent inside apache [L] symbol indicates rewrite.

After 2.break this rule match is completed, terminate match rule no longer matches the back.

3.redirect return 302 temporary redirect, the browser URL address after the jump address will be displayed.

4.permanent return 301 permanent redirect, the browser URL address after the jump address will be displayed.

Use last break and realize URI rewrite, the browser address bar unchanged. And both nuances, using alias instruction must last flag; proxy_pass use instructions required break mark. Last flag rewrite rules in the finished article, will have on its host server {......} request to reinitiate the tab, and the break is marked after the completion of this rule matching, matching termination.

For example: if we will be similar to URL / photo / 123456 redirected to /path/to/photo/12/1234/123456.png

rewrite "/photo/([0-9]{2})([0-9]{2})([0-9]{2})"/path/to/photo/$1/$1$2/$1$2$3.png ;

 

Reprinted from: http://www.cnblogs.com/1214804270hacker/p/9325150.html

Guess you like

Origin www.cnblogs.com/happydreamzjl/p/10936108.html