glcui 发表于 2018-11-10 12:07:48

Nginx基础配置

  1.Syntax:upstream name { ... }
  Default:—
  Context:http
  Defines a group of servers. Servers can listen on different ports. In addition, servers listening on TCP and UNIX-domain sockets can be mixed.
  用于指定后端服务器组,upstream 后面为自定义的后端服务器组,默认为轮询调度对后端服务器进行转发处理请求,如果后端某一台服务器发生故障调度器服务器会自动将其从组中踢除,待恢复后又自动加回组中。
  2.Syntax:server address ;
  Default:—
  Context:upstream
  Defines the address and other parameters of a server. The address can be specified as a domain name or IP address, with an optional port, or as a UNIX-domain socket path specified after the “unix:” prefix. If a port is not specified, the port 80 is used. A domain name that resolves to several IP addresses defines multiple servers at once.
  The following parameters can be defined:
  weight=number
  sets the weight of the server, by default, 1.
  max_conns=number
  该指令用于指定设置组内服务器
  address 服务器地址
  
  weight=number
  为组内添加权重,权重值越高服务器被优先用于服务器,组内权重默认为1,如果不设置则组内主句轮询处理;
  max_fails=number
  设置一个请求失败的次数,在一定时间范围内当对组内服务器请求失败可允许超过次数,如果请求失败次数超过该值时则认为该服务器为无效,默认值也是为1,
  fail_timeout=number
  该时间设置有两个作用。一是当多长时间尝试去连接已失效服务器,第二个是在max_fails里面说到的在一定时间内对组内服务器请求失败的时间;默认为1;
  backup ;
  将组内某台服务器设置为备用服务器,只有所有服务器都处于无效情况下,该服务器才会被用来处理请求
  down;
  将组内某台服务器设置为永久无效状态。
  示例:
  http {
  upstream baks {
  server 10.1.45.61:80 weight=1 max_fails=2 fail_timeout=30s;
  server 10.1.45.62:80 weight=3;
  server 127.0.0.1:80 backup;
  }
  }
  3.Syntax:ip_hash;
  Default:—
  Context:upstream
  Specifies that a group should use a load balancing method where requests are distributed between servers based on client IP addresses. The first three octets of the client IPv4 address, or the entire IPv6 address, are used as a hashing key. The method ensures that requests from the same client will always be passed to the same server except when this server is unavailable. In the latter case client requests will be passed to another server. Most probably, it will always be the same server as well.
  该指令用于实现会话保持功能,将来自于某一客户端的请求定向到组内任何一台服务器上,保证客户端与服务器之间建立稳定的会话,只有在该服务器在无效情况下才会被下一个服务器接收和处理
  实例如下:
  http {
  ip_hash;
  upstream baks {
  server 10.1.45.61:80 weight=1 max_fails=2 fail_timeout=30s;
  server 10.1.45.62:80 weight=3;
  server 127.0.0.1:80 backup;
  }
  }
  4.Syntax:keepalive connections;
  Default:—
  Context:upstream
  This directive appeared in version 1.1.4.
  Activates the cache for connections to upstream servers.

  The connections parameter sets the maximum number of>  该指令用于设置为每个worker进程保留的空闲的长连接数量。
  5.Syntax:least_conn;
  Default:—
  Context:upstream
  This directive appeared in versions 1.3.1 and 1.2.2.
  Specifies that a group should use a load balancing method where a request is passed to the server with the least number of active connections, taking into account weights of servers. If there are several such servers, they are tried in turn using a weighted round-robin balancing method.
  最少连接调度算法,当server拥有不同的权重时其为wlc;
  6.Syntax:hash key ;
  Default:—
  Context:upstream
  This directive appeared in version 1.7.2.
  Specifies a load balancing method for a server group where the client-server mapping is based on the hashed key value. The key can contain text, variables, and their combinations. Note that adding or removing a server from the group may result in remapping most of the keys to different servers. The method is compatible with the Cache::Memcached Perl library.
  If the consistent parameter is specified the ketama consistent hashing method will be used instead. The method ensures that only a few keys will be remapped to different servers when a server is added to or removed from the group. This helps to achieve a higher cache hit ratio for caching servers. The method is compatible with the Cache::Memcached::Fast Perl library with the ketama_points parameter set to 160.
  基于指定的key的hash表来实现对请求的调度,此处的key可以直接文本、变量或二者的组合;
  作用:将请求分类,同一类请求将发往同一个upstream server;

页: [1]
查看完整版本: Nginx基础配置