upstream:
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. 定义一组服务器;服务器可以监听不同的端口;此外,监听TCP和unix域套接字的服务器也可以混合使用;
server:
Syntax: server address [parameters]; 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.
parameters:
—————
weight=number 权重; sets the weight of the server, by default, 1. max_conns=number 每个后端主机的最大并发连接数; limits the maximum number of simultaneous active connections to the proxied server (1.11.5). Default value is zero, meaning there is no limit. If the server group does not reside in the shared memory, the limitation works per each worker process. max_fails=number 对后端服务器进行健康状态检查,超过此失败次数就标记为不可用; sets the number of unsuccessful attempts to communicate with the server that should happen in the duration set by the fail_timeout parameter to consider the server unavailable for a duration also set by the fail_timeout parameter. By default, the number of unsuccessful attempts is set to 1. The zero value disables the accounting of attempts. What is considered an unsuccessful attempt is defined by the proxy_next_upstream, fastcgi_next_upstream, uwsgi_next_upstream, scgi_next_upstream, memcached_next_upstream, and grpc_next_upstream directives. fail_timeout=time 对后端服务器进行健康状态检查,每次检查的超时时间; sets the time during which the specified number of unsuccessful attempts to communicate with the server should happen to consider the server unavailable; and the period of time the server will be considered unavailable. By default, the parameter is set to 10 seconds. backup 后端所有服务器都不可用时,默认连接的服务器; marks the server as a backup server. It will be passed requests when the primary servers are unavailable. down 程序版本迭代更新时,以此参数标记服务器软停机,此时后端服务器不会被调度; marks the server as permanently unavailable.
keepalive:
Syntax: keepalive connections; Default: — Context: upstream This directive appeared in version 1.1.4. nginx负载均衡服务器上设置,每个worker进程与后端服务器保持的最大空闲长连接数量; 此数值不应该设置过大,以便后端服务器可以处理新的连接请求;
*****************************************
示例配置:
nginx.conf
user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/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 /var/log/nginx/access.log main; proxy_cache_path /data/nginx/cache levels=1:1:1 keys_zone=pcache:10m max_size=2g inactive=10m; proxy_connect_timeout 30s; proxy_read_timeout 30s; proxy_send_timeout 30s; proxy_headers_hash_max_size 5120; proxy_headers_hash_bucket_size 640; fastcgi_cache_path /data/nginx/fastcgicache levels=1:2:1 keys_zone=fcgicache:10m max_size=2g inactive=10m; sendfile on; #tcp_nopush on; keepalive_timeout 65; gzip on; gzip_comp_level 1; gzip_min_length 100k; gzip_buffers 16 8k; gzip_proxied off; #gzip_proxied no-cache no-store private; gzip_types text/css text/xml application/javascript; server_tokens off; upstream webservers { server 192.168.206.22:80 weight=2 max_conns=2000 max_fails=3 fail_timeout=1; server 192.168.206.33:80 max_conns=2000 max_fails=3 fail_timeout=1; server 127.0.0.1:80 backup; } include /etc/nginx/conf.d/*.conf; }
vhost.conf
server { listen 80; server_name 192.168.206.11; location / { proxy_pass http://webservers; } }